Search code examples
vb.netopacitypicturebox

Change opacity in picturebox


I use this function

Public Shared Function ChangeOpacity(ByVal img As Image, ByVal opacityvalue As Single) As Bitmap
Dim bmp As New Bitmap(img.Width, img.Height)
  Dim graphics__1 As Graphics = Graphics.FromImage(bmp)
  Dim colormatrix As New colormatrix
        colormatrix.Matrix33 = opacityvalue
        Dim imgAttribute As New ImageAttributes
        imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
        graphics__1.DrawImage(img, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, _
         GraphicsUnit.Pixel, imgAttribute)
        graphics__1.Dispose()
    Return bmp
End Function

I use this command

picturebox.Image = ChangeOpacity(picturebox.Image, 0.3)

This is work ,The picturebox's opacity is fade out. but when I try this

picturebox.Image = ChangeOpacity(picturebox.Image, 1.0)

It is nothing happen , The picture is still fade out I want to return picture into default How to fix this ? Thank you very much


Solution

  • Load the image form file like this

    PictureBox1.Image = Image.FromFile("C:\Documents and 
    Settings\User3\Desktop\Sathish\image\calendar-icon-reportdate.png")
    

    And use the call opacity like this

     PictureBox1.Image.Dispose()
     PictureBox1.Image = ChangeOpacity(Image.FromFile("C:\Documents and
     Settings\User3\Desktop\Sathish\image\calendar-icon-reportdate.png"), 0.3)
    
    
    
     PictureBox1.Image.Dispose()
     PictureBox1.Image = ChangeOpacity(Image.FromFile("C:\Documents and
     Settings\User3\Desktop\Sathish\image\calendar-icon-reportdate.png"), 1)