How to create a reverse png image?

I am creating a png image that is drawn on my base, from the base I can save a png image for your reference

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 a one-color transparent image, now how can I invert this image, for example, PNG, filled with any color, and the real part of the image should be transparent, are there any possibilities?

bit: transparent will be opaque, and where the fill will be transparent.

+4
source share
4 answers

EDIT (thanks for Thomas note)

 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 #: image inversion

+6
source

There is a faster way if you want 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 does not excite colors and inverts them. If you want to use a specific color, then the code will need to be slightly modified.

+7
source

For those who want a quick method to invert Bitmap colors without using unsafe ones:

 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 extremely slow, this method works by copying the pixels of the Bitmap into an array of bytes, which can then be scrolled and changed before finally copying the pixels.

+6
source

When you say to invert transparent sections to color, do you preserve real colors in a PNG image, just set full transparency? Many programs optimize png by removing color data from transparency so that you cannot undo it.

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

If you are lucky, your PNG will not be optimized and will still have the original color data, but if you do this with user input, it will not work for a large percentage of cases.

0
source

All Articles