4

当我按下表单上的按钮时,我有一张想要裁剪的图像。我有以下代码在按下按钮时运行,但它对图像没有任何作用:

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);
}

要完成此操作,请按下同一个按钮,首先我要截取表单的屏幕截图,然后裁剪该图像,但裁剪不起作用。这是为什么?

4

4 回答 4

3

您的代码与我用来保存裁剪图像的代码很接近。您缺少保存裁剪图像的部分。您需要将裁剪后的图像写入字节流,然后将其保存到磁盘。我修改了你的代码,它未经测试,但试一试。

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);

    //Need to write the file to memory then save it
    MemorySteam ms = new MemoryStream();
    bmp.Save(ms, image.RawFormat); 
    byte[] buffer = ms.GetBuffer();

    var stream = new MemorySteam((buffer), 0, buffer.Length); 
    var croppedImage = SD.Image.FromStream(steam, true);
    croppedImage.Save("/your/path/image.jpg", croppedImage.RawFormat);

    // Dispose to free up resources
    image.Dispose();
    bmp.Dispose();
    gfx.Dispose();
    stream.Dispose();
    croppedImage.Dispose();

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
于 2012-04-25T15:42:18.927 回答
0

您可以使用 Bitmap 类的 Clone 方法 ( http://msdn.microsoft.com/en-us/library/ms141944.aspx ) 来获取目标 Bitmap 的子矩形。

于 2012-04-25T13:44:00.417 回答
0

您将在变量 bmp 中得到结果。你可以继续努力。如果您处置该对象,您的更改当然会丢失。

于 2012-04-25T13:44:05.723 回答
-1

我为我的一个项目创建了一个方法,这是尝试使用它并查看它是否有效的方法:

   public void ResizeImage(string sImageFile, decimal dWidth, decimal dHeight, string sOutputFile)
    {
        Image oImg = Bitmap.FromFile(sImageFile);
        Bitmap oBMP = new Bitmap(decimal.ToInt16(dWidth), decimal.ToInt16(dHeight));

        Graphics g = Graphics.FromImage(oBMP);
        g.PageUnit = pgUnits;
        g.SmoothingMode = psMode;
        g.InterpolationMode = piMode;
        g.PixelOffsetMode = ppOffsetMode;

        g.DrawImage(oImg, 0, 0, decimal.ToInt16(dWidth), decimal.ToInt16(dHeight));

        ImageCodecInfo oEncoder = GetEncoder();
        EncoderParameters oENC = new EncoderParameters(1);

        oENC.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, plEncoderQuality);

        oImg.Dispose();

        oBMP.Save(sOutputFile, oEncoder, oENC);
        g.Dispose();

    }
于 2012-04-25T13:46:27.307 回答