Monday, March 27, 2017

WPF Tip #6 - Freeze Vector Image Resources

The next several tips will examine Freezable objects. The description from MSDN:

A Freezable is a special type of object that has two states: unfrozen and frozen. When unfrozen, a Freezable appears to behave like any other object. When frozen, a Freezable can no longer be modified.

A few examples of WPF Freezable objects are Brushes, DrawingImages and Animation KeyFrames. If an application uses any of these resources and they are not going to be modified, they should be frozen. This will improve the performance of the application, as less processing will be used to re-render graphical elements.

Here is a quick example of Freezing a DrawingImage. Consider vector graphics used in fixed-size elements on a window (toolbar buttons, for example). These are good candidates to be frozen.

   <DrawingImage x:Key="SimpleImage" presentationOptions:Freeze="True"> 
      <DrawingImage.Drawing> 
         <DrawingGroup> 
            <DrawingGroup.Children> 
               <GeometryDrawing Brush="Black"> 
                  <GeometryDrawing.Geometry> 
                    <EllipseGeometry Center="50,50" RadiusX="50"  RadiusY="50"/> 
                  </GeometryDrawing.Geometry> 
                  <GeometryDrawing.Pen> 
                    <Pen Brush="Red" Thickness="2" /> 
                  </GeometryDrawing.Pen> 
               </GeometryDrawing> 
            </DrawingGroup.Children> 
         </DrawingGroup> 
     </DrawingImage.Drawing> 
   </DrawingImage>


Happy coding!


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.