Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Bitmap.LockBits和BitmapData设置特定像素(以及这些半径)的不透明度_C#_Image Processing_Bitmap_Unsafe_Bitmapdata - Fatal编程技术网

C# 使用Bitmap.LockBits和BitmapData设置特定像素(以及这些半径)的不透明度

C# 使用Bitmap.LockBits和BitmapData设置特定像素(以及这些半径)的不透明度,c#,image-processing,bitmap,unsafe,bitmapdata,C#,Image Processing,Bitmap,Unsafe,Bitmapdata,我试图设置位图中特定像素周围的不透明度。 现在我可以设置满足某些条件的像素的不透明度。(例如,左侧、右侧、顶部或底部的像素不是100%白色) 我使用的是Bitmap.LockBits和BitmapData,因为这个应用程序将处理非常大的图像 这是我的密码: private void SetOpacity(Bitmap processedBitmap, int radius) { int opacityStep = 255 / radius; int

我试图设置位图中特定像素周围的不透明度。 现在我可以设置满足某些条件的像素的不透明度。(例如,左侧、右侧、顶部或底部的像素不是100%白色)

我使用的是Bitmap.LockBits和BitmapData,因为这个应用程序将处理非常大的图像

这是我的密码:

    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);
        }
    }
private void SetOpacity(位图处理位图,整数半径)
{
int opacityStep=255/半径;
int不透明度=0;
不安全的
{
BitmapData BitmapData=processedBitmap.LockBits(新矩形(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=位图数据。宽度*字节/像素;
字节*ptrFirstPixel=(字节*)bitmapData.Scan0;
对于(int y=0;y
此代码的慢速变体为:

        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 x=0;x
我需要的是设置不透明度,不仅是满足条件的像素,还包括一些半径内的像素。 我还需要一些不透明的步骤

我想做的是:

        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)));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
for(int r=Radius;Radius>0;r--)
{
对于(int x=0;x
但要快得多

我使用的位图的像素格式是32bbpArgb


感谢您的建议。

请具体说明像素格式,它只能在32bppArgb的情况下正确快速地工作。然后改用
int*
,一次访问4个字节。并确保用户没有等待。谢谢。当然,我使用的位图的像素格式是32bppArgb。不像您可能假设的那样“当然”,32bppPArgb的渲染速度大约快十倍。直言不讳,这样你就不必后悔。