TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Crop an Image in C#
Mukesh Kumar
Oct 16
2015
Code
1.7
k
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
static
byte
[] Crop(
string
Img,
int
Width,
int
Height,
int
X,
int
Y)
{
try
{
using
(SD.Image OriginalImage = SD.Image.FromFile(Img))
{
using
(SD.Bitmap bmp =
new
SD.Bitmap(Width, Height))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using
(SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage,
new
SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
MemoryStream ms =
new
MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return
ms.GetBuffer();
}
}
}
}
catch
(Exception Ex)
{
throw
(Ex);
}
}
C#
crop image