I Have An Image And I just Need To Crop That
-
4stackoverflow.com/questions/734930/how-to-crop-an-image-using-cPeter Dennis Bartok– Peter Dennis Bartok2016-12-31 06:14:59 +00:00Commented Dec 31, 2016 at 6:14
-
Do you want to crop it or just show one part of it?Talha Talip Açıkgöz– Talha Talip Açıkgöz2016-12-31 07:45:58 +00:00Commented Dec 31, 2016 at 7:45
-
@TalhaTalipAçıkgöz yesamir– amir2016-12-31 08:27:47 +00:00Commented Dec 31, 2016 at 8:27
-
@TalhaTalipAçıkgöz yes and save it as new picamir– amir2016-12-31 08:28:47 +00:00Commented Dec 31, 2016 at 8:28
Add a comment
|
1 Answer
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));
}
}
