locked
Resize image RRS feed

  • Question

  • User-1988524439 posted

    I have an asp.net page that allows the user to do a FileUpload to select an image from their local drive.  I am using an asp.net Image control to display the image.  However, sometimes the images are pretty large and would like to both save and display the image in a roughly 200 x 200 image control.  How can I accomplish this and also specify the image height and width without losing quality?  Thanks.

    Friday, September 6, 2013 12:26 PM

Answers

All replies

  • User-658507263 posted

    See this post to upload a single image and save in different resolutions

    Upload multiple image in multiple size with progress bar in asp.net

     // Get thumb resolution
       
    Size ThumbNailSize100 = NewImageSize(Img.Height, Img.Width, 100, 100);
       
    using (System.Drawing.Image ImgThnail = new Bitmap(Img, ThumbNailSize100.Width, ThumbNailSize100.Height))
       
    {
           
    ImgThnail.Save(ThumbPath, Img.RawFormat);
       
    }

    For complete code see the link

    Friday, September 6, 2013 2:23 PM
  • User-1219482859 posted

    Hi,

    refer below articles. they might help you:

    http://www.codeproject.com/Tips/226192/Resizing-images-when-uploaded-in-asp-net

    http://www.codeproject.com/Articles/4097/Dynamically-resize-uploaded-images-save-in-PNG-for

    you can also use below code which i have used in my application to display images:

    byteArray = (byte[])s.FileByteArray;
                   						
                     mstream = new System.IO.MemoryStream(byteArray, 0, byteArray.Length);
                    try
                    {
                        System.IO.MemoryStream ImgStream2 = new System.IO.MemoryStream(byteArray);
                        ImgStream2.Position = 0;
                        System.Drawing.Image dbImage = System.Drawing.Image.FromStream(ImgStream2);
                        int height = (int)(pct * dbImage.Size.Height);
                        int width = (int)(pct * dbImage.Size.Width);
                        if (width > 2948)
                        {
                            width = 2948;
                        }
                        if (height > 4446)
                        {
                            height = 4446;
                        }
                        //height=2001;
    
                        System.Drawing.Image ImgCopy = dbImage.Clone() as System.Drawing.Image;
                
                        Bitmap thumbnailImage = (Bitmap)ImgCopy.GetThumbnailImage(width, height, null, IntPtr.Zero);
                        
                        System.IO.MemoryStream thumbStream = new System.IO.MemoryStream();
                        thumbnailImage.Save(thumbStream, ImageFormat.Gif);
    
                        Byte[] thumbnailByteArray = new Byte[thumbStream.Length];
                        thumbStream.Position = 0;
                        thumbStream.Read(thumbnailByteArray, 0, Convert.ToInt32(thumbStream.Length));
    					
                        thumbStream.Close();
                      
                        Response.BinaryWrite(thumbnailByteArray);
                    }
                    catch (Exception e1)
                    {
                        //throw new Exception(e1.ToString());						
                        lblError.Text = e1.Message;
                    }



    Friday, September 6, 2013 2:27 PM
  • User-1988524439 posted

    When I try either solution I get the error below when trying to save the image file.  The process works fine when I just save the uploaded file without resize.

    A generic error occurred in GDI+.

    Friday, September 6, 2013 3:55 PM
  • User-1961616419 posted

    I suggest to read and try the article :How to resize image in Asp.net?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 7, 2013 3:44 AM
  • User-1988524439 posted

    Thanks, it worked perfect!  I even placed the routine in a shared subroutine to use elsewhere in my app.

    Saturday, September 7, 2013 8:22 AM