Hi all,
I am now optimizing a render engine. I have run into difficulty converting my stored byte array into a Bitmap quickly.
I have a conventional way that is slow but works. It merely goes through all the pixels and uses the Bitmap.SetPixel function
GetAsBitmapSlow
public Bitmap GetAsBitmapSlow()
{
Bitmap ReturnImage = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb);
for(int Y = 0; Y < Height; Y++)
for (int X = 0; X < Width; X++)
{
Color PixelColour = GetPixel(X, Y);
ReturnImage.SetPixel(X, Y, PixelColour);
}
return ReturnImage;
}
GetPixel
public Color GetPixel(int X, int Y)
{
if (X >= 0 && Y >= 0 && X < Width && Y < Height)
{
int PixelLocation = (X + (Width * Y)) * 4;
return Color.FromArgb(ARGBcollection[PixelLocation], ARGBcollection[PixelLocation + 1], ARGBcollection[PixelLocation + 2], ARGBcollection[PixelLocation + 3]);
}
return Color.FromArgb(0, 0, 0, 0);
}
This function works as proven by the function that calls it.
I am now opting for a quicker method. This just doesn't work at all.
GetAsBitmap
public Bitmap GetAsBitmap()
{
Bitmap ReturnImage = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb);
BitmapData bmpData = ReturnImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
Marshal.Copy(ARGBcollection, 0, bmpData.Scan0, ARGBcollection.Length);
ReturnImage.UnlockBits(bmpData);
return ReturnImage;
}
My speculation is that the Marshal.Copy is something to do with the error. But after a lot of grief I come to you guys :p
Thank you all :) I hope you can shed some light