Crop image in c#

2019-02-23 16:05发布

问题:

I have a image that I want to crop it when I press a button on the form. I have the following code that is run when the button is pressed, but it doesn't do anything to the image:

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

My image is actually a screenshot of the active window of the form with the following code:

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

To finish this, at the press of the same button, first I want to take the screenshot of the form, then crop that image, but cropping doesn't work. Why is that?

回答1:

Your code is close to what I have been using for saving cropped images. You're missing the part where you save the cropped image. You need to write the cropped image to a byte stream then save it to disk. I modified your code, it's untested but give it a try.

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


回答2:

You can use the Bitmap class' Clone method (http://msdn.microsoft.com/en-us/library/ms141944.aspx) to get a subrectangle of a target Bitmap.



回答3:

You will have the result in variable bmp. You can continue working on that. If you dispose of that object, your changes will, of course, be lost.



回答4:

I created a method for one of my projects, here is the method try using it and see if it works:

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

    }