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

Article translations Article translations
Article ID: 316963 - View products that this article applies to.
This article was previously published under Q316963
This article references the following .NET Framework Class Library namespaces:
  • System.Drawing
  • System.Drawing.Imaging
Expand all | Collapse all

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 "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);
}
				

Properties

Article ID: 316963 - Last Review: April 4, 2006 - Revision: 3.3
APPLIES TO
  • 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
Keywords: 
kbinfo kbgdiplus kbdswgdi2003swept KB316963

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com