如何获取图像的一部分并将其用作单独的图像?

时间:2011-09-03 19:51:43

标签: c# .net image image-processing

我有一个tileset但是所有的tile都在一个图像中。我希望得到某种位图或图像数组中的每个图块。 是否有某种方法可以做到这一点?

1 个答案:

答案 0 :(得分:21)

使用Bitmap.Clone(Rectangle, PixelFormat) ,我们可以设置新图片的PixelFormat和矩形的大小

// Create a Bitmap object from a file.
Bitmap myBitmap = new Bitmap("Grapes.jpg");

// Clone a portion of the Bitmap object.
Rectangle cloneRect = new Rectangle(0, 0, 100, 100);
System.Drawing.Imaging.PixelFormat format =
    myBitmap.PixelFormat;
Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);

// Draw the cloned portion of the Bitmap object.
e.Graphics.DrawImage(cloneBitmap, 0, 0);
相关问题