I want to copy bytes with Marshall.Copy. My code work, but bytes is strange for me. I think I got indexes, not real bytes. If this compute and saves back, I got different colors in the image with a lot bigger byte size (image size is the same).
Bitmap bmp = new Bitmap(imagepath);
Width = bmp.Width;
Height = bmp.Height;
byte[] data;
BitmapData bdata;
switch (bmp.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
{
data = new byte[Width * Height];
bdata = bmp.LockBits(new Rectangle(0, 0, Width, Height),ImageLockMode.ReadOnly, bmp.PixelFormat);
Marshal.Copy(bdata.Scan0, data, 0, data.Length);
bmp.UnlockBits(bdata);
break;
}
}
Save image from bytes:
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(data, 0, pNative, Width * Height);
bmp.UnlockBits(bmData);
bmp.Save("output.gif",ImageFormat.Gif); //without format, have same problem
If I read color from first pixel, I got: Color [A=0, R=0, G=0, B=2]
. Is this really color in the input image?
I don't know, why the output is soo different from the input. Where is the problem?