c#中的裁剪图像

时间:2012-04-25 13:41:04

标签: c# winforms screenshot crop

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

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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

    }