|
Great work, that what I was looking for , but I need to create a png image with alpha channel
how to do that?
|
|
|
|
|
I am not sure in what step your problem is, so first I will explain how to create/edit a four band bitmap and then how to save it as a .png.
To add an alpha channel to an image, you need to set up your bitmap a bit differently:
int height = b.Height;
int width = b.Width;
BitmapData bmData = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
<br />
unsafe<br />
{<br />
byte* p = (byte*)(void*)Scan0;<br />
int nOffset = stride - width * 4;<br />
<br />
for (int y = 0; y < height; ++y)<br />
{<br />
for (int x = 0; x < width; ++x)<br />
{<br />
p += 4;<br />
}<br />
p += nOffset;<br />
}<br />
}<br />
b.UnlockBits(bmData);<br />
Notice the pixel format is now set up as Format32bppArgb, instead of the typical 24bpp. Alos be careful and remeber you need to increment the pixel value by four instead of three, and likewise nOffset is multiplied by four instead of three.
Once you have your bitmap created, it can then be saved as a .png via the bitmap.save command as follows.
<br />
b.Save(tbDestination.Text, ImageFormat.Png);
modified on Tuesday, April 29, 2008 7:31 AM
|
|
|
|