C# Thumbnail Generator
Earlier in this blog I posted a VB thumbnail generator. The following is the same function but converted to c#. It converts an image on the file system to a thumbnail using the X, Y values sent to the function along with its save path.
public static void GenerateThumbnail(string sImagePath, string sSavePath, string sSaveName, int iX, int iY)
{
using (System.Drawing.Image imgFull = System.Drawing.Image.FromFile(sImagePath))
{
System.Drawing.Imaging.ImageFormat ifFormat = imgFull.RawFormat;
decimal dOrigWidth = imgFull.Width;
decimal dOrigHeight = imgFull.Height;
int iNewX;
int iNewY;
decimal dRatio;
dRatio = iX / dOrigWidth;
iNewX = Convert.ToInt32(dOrigWidth * dRatio);
iNewY = Convert.ToInt32(dOrigHeight * dRatio);
using (System.Drawing.Bitmap imgOutput = new System.Drawing.Bitmap(imgFull, iNewX, iNewY))
{
using (System.Drawing.Graphics gfxResizer = System.Drawing.Graphics.FromImage(imgOutput))
{
gfxResizer.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gfxResizer.DrawImage(imgFull, 0, 0, iNewX, iNewY);
imgOutput.Save(sSavePath + "/" + sSaveName, ifFormat);
}
}
}
}
I hope this is helpful, if you need the VB.Net conversion then check out the original post here.