MSDN Magazine - June 2008 - (Page 105) ChArles Petzold Foundations Bitmaps And Pixel Bits The retained-mode graphics system of Windows® Presentation constructor that requires the pixel dimensions of the bitmap, the Foundation (WPF) has revolutionized Windows graphics pro- horizontal and vertical resolution in dots per inch, and an object gramming. No longer are programs required to recreate their vi- of type PixelFormat. sual appearance on the screen whenever the system demands. The I’ll have more to say about the PixelFormat structure and the recomposition system retains all graphical figures and arranges them lated static PixelFormats class shortly. For creating objects of type into a total visual presentation. RenderTargetBitmap you must use either PixelFormats.Default or Retained-mode graphics certainly makes life more convenient, PixelFormats.Pbgra32 as the last argument to the RenderTargetbut convenience itself has never been a high priority for Windows Bitmap constructor. In either case, you’re creating a bitmap with programmers. It is really the combination of the retained-mode 32 bits per pixel and transparency. graphics system with notification mechanisms such as depenInitially, the RenderTargetBitmap object is entirely transparent. dency properties that unleashes the flexibility and power of WPF. You can then draw on the bitmap by calling the Render method with Graphical objects such as paths an object of type Visual (includand brushes seem to remain Virtual lab Bitmaps, Pixels, and WPF ing classes that derive from Vi“alive” within the composition sual such as FrameworkElement system and continue to respond WPF bitmaps are responsive to changes via both graphics transand Control). You can restore the forms and changes to pixels within the bitmap. In this Virtual lab, to property changes and graphics all-transparent image by calling transforms, thus allowing these you can experiment with the rendertargetBitmap and WriteClear. If the bitmap is currently ableBitmap classes that make your bitmaps responsive to changobjects to be targets of data bindbeing displayed, these calls are es within your application. the environment is all set up, including ings and animations. reflected in the displayed bitmap Charles Petzold’s sample code, so you can begin coding immediI recently discovered that WPF ately as you read this installment of the Foundations column. without any further ado. bitmaps have a similar dynamic Figure 1 shows a small, comExperiment in the virtual lab: quality. A rendered bitmap replete program that demonstrates go.microsoft.com/fwlink/?LinkId=117105 mains responsive to changes— RenderTargetBitmap. The pronot only to graphics transforms gram creates a bitmap 1,200 pixels (this we all knew) but also to changes in the actual pixel bits with- wide and 900 pixels high. These become the PixelWidth and Pixelin the bitmap. Height properties of the bitmap object. Each pixel is 4 bytes wide, The two bitmap classes that reveal this dynamic responsiveness so the bitmap occupies more than 4 megabytes of memory. are RenderTargetBitmap and WriteableBitmap, which are among The call to the RenderTargetBitmap constructor also specifies a the nine classes that derive from BitmapSource, the abstract class resolution of 300 dots per inch. The combination of the pixel dithat is the basis for all bitmap support in WPF. Regardless of how mension and the resolution results in the creation of a bitmap that a program uses one of these bitmap objects—whether it’s displayed is 4 inches wide and 3 inches tall. The device-independent WPF cowith an Image element, or made into a tiled brush with the Image- ordinate system is 96 units to the inch, so the bitmap has a deviceBrush class, or as part of a larger drawing (perhaps mixed with vec- independent width of 384 units and a height of 288 units. These tor graphics) with the ImageDrawing class—the bitmap is never are the numbers you’ll see if you examine the Width and Height simply rendered and forgotten. The bitmap remains alive within properties defined by BitmapSource. the visual composition system and continues to be responsive to The RenderTargetBitmapDemo program uses the Image element application changes. to display the bitmap without stretching. It also traps MouseDown events. For each mouse click, the program creates a DrawingVi- Using RenderTargetBitmap A RenderTargetBitmap is a bitmap on which you can effectively draw by transferring objects of type Visual to its surface. The only way to create a new object of type RenderTargetBitmap is with a Send your questions and comments to mmnet30@microsoft.com. Code download available at msdn2.microsoft.com/magazine/cc135911. June 2008 105 http://go.microsoft.com/fwlink/?LinkId=117105 http://msdn2.microsoft.com/magazine/cc135911
Table of Contents Feed for the Digital Edition of MSDN Magazine - June 2008 MSDN Magazine - June 2008 Contents Toolbox CLR Inside Out Cutting Edge Patterns In Practice SAAS Concurrency Robotics Form Filler GUI Library Service Station Foundations Windows With C++ Concurrent Affairs Going Places { End Bracket } MSDN Magazine - June 2008 MSDN Magazine - June 2008 - Contents (Page Cover1) MSDN Magazine - June 2008 - Contents (Page Cover2) MSDN Magazine - June 2008 - Contents (Page 1) MSDN Magazine - June 2008 - Contents (Page 2) MSDN Magazine - June 2008 - Contents (Page 3) MSDN Magazine - June 2008 - Contents (Page 4) MSDN Magazine - June 2008 - Contents (Page 5) MSDN Magazine - June 2008 - Contents (Page 6) MSDN Magazine - June 2008 - Contents (Page 7) MSDN Magazine - June 2008 - Contents (Page 8) MSDN Magazine - June 2008 - Contents (Page 9) MSDN Magazine - June 2008 - Contents (Page 10) MSDN Magazine - June 2008 - Toolbox (Page 11) MSDN Magazine - June 2008 - Toolbox (Page 12) MSDN Magazine - June 2008 - Toolbox (Page 13) MSDN Magazine - June 2008 - Toolbox (Page 14) MSDN Magazine - June 2008 - CLR Inside Out (Page 15) MSDN Magazine - June 2008 - CLR Inside Out (Page 16) MSDN Magazine - June 2008 - CLR Inside Out (Page 17) MSDN Magazine - June 2008 - CLR Inside Out (Page 18) MSDN Magazine - June 2008 - CLR Inside Out (Page 19) MSDN Magazine - June 2008 - CLR Inside Out (Page 20) MSDN Magazine - June 2008 - CLR Inside Out (Page 21) MSDN Magazine - June 2008 - CLR Inside Out (Page 22) MSDN Magazine - June 2008 - CLR Inside Out (Page 23) MSDN Magazine - June 2008 - CLR Inside Out (Page 24) MSDN Magazine - June 2008 - Cutting Edge (Page 25) MSDN Magazine - June 2008 - Cutting Edge (Page 26) MSDN Magazine - June 2008 - Cutting Edge (Page 27) MSDN Magazine - June 2008 - Cutting Edge (Page 28) MSDN Magazine - June 2008 - Cutting Edge (Page 29) MSDN Magazine - June 2008 - Cutting Edge (Page 30) MSDN Magazine - June 2008 - Cutting Edge (Page 31) MSDN Magazine - June 2008 - Cutting Edge (Page 32) MSDN Magazine - June 2008 - Cutting Edge (Page 33) MSDN Magazine - June 2008 - Cutting Edge (Page 34) MSDN Magazine - June 2008 - Cutting Edge (Page 35) MSDN Magazine - June 2008 - Cutting Edge (Page 36) MSDN Magazine - June 2008 - Patterns In Practice (Page 37) MSDN Magazine - June 2008 - Patterns In Practice (Page 38) MSDN Magazine - June 2008 - Patterns In Practice (Page 39) MSDN Magazine - June 2008 - Patterns In Practice (Page 40) MSDN Magazine - June 2008 - Patterns In Practice (Page 41) MSDN Magazine - June 2008 - Patterns In Practice (Page 42) MSDN Magazine - June 2008 - Patterns In Practice (Page 43) MSDN Magazine - June 2008 - SAAS (Page 44) MSDN Magazine - June 2008 - SAAS (Page 45) MSDN Magazine - June 2008 - SAAS (Page 46) MSDN Magazine - June 2008 - SAAS (Page 47) MSDN Magazine - June 2008 - SAAS (Page 48) MSDN Magazine - June 2008 - SAAS (Page 49) MSDN Magazine - June 2008 - SAAS (Page 50) MSDN Magazine - June 2008 - SAAS (Page 51) MSDN Magazine - June 2008 - SAAS (Page 52) MSDN Magazine - June 2008 - SAAS (Page 53) MSDN Magazine - June 2008 - Concurrency (Page 54) MSDN Magazine - June 2008 - Concurrency (Page 55) MSDN Magazine - June 2008 - Concurrency (Page 56) MSDN Magazine - June 2008 - Concurrency (Page 57) MSDN Magazine - June 2008 - Concurrency (Page 58) MSDN Magazine - June 2008 - Concurrency (Page 59) MSDN Magazine - June 2008 - Concurrency (Page 60) MSDN Magazine - June 2008 - Concurrency (Page 61) MSDN Magazine - June 2008 - Concurrency (Page 62) MSDN Magazine - June 2008 - Concurrency (Page 63) MSDN Magazine - June 2008 - Robotics (Page 64) MSDN Magazine - June 2008 - Robotics (Page 65) MSDN Magazine - June 2008 - Robotics (Page 66) MSDN Magazine - June 2008 - Robotics (Page 67) MSDN Magazine - June 2008 - Robotics (Page 68) MSDN Magazine - June 2008 - Robotics (Page 69) MSDN Magazine - June 2008 - Robotics (Page 70) MSDN Magazine - June 2008 - Robotics (Page 71) MSDN Magazine - June 2008 - Robotics (Page 72) MSDN Magazine - June 2008 - Robotics (Page 73) MSDN Magazine - June 2008 - Robotics (Page 74) MSDN Magazine - June 2008 - Robotics (Page 75) MSDN Magazine - June 2008 - Robotics (Page 76) MSDN Magazine - June 2008 - Robotics (Page 77) MSDN Magazine - June 2008 - Robotics (Page 78) MSDN Magazine - June 2008 - Form Filler (Page 79) MSDN Magazine - June 2008 - Form Filler (Page 80) MSDN Magazine - June 2008 - Form Filler (Page 81) MSDN Magazine - June 2008 - Form Filler (Page 82) MSDN Magazine - June 2008 - Form Filler (Page 83) MSDN Magazine - June 2008 - Form Filler (Page 84) MSDN Magazine - June 2008 - Form Filler (Page 85) MSDN Magazine - June 2008 - GUI Library (Page 86) MSDN Magazine - June 2008 - GUI Library (Page 87) MSDN Magazine - June 2008 - GUI Library (Page 88) MSDN Magazine - June 2008 - GUI Library (Page 89) MSDN Magazine - June 2008 - GUI Library (Page 90) MSDN Magazine - June 2008 - GUI Library (Page 91) MSDN Magazine - June 2008 - GUI Library (Page 92) MSDN Magazine - June 2008 - GUI Library (Page 93) MSDN Magazine - June 2008 - GUI Library (Page 94) MSDN Magazine - June 2008 - GUI Library (Page 95) MSDN Magazine - June 2008 - GUI Library (Page 96) MSDN Magazine - June 2008 - Service Station (Page 97) MSDN Magazine - June 2008 - Service Station (Page 98) MSDN Magazine - June 2008 - Service Station (Page 99) MSDN Magazine - June 2008 - Service Station (Page 100) MSDN Magazine - June 2008 - Service Station (Page 101) MSDN Magazine - June 2008 - Service Station (Page 102) MSDN Magazine - June 2008 - Service Station (Page 103) MSDN Magazine - June 2008 - Service Station (Page 104) MSDN Magazine - June 2008 - Foundations (Page 105) MSDN Magazine - June 2008 - Foundations (Page 106) MSDN Magazine - June 2008 - Foundations (Page 107) MSDN Magazine - June 2008 - Foundations (Page 108) MSDN Magazine - June 2008 - Foundations (Page 109) MSDN Magazine - June 2008 - Foundations (Page 110) MSDN Magazine - June 2008 - Foundations (Page 111) MSDN Magazine - June 2008 - Foundations (Page 112) MSDN Magazine - June 2008 - Windows With C++ (Page 113) MSDN Magazine - June 2008 - Windows With C++ (Page 114) MSDN Magazine - June 2008 - Windows With C++ (Page 115) MSDN Magazine - June 2008 - Windows With C++ (Page 116) MSDN Magazine - June 2008 - Windows With C++ (Page 117) MSDN Magazine - June 2008 - Windows With C++ (Page 118) MSDN Magazine - June 2008 - Concurrent Affairs (Page 119) MSDN Magazine - June 2008 - Concurrent Affairs (Page 120) MSDN Magazine - June 2008 - Concurrent Affairs (Page 121) MSDN Magazine - June 2008 - Concurrent Affairs (Page 122) MSDN Magazine - June 2008 - Concurrent Affairs (Page 123) MSDN Magazine - June 2008 - Concurrent Affairs (Page 124) MSDN Magazine - June 2008 - Concurrent Affairs (Page 125) MSDN Magazine - June 2008 - Concurrent Affairs (Page 126) MSDN Magazine - June 2008 - Going Places (Page 127) MSDN Magazine - June 2008 - Going Places (Page 128) MSDN Magazine - June 2008 - Going Places (Page 129) MSDN Magazine - June 2008 - Going Places (Page 130) MSDN Magazine - June 2008 - Going Places (Page 131) MSDN Magazine - June 2008 - { End Bracket } (Page 132) MSDN Magazine - June 2008 - { End Bracket } (Page Cover3) MSDN Magazine - June 2008 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.