0

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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by J-P
0

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));
0

But shouldn't the code

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

work just like your code?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.