Question Draw image semi-transparant

You can paint an image at any transparency by using one of the more complicated overloads of Graphics.DrawImage -- one of the two that take an ImageAttributes as an argument. To make my own life easier, I used an ImageAttributes in a function for setting the opacity of any bitmap. Maybe you'll find it useful too:

'Example of use - displays Pic1 at 75% opacity in PicBox1:
VB.NET:
PictureBox1.Image = FadeBitmap(My.Resources.Pic1, 75)
VB.NET:
Private Function FadeBitmap(ByVal bmp As Bitmap, ByVal OpacityPercent As Single) As Bitmap
   Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
   OpacityPercent = Math.Min(OpacityPercent, 100)
        
   Using ia As New Imaging.ImageAttributes
       Dim cm As New Imaging.ColorMatrix
       cm.Matrix33 = OpacityPercent / 100.0F
       ia.SetColorMatrix(cm)
       Dim destpoints() As PointF = _
              {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
           Using g As Graphics = Graphics.FromImage(bmp2)
               g.Clear(Color.Transparent)
               g.DrawImage(bmp, destpoints, _
                  New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
           End Using
      End Using
  Return bmp2
End Function

bye, VicJ
 
Back
Top