Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create inverse png image?

Tags:

c#

.net

wpf

onpaint

i am creating png image which painted 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 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?

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

like image 216
manny Avatar asked Dec 01 '22 23:12

manny


1 Answers

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.

like image 67
Vilx- Avatar answered Dec 05 '22 07:12

Vilx-