如何创建逆png图像?

man*_*nny 4 .net c# wpf onpaint

我正在创建在我的基础上绘制的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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

Vil*_*lx- 7

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 给了一个链接.基本上它是一个使用指针的代码(就像`int*`那里)..NET运行时无法验证此代码是否会执行愚蠢的操作,例如编写未分配的内存,因此名称为"unsafe".因为如果你不小心它会损坏程序存储器.要编译此代码,您必须在项目选项中启用不安全的代码(默认情况下它是关闭的),我认为也存在一些安全隐患(不安全的代码在部分受信任的程序集中不起作用). (2认同)

Ria*_*Ria 6

编辑 (托马斯表示法)

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));  
        }  
    }  
}
Run Code Online (Sandbox Code Playgroud)

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


Who*_*ich 6

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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