This example shows how to draw a cropped image from a file. In this instance we're going to discard an eighty pixel border round the edge of the image.
1
Creating the Bitmap

We will need a bitmap so we create it here, loading the base image from file.

[C#]

using (Image image = Bitmap.FromFile(Server.MapPath("rez/josh-hild-_TuI8tZHlk4-unsplash.jpg"))) {


2
Creating the image

We put the horizontal and vertical borders into a Size structure. To find the size of the output bitmap we subtract twice the borders from the image size.

[C#]

  Size delta = new Size(80, 80);
  Size size = image.Size - (delta * 2);


3
Drawing the image

We then create a new bitmaps of the correct size, copying the resolution from the original image. Then we draw the image, offset up and left by the border size.

[C#]

  using (Bitmap bitmap = new Bitmap(size)) {
    bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    using (Graphics graphics = Graphics.FromImage(bitmap))
      graphics.DrawImageUnscaled(image, -(Point)delta);


4
Saving

Finally we save the picture as a JPEG image.

[C#]

    bitmap.Save(Server.MapPath("Cropping_an_Image_4.jpg"));
  }
}


5
Input and Output

Sample input and output images are shown below.


josh-hild-_TuI8tZHlk4-unsplash.jpg


Cropping_an_Image_4.jpg