public static Bitmap Clone(this Bitmap source)
{
// lock source bitmap data
BitmapData sourceData = source.LockBits(
new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, source.PixelFormat);
// create new image
Bitmap destination = Clone(sourceData);
// unlock source image
source.UnlockBits(sourceData);
//
if (
(source.PixelFormat == PixelFormat.Format1bppIndexed) ||
(source.PixelFormat == PixelFormat.Format4bppIndexed) ||
(source.PixelFormat == PixelFormat.Format8bppIndexed) ||
(source.PixelFormat == PixelFormat.Indexed))
{
ColorPalette srcPalette = source.Palette;
ColorPalette dstPalette = destination.Palette;
int n = srcPalette.Entries.Length;
// copy pallete
for (int i = 0; i < n; i++)
{
dstPalette.Entries[i] = srcPalette.Entries[i];
}
destination.Palette = dstPalette;
}
return destination;
}