How to create a Bitmap deep copy

∥☆過路亽.° 提交于 2019-11-29 09:09:13
B.Clone(new Rectangle(0, 0, B.Width, B.Height), B.PixelFormat)

You could serialize the bitmap and then deserialize it. Bitmap is serializable.

Another way I stumbled on that achieves the same thing is to rotate or flip the image. Under the hood that seems to create a completely new copy of the bitmap. Doing two rotations or flips lets you end up with an exact copy of the original image.

result.RotateFlip(RotateFlipType.Rotate180FlipX);
result.RotateFlip(RotateFlipType.Rotate180FlipX);

Suppose you already have a bitmap called original with something in it

Bitmap original = new Bitmap( 200, 200 );     
Bitmap copy = new Bitmap(original.Width, original.Height);
using (Graphics graphics = Graphics.FromImage(copy))
{
  Rectangle imageRectangle = new Rectangle(0, 0, copy.Width, copy.Height);
  graphics.DrawImage( original, imageRectangle, imageRectangle, GraphicsUnit.Pixel);
}

This should create a copy of the same size, and draw the original into the copy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!