繁体   English   中英

c#中的裁剪图像

[英]Crop image in c#

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

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

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

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

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

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

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

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

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM