How to crop image in C#?

Jak
Jak
Member
858 Points
132 Posts

Hi,

 How to crop image in C#?

Views: 9196
Total Answered: 1
Total Marked As Answer: 1
Posted On: 18-Dec-2014 08:25

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

I have got:

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public byte[] CropImage(Image OriginalImage, int Width, int Height, int X, int Y)
try
{
 
Bitmap bmp = new Bitmap(Width, Height);
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
 
Graphics Graphic = Graphics.FromImage(bmp);
Graphic.SmoothingMode =SmoothingMode.AntiAlias;
Graphic.InterpolationMode =InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode =PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage,new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel);
 
MemoryStream ms = newMemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
 
return ms.GetBuffer();
}
 
catch (Exception Ex)
throw (Ex);
}
}
You can use as:
 
byte[] CroppedImage = null;
 
CroppedImage = CropImage(OriginalImage, imageWidth, imageHeight, x, y);
 
Where imageWidth=width of required image
imageHeight=hight of required image
x=x-coordinate
y=ycoordinate

 

Posted On: 18-Dec-2014 08:52
 Log In to Chat