Bitmap Alpha Channel

时间:2016-02-03 17:31:32

标签: c# bitmap

I'm working on a simple drawing application where I can 'paint' on top of an existing image. I've made a little headway, but I've noticed a weird issue with the alpha channel of the bitmap I'm displaying that doesn't seem quite right. My draw brush function looks like this:

public unsafe void BitmapDrawBrush(double _x, double _y, double _radius, double _falloff, double _strength)
    {
        if (DisplayBmp == null)
        {
            DisplayBmp = new Bitmap(ImageWidth, ImageHeight, PixelFormat.Format32bppArgb);
        }

        const int pixelSize = 4; // 32 bits per pixel

        Bitmap target = new Bitmap(DisplayBmp.Width, DisplayBmp.Height, PixelFormat.Format32bppArgb);

        BitmapData sourceData = null, targetData = null;

        try
        {
            sourceData = DisplayBmp.LockBits(new Rectangle(0, 0, DisplayBmp.Width, DisplayBmp.Height),ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            targetData = target.LockBits(new Rectangle(0, 0, target.Width, target.Height),ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            for (int y = 0; y < DisplayBmp.Height; ++y)
            {
                byte* sourceRow = (byte*)sourceData.Scan0 + (y * sourceData.Stride);
                byte* targetRow = (byte*)targetData.Scan0 + (y * targetData.Stride);

                for (int x = 0; x < DisplayBmp.Width; ++x)
                {
                    byte b = sourceRow[x * pixelSize + 0];
                    byte g = sourceRow[x * pixelSize + 1];
                    byte r = sourceRow[x * pixelSize + 2];
                    byte a = sourceRow[x * pixelSize + 3];

                    double nx = x / (double)ImageWidth;
                    double ny = y/(double)ImageHeight;

                    double xDist = nx - _x;
                    double yDist = ny - _y;

                    if ((xDist * xDist) + (yDist * yDist) <= (_radius * _radius))
                    {

                        double pxDist = 1.0 - (((xDist * xDist) + (yDist * yDist)) / (_radius * _radius));

                        r = (byte)(255 * pxDist);
                        g = (byte)(255 * pxDist);
                        b = (byte)(255 * pxDist);
                        a = (byte)(255 * pxDist * _strength);  // <-the alpha channel value
                    }

                    targetRow[x * pixelSize + 0] = b;
                    targetRow[x * pixelSize + 1] = g;
                    targetRow[x * pixelSize + 2] = r;
                    targetRow[x * pixelSize + 3] = a;
                }
            }
        }
        finally
        {
            if (sourceData != null)
                DisplayBmp.UnlockBits(sourceData);

            if (targetData != null)
                target.UnlockBits(targetData);
        }

        DisplayBmp = target;

        UpdateBitmap();
    }

Yet, when I run my application and place a few brush strokes (actually the draw brush function is only called on mouse down at the moment so I'm only drawing dots really), you see that the alpha channel doesn't seem to be fading toward the edge. What I would expect is that as the 'dot' gets more black, so too does the alpha channel diminish. See below. Any ideas as to why this is happening?

enter image description here

的文字

0 个答案:

没有答案