我在互联网上找到这个功能来裁剪图像,如果pictureBox源sizeMode正常,它会很好。但是当pictureBox sizeMode被缩放时,它仍然像普通的sizeMode一样被克隆。
如何克隆与缩放的pictureBox相同的内容?不是原始普通位图的大小?
public static Bitmap CropBitmap(Bitmap bitmap, int x, int y, int w, int h)
{
Rectangle rect = new Rectangle(x, y, w, h);
Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
return cropped;
}
并像这样使用
pictureBox2.Image = CropBitmap((Bitmap)pictureBox1.Image.Clone(), 35, 0, 110, 150);
答案 0 :(得分:6)
最简单的方法是使用DrawToBitmap方法获取图片框中的输出而不管SizeMode
,然后像这样裁剪输出:
public static Bitmap Crop(PictureBox pb, int x, int y, int w, int h)
{
var rect = pb.ClientRectangle;
using (var output = new Bitmap(rect.Width, rect.Height, pb.Image.PixelFormat))
{
pb.DrawToBitmap(output, rect);
return output.Clone(new Rectangle(x, y, w, h), output.PixelFormat);
}
}
然而,上述方法的缺点是它确实会裁剪可能缩放的图像。
如果您确实要裁剪原始图像,则需要将传递的矩形(我假设它在图片框客户端坐标中)映射到原始图像坐标。
如果图片框提供方法ClientToImage
(类似于ClientToScreen
)会很好,但它没有,所以我们需要从中提取SizeMode
逻辑Reference Source。
新方法是这样的:
public static class ImageUtils
{
public static Bitmap CropImage(this PictureBox pb, int x, int y, int w, int h)
{
var imageRect = pb.GetImageRectangle();
var image = pb.Image;
float scaleX = (float)image.Width / imageRect.Width;
float scaleY = (float)image.Height / imageRect.Height;
var cropRect = new Rectangle();
cropRect.X = Scale(x - imageRect.X, scaleX, image.Width);
cropRect.Y = Scale(y - imageRect.Y, scaleY, image.Height);
cropRect.Width = Scale(w, scaleX, image.Width - cropRect.X);
cropRect.Height = Scale(h, scaleY, image.Height - cropRect.Y);
var result = new Bitmap(cropRect.Width, cropRect.Height, image.PixelFormat);
using (var g = Graphics.FromImage(result))
g.DrawImage(image, new Rectangle(new Point(0, 0), cropRect.Size), cropRect, GraphicsUnit.Pixel);
return result;
}
static int Scale(int value, float scale, int maxValue)
{
int result = (int)(value * scale);
return result < 0 ? 0 : result > maxValue ? maxValue : result;
}
public static Rectangle GetImageRectangle(this PictureBox pb)
{
var rect = pb.ClientRectangle;
var padding = pb.Padding;
rect.X += padding.Left;
rect.Y += padding.Top;
rect.Width -= padding.Horizontal;
rect.Height -= padding.Vertical;
var image = pb.Image;
var sizeMode = pb.SizeMode;
if (sizeMode == PictureBoxSizeMode.Normal || sizeMode == PictureBoxSizeMode.AutoSize)
rect.Size = image.Size;
else if (sizeMode == PictureBoxSizeMode.CenterImage)
{
rect.X += (rect.Width - image.Width) / 2;
rect.Y += (rect.Height - image.Height) / 2;
rect.Size = image.Size;
}
else if (sizeMode == PictureBoxSizeMode.Zoom)
{
var imageSize = image.Size;
var zoomSize = pb.ClientSize;
float ratio = Math.Min((float)zoomSize.Width / imageSize.Width, (float)zoomSize.Height / imageSize.Height);
rect.Width = (int)(imageSize.Width * ratio);
rect.Height = (int)(imageSize.Height * ratio);
rect.X = (pb.ClientRectangle.Width - rect.Width) / 2;
rect.Y = (pb.ClientRectangle.Height - rect.Height) / 2;
}
return rect;
}
}