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.

 

Posted: Dec 02 2008, 18:54 by Adrian | Comments (702) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.Net 2.0 | C# | VB.Net

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading