如何创建逆png图像?

时间:2022-08-17 20:51:16

i am creating png image which painted on my base, from the base i can save a png image, for your reference

我正在创建在我的基础上绘制的png图像,从基础我可以保存一个png图像,供您参考

Graphics g = e.Graphics;
 ....
g.DrawLine(pen, new Point(x, y), new Point(x1, y1));
 .....
base.OnPaint(e);

using (var bmp = new Bitmap(500, 50))
{
    base.DrawToBitmap(bmp, new Rectangle(0, 0, 500, 50));
    bmp.Save(outPath);
}

this is single color transparency image, now how do i can inverse this image like png filled with any color and the real image portion should be transparent, is there any possibilities?

这是单色透明图像,现在我如何反转这个图像像png填充任何颜色和真实图像部分应该是透明的,有可能吗?

bit detail : so transparent will go nontransparent and where there is fill will go to transparent

位细节:透明会变得不透明,填充的地方会变得透明

4 个解决方案

#1


5  

EDIT (thaks for Thomas notation)

编辑(托马斯表示法)

public void ApplyInvert()  
{  
    byte A, R, G, B;  
    Color pixelColor;  

    for (int y = 0; y < bitmapImage.Height; y++)  
    {  
        for (int x = 0; x < bitmapImage.Width; x++)  
        {  
            pixelColor = bitmapImage.GetPixel(x, y);  
            A = (byte)(255 - pixelColor.A); 
            R = pixelColor.R;  
            G = pixelColor.G;  
            B = pixelColor.B;  
            bitmapImage.SetPixel(x, y, Color.FromArgb((int)A, (int)R, (int)G, (int)B));  
        }  
    }  
}

from here : Image Processing in C#: Inverting an image

从这里:C#中的图像处理:反转图像

#2


7  

There's a faster way if you're willing to use unsafe code:

如果您愿意使用不安全的代码,有一种更快捷的方法:

private unsafe void Invert(Bitmap bmp)
{
    int w = bmp.Width, h = bmp.Height;
    BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

    int* bytes = (int*)data.Scan0;
    for ( int i = w*h-1; i >= 0; i-- )
        bytes[i] = ~bytes[i];
    bmp.UnlockBits(data);
}

Note that this doesn't care about the colors and will invert those as well. If you wish to use a specific color, then the code will have to be modified a bit.

请注意,这并不关心颜色,也会反转这些颜色。如果您希望使用特定颜色,则必须稍微修改代码。

#3


6  

For anyone who wants a fast method for inverting Bitmap colors without using unsafe:

对于任何想要快速方法来反转位图颜色而不使用不安全的人:

public static void BitmapInvertColors(Bitmap bitmapImage)
{
    var bitmapRead   = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
    var bitmapLength = bitmapRead.Stride * bitmapRead.Height;
    var bitmapBGRA   = new byte[bitmapLength];
    Marshal.Copy(bitmapRead.Scan0, bitmapBGRA, 0, bitmapLength);
    bitmapImage.UnlockBits(bitmapRead);

    for (int i = 0; i < bitmapLength; i += 4)
    {
        bitmapBGRA[i]     = (byte)(255 - bitmapBGRA[i]);
        bitmapBGRA[i + 1] = (byte)(255 - bitmapBGRA[i + 1]);
        bitmapBGRA[i + 2] = (byte)(255 - bitmapBGRA[i + 2]);
        //        [i + 3] = ALPHA.
    }

    var bitmapWrite = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb);
    Marshal.Copy(bitmapBGRA, 0, bitmapWrite.Scan0, bitmapLength);
    bitmapImage.UnlockBits(bitmapWrite);
}

Bitmap GetPixel and SetPixel are extremly slow, this method works by copying the Bitmap pixels into a byte array, which you can then loop through and change, before finally copying the pixels back.

Bitmap GetPixel和SetPixel非常慢,这种方法的工作原理是将Bitmap像素复制到一个字节数组中,然后在最终复制像素之前可以循环并更改。

#4


0  

When you say invert the transparent sections into color, are you storing the real colors in the PNG image just set to full transparency? A lot of programs will optimize a png by removing the color data from transparency so you can't reverse it.

当您说将透明部分反转为彩色时,您是否将实际颜色存储在刚刚设置为完全透明的PNG图像中?许多程序将通过从透明度中删除颜色数据来优化png,因此您无法对其进行反转。

Colors can be converted to transparency But transparency (without underlying colors) cannot be converted to color.

颜色可以转换为透明度但是透明度(没有底层颜色)无法转换为颜色。

If your lucky your PNG will be non optimized and still have the original color data intact, but if your doing this from user input then it won't work for a high percentage of cases.

如果幸运的话,你的PNG将不经过优化并且仍然保持原始颜色数据的完整性,但是如果你从用户输入中这样做,那么它将不适用于大部分情况。

#1


5  

EDIT (thaks for Thomas notation)

编辑(托马斯表示法)

public void ApplyInvert()  
{  
    byte A, R, G, B;  
    Color pixelColor;  

    for (int y = 0; y < bitmapImage.Height; y++)  
    {  
        for (int x = 0; x < bitmapImage.Width; x++)  
        {  
            pixelColor = bitmapImage.GetPixel(x, y);  
            A = (byte)(255 - pixelColor.A); 
            R = pixelColor.R;  
            G = pixelColor.G;  
            B = pixelColor.B;  
            bitmapImage.SetPixel(x, y, Color.FromArgb((int)A, (int)R, (int)G, (int)B));  
        }  
    }  
}

from here : Image Processing in C#: Inverting an image

从这里:C#中的图像处理:反转图像

#2


7  

There's a faster way if you're willing to use unsafe code:

如果您愿意使用不安全的代码,有一种更快捷的方法:

private unsafe void Invert(Bitmap bmp)
{
    int w = bmp.Width, h = bmp.Height;
    BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

    int* bytes = (int*)data.Scan0;
    for ( int i = w*h-1; i >= 0; i-- )
        bytes[i] = ~bytes[i];
    bmp.UnlockBits(data);
}

Note that this doesn't care about the colors and will invert those as well. If you wish to use a specific color, then the code will have to be modified a bit.

请注意,这并不关心颜色,也会反转这些颜色。如果您希望使用特定颜色,则必须稍微修改代码。

#3


6  

For anyone who wants a fast method for inverting Bitmap colors without using unsafe:

对于任何想要快速方法来反转位图颜色而不使用不安全的人:

public static void BitmapInvertColors(Bitmap bitmapImage)
{
    var bitmapRead   = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
    var bitmapLength = bitmapRead.Stride * bitmapRead.Height;
    var bitmapBGRA   = new byte[bitmapLength];
    Marshal.Copy(bitmapRead.Scan0, bitmapBGRA, 0, bitmapLength);
    bitmapImage.UnlockBits(bitmapRead);

    for (int i = 0; i < bitmapLength; i += 4)
    {
        bitmapBGRA[i]     = (byte)(255 - bitmapBGRA[i]);
        bitmapBGRA[i + 1] = (byte)(255 - bitmapBGRA[i + 1]);
        bitmapBGRA[i + 2] = (byte)(255 - bitmapBGRA[i + 2]);
        //        [i + 3] = ALPHA.
    }

    var bitmapWrite = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb);
    Marshal.Copy(bitmapBGRA, 0, bitmapWrite.Scan0, bitmapLength);
    bitmapImage.UnlockBits(bitmapWrite);
}

Bitmap GetPixel and SetPixel are extremly slow, this method works by copying the Bitmap pixels into a byte array, which you can then loop through and change, before finally copying the pixels back.

Bitmap GetPixel和SetPixel非常慢,这种方法的工作原理是将Bitmap像素复制到一个字节数组中,然后在最终复制像素之前可以循环并更改。

#4


0  

When you say invert the transparent sections into color, are you storing the real colors in the PNG image just set to full transparency? A lot of programs will optimize a png by removing the color data from transparency so you can't reverse it.

当您说将透明部分反转为彩色时,您是否将实际颜色存储在刚刚设置为完全透明的PNG图像中?许多程序将通过从透明度中删除颜色数据来优化png,因此您无法对其进行反转。

Colors can be converted to transparency But transparency (without underlying colors) cannot be converted to color.

颜色可以转换为透明度但是透明度(没有底层颜色)无法转换为颜色。

If your lucky your PNG will be non optimized and still have the original color data intact, but if your doing this from user input then it won't work for a high percentage of cases.

如果幸运的话,你的PNG将不经过优化并且仍然保持原始颜色数据的完整性,但是如果你从用户输入中这样做,那么它将不适用于大部分情况。