0

I'm trying to set opacity of surroundings of particular pixels in bitmap. For now I can set opacity of pixels that meets some conditions. (e.g. pixel on left or rigt or top or down is not 100% white)

I'm using Bitmap.LockBits and BitmapData, becouse application will process very big images.

Here is my code:

    private void SetOpacity(Bitmap processedBitmap, int radius)
    {
        int opacityStep = 255 / radius;
        int opacity = 0;

        unsafe
        {
            BitmapData bitmapData = processedBitmap.LockBits(new Rectangle(0, 0, processedBitmap.Width, processedBitmap.Height), ImageLockMode.ReadWrite, processedBitmap.PixelFormat);
            int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(processedBitmap.PixelFormat) / 8;
            int heightInPixels = bitmapData.Height;
            int widthInBytes = bitmapData.Width * bytesPerPixel;
            byte* ptrFirstPixel = (byte*)bitmapData.Scan0;

            for (int y = 0; y < heightInPixels; y++)
            {
                byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
                byte* previousLine = ptrFirstPixel + ((y - 1) * bitmapData.Stride);
                byte* nextLine = ptrFirstPixel + ((y + 1) * bitmapData.Stride);
                for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
                {
                    if (currentLine[x] + currentLine[x + 1] + currentLine[x + 2] == 765)
                    {
                        if (currentLine[x + bytesPerPixel] + currentLine[x + bytesPerPixel + 1] + currentLine[x + bytesPerPixel + 2] != 765)
                        {
                            currentLine[x + 3] = (byte)opacity;
                        }
                        else if (currentLine[x - bytesPerPixel] + currentLine[x - bytesPerPixel - 1] + currentLine[x - bytesPerPixel - 2] != 765)
                        {
                            currentLine[x + 3] = (byte)opacity;
                        }
                        else if (previousLine[x] + previousLine[x + 1] + previousLine[x + 2] != 765)
                        {
                            currentLine[x + 3] = (byte)opacity;
                        }
                        else if (nextLine[x] + nextLine[x + 1] + nextLine[x + 2] != 765)
                        {
                            currentLine[x + 3] = (byte)opacity;
                        }
                    }
                }
            }
            processedBitmap.UnlockBits(bitmapData);
        }
    }

Slow variant of this code is:

        for (int x = 0; x < processedBitmap.Width; x++)
        {
            for (int y = 0; y < processedBitmap.Height; y++)
            {
                if (processedBitmap.GetPixel(x, y) == Color.White)
                {
                    if (processedBitmap.GetPixel(x - 1, y) != Color.White || processedBitmap.GetPixel(x + 1, y) != Color.White
                        || processedBitmap.GetPixel(x, y - 1) != Color.White || processedBitmap.GetPixel(x, y + 1) != Color.White)
                    {
                        processedBitmap.SetPixel(x, y, Color.FromArgb(0, processedBitmap.GetPixel(x, y)));
                    }
                }
            }
        }

What I need is to set opacity not only of pixels that meet conditions, but also pixels in some radius. Also I need some opacity steps.

What I'm trying to do is:

        for (int r = Radius; Radius > 0; r--)
        {
            for (int x = 0; x < processedBitmap.Width; x++)
            {
                for (int y = 0; y < processedBitmap.Height; y++)
                {
                    if (processedBitmap.GetPixel(x, y) == Color.White)
                    {
                        if (processedBitmap.GetPixel(x - 1, y) != Color.White || processedBitmap.GetPixel(x + 1, y) != Color.White
                            || processedBitmap.GetPixel(x, y - 1) != Color.White || processedBitmap.GetPixel(x, y + 1) != Color.White)
                        {
                            processedBitmap.SetPixel(x, y, Color.FromArgb(0, processedBitmap.GetPixel(x, y)));
                            for (int i = -r; i < r; i++)
                            {
                                for (int j = -r; j < r; j++)
                                {
                                    if ((i * i + j * j) < (r * r))
                                    {
                                        processedBitmap.SetPixel(x + i, y + j, Color.FromArgb(OpacityStep * r, processedBitmap.GetPixel(x + i, y + i)));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

But way faster.

Pixel format of bitmaps i use is 32bbpArgb

Thanks for any suggestions.

  • Be specific about the pixel format, it will only work correctly and fast with 32bppArgb. And then use `int*` instead so you access 4 bytes at a time. And make sure the user isn't waiting for it. – Hans Passant Dec 15 '15 at 16:55
  • Thanks. Of course the pixel format of bitmaps I use is 32bppArgb. – Henryk Zaborowski Dec 15 '15 at 17:00
  • Not as "of course" as you might assume, 32bppPArgb is about ten times faster to render. Be explicit so you never have to be sorry. – Hans Passant Dec 15 '15 at 17:06

0 Answers0