skip to main | skip to sidebar
Prabir Kumar Basak's Blog
RSS

Resize Image at runtime

Image Resizing 0 comments

Hey Friends,

here is the code to generate resize image at the time of uploading. Hope It will help.

 public string ResizeBookImage(FileUpload ftpControl, int intWidth, int intHeight, string UploadDirectory, string strTempDirectory)
    {
        string strFileName = "";
        if (ftpControl.HasFile)
        {
            imgResizedName = Guid.NewGuid();
            string ext = Path.GetExtension(ftpControl.FileName);
            strFileName = imgResizedName.ToString();
            string strFName = ftpControl.FileName;
            ftpControl.SaveAs(strTempDirectory + ftpControl.FileName);
            string strFCName = strTempDirectory + strFName;

            System.Drawing.Image image = System.Drawing.Image.FromFile(strFCName);
            int srcWidth = image.Width;
            int srcHeight = image.Height;
            //int thumbHeight = (srcHeight / srcWidth) * thumbWidth;
            Bitmap bmp = new Bitmap(intWidth, intHeight);
            string dest = UploadDirectory + strFileName + ".jpg";
            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, intWidth, intHeight);
            gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

            bmp.Save(dest, System.Drawing.Imaging.ImageFormat.Jpeg);

            bmp.Dispose();
            image.Dispose();

            File.Delete(strTempDirectory + strFName);
        }
        return strFileName;
    }


7:19 PM



0 responses to "Resize Image at runtime"


Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

    Blog Archive

    • ▼  2010 (6)
      • ►  July (1)
      • ►  March (1)
      • ▼  February (3)
        • Generate Unique Id using DateTime
        • Resize Image at runtime
        • Search Procedure using ':' as seperator
      • ►  January (1)

    About Me

    My photo
    Prabir
    Web based application developer, working with Microsoft.Net technology.
    View my complete profile

    Search This Blog

    Labels

    • Blog Archive (1)
    • Image Resizing (1)
    • Search (1)
    • Stored Procedure (3)
    • XML (1)

    Followers

Copyright © All Rights Reserved. Prabir Kumar Basak's Blog | Converted into Blogger Templates by Theme Craft