[VB.NET] Fade in and Fade Out Image Picture Box


Description
      this source code (function) will make you can control opacity of the image in picturebox. opacity range between 0,0 to 1,0 (it's double not integer). i am not owner of this source code but it's help me for my work.


HowTo

'Here's the function

Public Function FadeInImage(ByVal bmp As Bitmap, ByVal opacity As Single) As Bitmap
Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
opacity = Math.Max(0, Math.Min(opacity, 1.0F))
Using ia As New Imaging.ImageAttributes
Dim cm As New Imaging.ColorMatrix
cm.Matrix33 = opacity
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.DrawImage(bmp, destpoints, New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
End Using
End Using
Return bmp2
End Function

to call this function :
picturebox1.Image = FadeInImage(Image.FromFile(My.Application.Info.DirectoryPath & "/myimage.png"), imgOpacityValue).

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih