locked
Resizing image before saving to server RRS feed

  • Question

  • User-1246604461 posted
    I use a fileupload control.
    To save an image to the server I use:

    MyFileUpload.SaveAs(Server.MapPath("Data\Customers") & "\" &Session("Code").ToString & "\" & FileName)
    That does work, wasnt it for the fact that I still need to perform the following operations:
    But the image the customers uploads can be 1024x768. However, I want to resize it for him to the max dimensions allowed (600x500).
    For that I have first called my method which resizes it proportionally. Then I call the routine:

    img.GetThumbnailImage(ThumbWidth, ThumbHeight, dummyCallBack, IntPtr.Zero)
    So the 1024*768 file is already saved to the server, but I havent been able to resize it to the max dimensions allowed...
    How can I perform those resize operations BEFORE I save the file to the server?
    Tuesday, August 16, 2005 2:23 PM

All replies

  • User672738083 posted
    something like:

                System.Drawing.Image source = System.Drawing.Image.FromStream(fileUpload.PostedFile.InputStream);
                Bitmap bm = new Bitmap(100, 100, source.PixelFormat);
                Graphics g = Graphics.FromImage(bm);
                g.DrawImage(source, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0 , source.Width, source.Height), GraphicsUnit.Pixel);
                source.Dispose();
                bm.Save(fileName, GraphicsFormat.Jpeg);
                g.Dispose();
                bm.Dispose();

           

    Tuesday, August 16, 2005 3:33 PM
  • User-1246604461 posted
    alrite thanks, but could you please briefly explain what each line exactly does? ( Im a rookie ;) )
    Tuesday, August 16, 2005 4:13 PM
  • User672738083 posted
    something like:

                System.Drawing.Image source = System.Drawing.Image.FromStream(fileUpload.PostedFile.InputStream);

                // Opens the image from the stream

                Bitmap bm = new Bitmap(100, 100, source.PixelFormat);
               
    // Creates a new image in memory that is 100x100, using the same pixel format as the uploaded
                Graphics g = Graphics.FromImage(bm);
               
    // gets the graphics object from the image to redraw/resize on
                g.DrawImage(source, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0 , source.Width, source.Height), GraphicsUnit.Pixel);

                // redraw the source image to the destination in memory one resized to 100x100

                source.Dispose();

                // clean up memory

                bm.Save(fileName, GraphicsFormat.Jpeg);

                // save the file to your filename, use whatever graphics format is appropriate

                g.Dispose();
               
    // clean up memory
                bm.Dispose();
               
    // clean up memory

           



    there are other ways, i am not sure how good this is at keeping image quality, i have a much more complicated method in other apps, this was something i am playing with at the moment. it works though
    Tuesday, August 16, 2005 4:49 PM