0

I Have An Image And I just Need To Crop That

  • For Example My Image's Pixel Is (257,50)
  • I Just Need (200,20) Of That Pic I Don't Need The Rest Of That Pic How Can I Do That?
  • List item
4

1 Answer 1

1

You can try this function from How to crop/resize image

private Bitmap CropImage(Image originalImage, Rectangle sourceRectangle,
                    Rectangle? destinationRectangle = null)
    {
        if (destinationRectangle == null)
        {
            destinationRectangle = new Rectangle(Point.Empty, sourceRectangle.Size);
        }

        var croppedImage = new Bitmap(destinationRectangle.Value.Width,
            destinationRectangle.Value.Height);
        using (var graphics = Graphics.FromImage(croppedImage))
        {
            graphics.DrawImage(originalImage, destinationRectangle.Value,
                sourceRectangle, GraphicsUnit.Pixel);
        }
        return croppedImage;
    }

    /// <summary>
    /// Button click to choose an image and test
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnCrop_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            string imageFile = ofd.FileName;

            Image img = new Bitmap(imageFile);
            Rectangle source = new Rectangle(0, 0, 120, 20);
            Image cropped = CropImage(img, source);
            // Save cropped image here
            cropped.Save(Path.GetDirectoryName(imageFile) + "\\croppped." + Path.GetExtension(imageFile));
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.