How to crop image in C#?

Jak
Jak
Member
352 Points
151 Posts

Hi,

 How to crop image in C#?

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

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1170 Points
210 Posts
Marked as answer      

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
banner

Blog

Active User (2)

 Log In to Chat