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 Make Thumbnail Image in C#
Mukesh Kumar
Nov 23
2015
Code
1.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
public
static
Bitmap CreateThumbnail(
string
filename,
int
width,
int
height)
{
Bitmap bmpOut =
null
;
try
{
Bitmap loBMP =
new
Bitmap(filename);
ImageFormat loFormat = loBMP.RawFormat;
decimal
lnRatio;
int
lnNewWidth = 0;
int
lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if
(loBMP.Width < width && loBMP.Height < height)
return
loBMP;
if
(loBMP.Width > loBMP.Height)
{
lnRatio = (
decimal
)width / loBMP.Width;
lnNewWidth = width;
decimal
lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (
int
)lnTemp;
}
else
{
lnRatio = (
decimal
)height / loBMP.Height;
lnNewHeight = height;
decimal
lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (
int
)lnTemp;
}
bmpOut =
new
Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return
null
;
}
return
bmpOut;
}
C#