public unsafe Bitmap ToManagedImage(bool makeCopy)
{
Bitmap bitmap = null;
try
{
if (!makeCopy)
{
bitmap = new Bitmap(width, height, stride, pixelFormat, imageData);
if (pixelFormat == PixelFormat.Format8bppIndexed)
{
Image.SetGrayscalePalette(bitmap);
}
}
else
{
bitmap = ((pixelFormat == PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(width, height) : new Bitmap(width, height, pixelFormat));
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pixelFormat);
int num = bitmapData.Stride;
int count = System.Math.Min(stride, num);
byte * ptr = (byte *)bitmapData.Scan0.ToPointer();
byte * ptr2 = (byte *)imageData.ToPointer();
if (stride != num)
{
for (int i = 0; i < height; i++)
{
SystemTools.CopyUnmanagedMemory(ptr, ptr2, count);
ptr += num;
ptr2 += stride;
}
}
else
{
SystemTools.CopyUnmanagedMemory(ptr, ptr2, stride * height);
}
bitmap.UnlockBits(bitmapData);
}
return(bitmap);
}
catch (Exception)
{
bitmap?.Dispose();
throw new InvalidImagePropertiesException("The unmanaged image has some invalid properties, which results in failure of converting it to managed image.");
}
}