Hi, I'm trying to write a function that takes an image and a given value and then changes the opacity of the image. Though, when I try to run the code nothing happens. Can someone look at the code and tell me what I did wrong?

Public Function SetOpacity(ByVal Image As Bitmap, ByVal Opacity As Integer, ByVal G As Graphics) As Image
        Dim NewImage As New Bitmap(Image.Width, Image.Height, G)
        G = Graphics.FromImage(NewImage)
        G.Clear(Color.Transparent)
        For X As Integer = 1 To Image.Width
            For Y As Integer = 1 To Image.Height
                Dim BasicColor As Color = Image.GetPixel(X, Y)
                NewImage.SetPixel(X, Y, Color.FromArgb(Opacity, _
                                                       BasicColor.R, BasicColor.G, BasicColor.B))
            Next
        Next
        Return NewImage
    End Function

Thanks,
J-P

Opacity is an alpha setting! Either master or each pixel.

DstRGB = (SrcRGB * SrcAlpha) + ((1.0 - SrcAlpha) * DstRGB)

An alpha blending is similar to as follows

a = (1.0f / 255.0f) * (float)pSrc->a;
   d = 1.0f - a;
   pDst->b = (byte)((a * pSrc->b) + (d * pDst->b));
   pDst->g = (byte)((a * pSrc->g) + (d * pDst->g));
   pDst->r = (byte)((a * pSrc->r) + (d * pDst->r));

But shouldn't the code

NewImage.SetPixel(X, Y, Color.FromArgb(Opacity, BasicColor.R, BasicColor.G, BasicColor.B))

work just like your code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.