--- title: "Optimizing Performance: 2D Graphics and Imaging" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "graphics [WPF], performance" - "drawing [WPF], optimizing performance" - "imaging [WPF], optimizing performance" - "shapes [WPF], optimizing performance" - "2D graphics [WPF]" - "images [WPF], optimizing performance" ms.assetid: e335601e-28c8-4d64-ba27-778fffd55f72 --- # Optimizing Performance: 2D Graphics and Imaging [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] provides a wide range of 2D graphics and imaging functionality that can be optimized for your application requirements. This topic provides information about performance optimization in those areas. ## Drawing and Shapes [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] provides both and objects to represent graphical drawing content. However, objects are simpler constructs than objects and provide better performance characteristics. A allows you to draw a graphical shape to the screen. Because they are derived from the class, objects can be used inside panels and most controls. [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] offers several layers of access to graphics and rendering services. At the top layer, objects are easy to use and provide many useful features, such as layout and event handling. [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] provides a number of ready-to-use shape objects. All shape objects inherit from the class. Available shape objects include , , , , , and . objects, on the other hand, do not derive from the class and provide a lighter-weight implementation for rendering shapes, images, and text. There are four types of objects: - Draws a shape. - Draws an image. - Draws text. - Draws other drawings. Use a drawing group to combine other drawings into a single composite drawing. The object is used to render geometry content. The class and the concrete classes that derive from it, such as , , and , provide a means for rendering 2D graphics and providing hit-testing and clipping support. Geometry objects can be used to define the region of a control, for example, or to define the clip region to apply to an image. Geometry objects can be simple regions, such as rectangles and circles, or composite regions created from two or more geometry objects. More complex geometric regions can be created by combining -derived objects, such as , , and . On the surface, the class and the class are similar. Both are used in the rendering of 2D graphics and both have similar concrete classes that derive from them, for example, and . However, there are important differences between these two sets of classes. For one, the class lacks some of the functionality of the class, such as the ability to draw itself. To draw a geometry object, another class such as DrawingContext, Drawing, or a Path (it is worth noting that a Path is a Shape) must be used to perform the drawing operation. Rendering properties such as fill, stroke, and the stroke thickness are on the class that draws the geometry object, while a shape object contains these properties. One way to think of this difference is that a geometry object defines a region, for example, a circle, while a shape object defines a region, defines how that region is filled and outlined, and participates in the layout system. Since objects derive from the class, using them can add significantly more memory consumption in your application. If you really do not need the features for your graphical content, consider using the lighter-weight objects. For more information on objects, see [Drawing Objects Overview](../graphics-multimedia/drawing-objects-overview.md). ## StreamGeometry Objects The object is a lightweight alternative to for creating geometric shapes. Use a when you need to describe a complex geometry. is optimized for handling many objects and performs better when compared to using many individual objects. The following example uses attribute syntax to create a triangular in [!INCLUDE[TLA2#tla_xaml](../../../includes/tla2sharptla-xaml-md.md)]. [!code-xaml[GeometriesMiscSnippets_snip#StreamGeometryTriangleExampleWholePage](~/samples/snippets/xaml/VS_Snippets_Wpf/GeometriesMiscSnippets_snip/XAML/StreamGeometryExample.xaml#streamgeometrytriangleexamplewholepage)] For more information on objects, see [Create a Shape Using a StreamGeometry](../graphics-multimedia/how-to-create-a-shape-using-a-streamgeometry.md). ## DrawingVisual Objects The object is a lightweight drawing class that is used to render shapes, images, or text. This class is considered lightweight because it does not provide layout or event handling, which improves its performance. For this reason, drawings are ideal for backgrounds and clip art. For more information, see [Using DrawingVisual Objects](../graphics-multimedia/using-drawingvisual-objects.md). ## Images [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] imaging provides a significant improvement over the imaging capabilities in previous versions of Windows. Imaging capabilities, such as displaying a bitmap or using an image on a common control, were primarily handled by the Microsoft Windows Graphics Device Interface (GDI) or Microsoft Windows GDI+ application programming interface (API). These APIs provided baseline imaging functionality but lacked features such as support for codec extensibility and high fidelity image support. WPF Imaging APIs have been redesigned to overcome the shortcomings of GDI and GDI+ and provide a new set of APIs to display and use images within your applications. When using images, consider the following recommendations for gaining better performance: - If your application requires you to display thumbnail images, consider creating a reduced-sized version of the image. By default, [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] loads your image and decodes it to its full size. If you only want a thumbnail version of the image, [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] unnecessary decodes the image to its full-size and then scales it down to a thumbnail size. To avoid this unnecessary overhead, you can either request [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] to decode the image to a thumbnail size, or request [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] to load a thumbnail size image. - Always decode the image to desired size and not to the default size. As mentioned above, request [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] to decode your image to a desired size and not the default full size. You will reduce not only your application's working set, but execution speed as well. - If possible, combine the images into a single image, such as a film strip composed of multiple images. - For more information, see [Imaging Overview](../graphics-multimedia/imaging-overview.md). ### BitmapScalingMode When animating the scale of any bitmap, the default high-quality image resampling algorithm can sometimes consume sufficient system resources to cause frame rate degradation, effectively causing animations to stutter. By setting the property of the object to , you can create a smoother animation when scaling a bitmap. mode tells the [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] rendering engine to switch from a quality-optimized algorithm to a speed-optimized algorithm when processing images. The following example shows how to set the for an image object. [!code-csharp[RenderOptions#RenderOptionsSnippet2](~/samples/snippets/csharp/VS_Snippets_Wpf/RenderOptions/CSharp/Window1.xaml.cs#renderoptionssnippet2)] [!code-vb[RenderOptions#RenderOptionsSnippet2](~/samples/snippets/visualbasic/VS_Snippets_Wpf/RenderOptions/visualbasic/window1.xaml.vb#renderoptionssnippet2)] ### CachingHint By default, [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] does not cache the rendered contents of objects, such as and . In static scenarios where the contents or use of the in the scene aren't changing, this makes sense, since it conserves video memory. It does not make as much sense when a with static content is used in a non-static way—for example, when a static or is mapped to the surface of a rotating 3D object. The default behavior of [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] is to re-render the entire content of the or for every frame, even though the content is unchanging. By setting the property of the object to , you can increase performance by using cached versions of the tiled brush objects. The and property values are relative size values that determine when the object should be regenerated due to changes in scale. For example, by setting the property to 2.0, the cache for the only needs to be regenerated when its size exceeds twice the size of the current cache. The following example shows how to use the caching hint option for a . [!code-csharp[RenderOptions#RenderOptionsSnippet3](~/samples/snippets/csharp/VS_Snippets_Wpf/RenderOptions/CSharp/Window1.xaml.cs#renderoptionssnippet3)] [!code-vb[RenderOptions#RenderOptionsSnippet3](~/samples/snippets/visualbasic/VS_Snippets_Wpf/RenderOptions/visualbasic/window1.xaml.vb#renderoptionssnippet3)] ## See also - [Optimizing WPF Application Performance](optimizing-wpf-application-performance.md) - [Planning for Application Performance](planning-for-application-performance.md) - [Taking Advantage of Hardware](optimizing-performance-taking-advantage-of-hardware.md) - [Layout and Design](optimizing-performance-layout-and-design.md) - [Object Behavior](optimizing-performance-object-behavior.md) - [Application Resources](optimizing-performance-application-resources.md) - [Text](optimizing-performance-text.md) - [Data Binding](optimizing-performance-data-binding.md) - [Other Performance Recommendations](optimizing-performance-other-recommendations.md) - [Animation Tips and Tricks](../graphics-multimedia/animation-tips-and-tricks.md)