Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Make Thumbnail Image in C#
WhatsApp
Mukesh Kumar
Nov 23
2015
1.3
k
0
0
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#
Up Next
How to Make Thumbnail Image in C#