当我按下表单上的按钮时,我有一张想要裁剪的图像。我有以下代码在按下按钮时运行,但它对图像没有任何作用:
try
{
Image image = Image.FromFile("test.jpg");
Bitmap bmp = new Bitmap(200, 200, PixelFormat.Format24bppRgb);
bmp.SetResolution(80, 60);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, 200, 200), 10, 10, 200, 200, GraphicsUnit.Pixel);
// Dispose to free up resources
image.Dispose();
bmp.Dispose();
gfx.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我的图像实际上是带有以下代码的表单活动窗口的屏幕截图:
Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
bitmap.Save("test.jpg", ImageFormat.Jpeg);
}
要完成此操作,请按下同一个按钮,首先我要截取表单的屏幕截图,然后裁剪该图像,但裁剪不起作用。这是为什么?