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

Support for Windows XP has ended

Microsoft ended support for Windows XP on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q316963
This article has been archived. It is offered "as is" and will no longer be updated.
This article references the following .NET Framework Class Library namespaces:
  • System.Drawing
  • System.Drawing.Imaging
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.
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 "winter.jpg" picture included with Windows XP    Image newImage = Image.FromFile("winter.jpg");    // Load the sample "Water Lilies.jpg" picture included with Windows XP    Image newAlphaImage = Image.FromFile("Water Lilies.jpg");    // 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 = "Text";    string stringText2 = "Line2";    FontFamily family = new FontFamily("Arial");    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 "alpha" TextureBrush we have created                e.Graphics.FillPath(newTextureBrush, myPath);}				

Article ID: 316963 - Last Review: 01/10/2015 13:27:45 - Revision: 3.3

Microsoft GDI+ 1.0, Microsoft .NET Framework 1.0, Microsoft .NET Framework 1.1, Microsoft Windows XP Professional, the operating system: Microsoft Windows XP 64-Bit Edition

  • kbnosurvey kbarchive kbinfo kbgdiplus kbdswgdi2003swept KB316963