Microsoft KB Archive/316963

= INFO: How to Create and Use a TextureBrush with an Alpha-Based Image in C# =

Article ID: 316963

Article Last Modified on 4/4/2006

-

APPLIES TO


 * Microsoft GDI+ 1.0
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Professional for Itanium-based systems

-



This article was previously published under Q316963



This article references the following .NET Framework Class Library namespaces:
 * System.Drawing
 * System.Drawing.Imaging



SUMMARY
After you use Image.FromFile method to (for example) load an image from a file, you can use the ImageAttributes class to specify alpha values for the image. When you create a TextureBrush object based on an image, you can add alpha values to the image by specifying an ImageAttributes object. To create this TextureBrush object, you must call the corresponding overloaded TextureBrush constructor, which includes an ImageAttributes object in the parameter list. After the brush is created, you can use it to fill the interiors of graphical shapes, such as rectangles, ellipses, pies, polygons, and paths.



MORE INFORMATION
The ImageAttributes class allows you to modify the alpha values of images during rendering. In the following sample code, an ImageAttributes object is used to set all the alpha values to 50 percent of what they were. This is done by initializing a color matrix and setting the alpha scaling value in the matrix to 0.5. The sample code draws a background image that is loaded from a file. Then, it loads a second bitmap and creates an ImageAttributes object to specify the alpha values, after which it creates a TextureBrush object based on the image and the ImageAttributes object. Finally, it displays a path by using that semitransparent, bitmap-based TextureBrush object. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {

// Load the sample &quot;winter.jpg&quot; picture included with Windows XP   Image newImage = Image.FromFile(&quot;winter.jpg&quot;); // Load the sample &quot;Water Lilies.jpg&quot; picture included with Windows XP   Image newAlphaImage = Image.FromFile(&quot;Water Lilies.jpg&quot;); // Create rectangle for displaying the image (Both JPG's are 800x600). Rectangle destRect1 = new Rectangle( 0, 0, 800, 600); // Create coordinates of rectangle for source image. float width = 800.0F; float height = 600.0F; GraphicsUnit units = GraphicsUnit.Pixel; // Draw background image to the screen. e.Graphics.DrawImage(newImage, destRect1, 0.0F, 0.0F, width, height, units);

// Create a GraphicsPath object. GraphicsPath myPath = new GraphicsPath; // Set up all the string parameters. string stringText1 = &quot;Text&quot;; string stringText2 = &quot;Line2&quot;; FontFamily family = new FontFamily(&quot;Arial&quot;); int fontStyle = (int)FontStyle.Italic; int emSize = 200; // Let's create a large font to show off the alpha bitmap. Point origin1 = new Point(10, 20); Point origin2 = new Point(10, 300); StringFormat format = StringFormat.GenericDefault; // Add the string to the path. myPath.AddString(stringText1,family,fontStyle,        emSize,origin1,format); myPath.AddString(stringText2,family,fontStyle,        emSize,origin2,format); // Initialize the color matrix. // Note the value 0.5 in row 4, column 4. We are making the image 50% transparent. float[][] matrixItems ={ new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, 0.5f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

// Create an ImageAttributes object and set its color matrix. ImageAttributes imageAtt = new ImageAttributes; imageAtt.SetColorMatrix(colorMatrix,       ColorMatrixFlag.Default,        ColorAdjustType.Bitmap);

RectangleF newRectangleF = new RectangleF(0.0F,0.0F,width,height); // Create a TextureBrush using the bitmap and the specified ImageAttributes TextureBrush newTextureBrush = new TextureBrush(newAlphaImage,                                          newRectangleF,imageAtt); // Draw the path using the &quot;alpha&quot; TextureBrush we have created e.Graphics.FillPath(newTextureBrush, myPath); }

Keywords: kbinfo kbgdiplus kbdswgdi2003swept KB316963

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.