Microsoft KB Archive/317055

From BetaArchive Wiki
Knowledge Base


INFO: Creating and Using a TextureBrush with an Alpha-Based Image in Visual Basic .NET

Article ID: 317055

Article Last Modified on 4/3/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 Q317055

This article references the following .NET Framework Class Library namespaces:

  • System.Drawing
  • System.Drawing.Imaging


SUMMARY

After you have loaded an image from a file by using (for example) the Image.FromFile method, you can specify alpha values for the image by using the ImageAttributes class. When you create a TextureBrush object based on an image, you can add alpha values to the image by specifying an ImageAttributes object. This TextureBrush object must be created by calling 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 file. Next, it loads a second bitmap and creates an ImageAttributes object to specify the alpha values, and then it creates a TextureBrush based on the image and the ImageAttributes object. Finally, it displays a path by using that semitransparent, bitmap-based brush.

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    ' Load the sample "winter.jpg" picture included with Windows XP
    Dim newImage As Image = Image.FromFile("c:\samp32\windowsapp2\winter.jpg")
    ' Load the sample "Water Lilies.jpg" picture included with Windows XP
    Dim newAlphaImage As Image = Image.FromFile("c:\samp32\windowsapp2\Water Lilies.jpg")
    ' Create rectangle for displaying original image.
    Dim destRect1 As New Rectangle(0, 0, 800, 600)
    ' Create width and height for source image.
    Dim width As Single = 800.0F
    Dim height As Single = 600.0F
    Dim units As GraphicsUnit = GraphicsUnit.Pixel
    ' Draw background image to screen.
    e.Graphics.DrawImage(newImage, destRect1, 0.0F, 0.0F, width, _
    height, units)

    ' Create a graphics path object.
    Dim myPath As New GraphicsPath()

    ' Set up all the string parameters.
    Dim stringText1 As String = "Text"
    Dim stringText2 As String = "Line2"
    Dim family As New FontFamily("Arial")
    Dim myfontStyle As Integer = CInt(FontStyle.Italic)
    Dim emSize As Integer = 200
    Dim origin1 As New Point(10, 20)
    Dim origin2 As New Point(10, 300)
    Dim format As StringFormat = StringFormat.GenericDefault
    ' Add the strings to the path.
    myPath.AddString(stringText1, family, myfontStyle, emSize, _
    origin1, format)
    myPath.AddString(stringText2, family, myfontStyle, emSize, _
    origin2, format)

    ' Initialize the color matrix.
    ' Note the value 0.5 in row 4, column 4.  We are making the image 50% transparent
    Dim matrixItems As Single()() = { _
       New Single() {1, 0, 0, 0, 0}, _
       New Single() {0, 1, 0, 0, 0}, _
       New Single() {0, 0, 1, 0, 0}, _
       New Single() {0, 0, 0, 0.5F, 0}, _
       New Single() {0, 0, 0, 0, 1}}

    Dim colorMatrix As New ColorMatrix(matrixItems)

    ' Create an ImageAttributes object and set its color matrix.
    Dim imageAtt As New ImageAttributes()
    imageAtt.SetColorMatrix( _
       colorMatrix, _
       ColorMatrixFlag.Default, _
       ColorAdjustType.Bitmap)

    Dim newRectangleF As New RectangleF(0.0F, 0.0F, width, height)
    ' Create a TextureBrush using the bitmap and the specified ImageAttributes
    Dim newTextureBrush As New TextureBrush(newAlphaImage, _
                                       newRectangleF, imageAtt)
    ' Draw the path using the "alpha" TextureBrush we have created
   e.Graphics.FillPath(newTextureBrush, myPath)
End Sub
                

Keywords: kbdswgdi2003swept kbgdiplus kbinfo KB317055