Proportional Image Resize in C# .net :
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
Bitmap bmpOut = null;
try
{
var loBmp = new Bitmap(lcFilename);
var lnNewWidth = 0;
var lnNewHeight = 0;
if (loBmp.Height == loBmp.Width)
{
lnNewWidth = lnNewHeight = lnHeight;
}
else if (loBmp.Height > loBmp.Width)
{
lnNewHeight = lnHeight;
lnNewWidth = (loBmp.Width * lnWidth) / loBmp.Height;
}
else
{
lnNewHeight = lnWidth;
lnNewWidth = (loBmp.Width * lnHeight) / loBmp.Height;
lnNewWidth = lnWidth;
lnNewHeight = (loBmp.Height * lnHeight) / loBmp.Width;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
var g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.Transparent, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBmp, 0, 0, lnNewWidth, lnNewHeight);
loBmp.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return bmpOut;
}
No comments:
Post a Comment