locked
resize image RRS feed

  • Question

  • User-1357109602 posted

    I have created upload image, save it, resize it and save with different name. But in that case I have always 2 images - normal and small. I want only 1 image - small - resize on the fly. So what should I write inside System.Drawing.Image.FromFile() - previous I have there path to saved image with normal size but now I don't have saved image with normal size so what should be there ?

                    HttpPostedFileBase file = Request.Files["file"];
    
                    System.Drawing.Image picture = System.Drawing.Image.FromFile( ?????????? ); //what should be here ?
    
                    //resize:
                    double width = picture.Width;
                    double height = picture.Height;
    
                    double newWidth = picture.Width;
                    double newHeight = picture.Height;
                    double factor;
                    if (width > maxWidthHeight || height > maxWidthHeight)
                    {
                        if (width > height)
                        {
                            factor = maxWidthHeight / width;
                        }
                        else
                        {
                            factor = maxWidthHeight / height;
                        }
    
                        newWidth = Math.Round(width * factor, MidpointRounding.AwayFromZero);
                        newHeight = Math.Round(height * factor, MidpointRounding.AwayFromZero);
                    }
    
                    Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
                    Graphics g = Graphics.FromImage(result);
                    g.DrawImage(picture, 0, 0, (int)newWidth, (int)newHeight);
                    result.Save(katalog + nowa_nazwa_pliku);
    
    
    
    
    


                
    Saturday, February 27, 2010 1:23 PM

Answers

  • User1211441112 posted

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 12:35 AM

All replies

  • User2130758966 posted

    Hey,

    You can create a httphandler to resize stored images on the fly.

    Rick Strahl has some nice resize code that I have used recently in a project:

    His code is good but for some reason he uses an actual aspx page rather than a asmx httphandler. Its easily transferred over though...

    Saturday, February 27, 2010 2:01 PM
  • User-1357109602 posted

    I only want to know what I should write inside System.Drawing.Image.FromFile() ?

    In PHP uploaded files before save them have temporary localization which we can check using: $_FILES['file']['tmp_name'] - I need something like that in ASP.NET - I think I should write inside System.Drawing.Image.FromFile() path to temporary file.

    Saturday, February 27, 2010 2:44 PM
  • User2130758966 posted

    Oh ok sorry.

    I dont think there is a tmp location available but you can get the contents uploaded before you write it to file using the .FileBytes property:


    I dont think the FromFile() will work with that but is there one that takes a stream of some kind?

    Saturday, February 27, 2010 4:00 PM
  • User-1357109602 posted

    "is there one that takes a stream of some kind" - I don't understand what You mean. So I should use System.Drawing.Image.FromStream() - not FromFile but FromStream ? I don't know...


    Saturday, February 27, 2010 4:26 PM
  • User-1357109602 posted

    I know:

                    int imageLength = file.ContentLength; 
                    byte[] binaryImagefile = new byte[imageLength];
                    file.InputStream.Read(binaryImagefile, 0, imageLength);                
                    System.Drawing.Image picture = System.Drawing.Image.FromStream(new System.IO.MemoryStream(binaryImagefile));


    Saturday, February 27, 2010 5:38 PM
  • User1211441112 posted

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 12:35 AM
  • User2002545590 posted

    very helpful, thank you

    giorgi

    Friday, April 16, 2010 6:15 PM
  • User1560559919 posted

    hii...vipin ...thnks bt im gettin this two type of errors....

     

    Error 14 The best overloaded method match for 'System.Drawing.Graphics.FromImage(System.Drawing.Image)' has some invalid arguments 

    Error 13 The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

     

    plz help me out.....

    Tuesday, July 20, 2010 5:22 AM
  • User2002545590 posted

    hi, could you post your code, please..

    thanks

    Friday, July 23, 2010 4:13 AM
  • User1560559919 posted

    hi....

    thnks 4 rply bt i hav already solvd tat issue...

    bt thanks... Smile

    Friday, July 30, 2010 8:33 AM
  • User-22305662 posted

    I am trying to create thumbnail images on the fly when a user posts an article.  You know, Title, short description, outside small picture and content.  It works perfect except for one thing:  the thumbnail issue.  Although it creates a thumbnail with new dimentions and new size, however, the size is still high.  Example: taking a picture 800x600 and resize it to 150x113 instead of the size going down from 500kb to 6 or 7kb, it gives me a size of 41 - 43 kb.  It is high when you have so many articles listed.  Even when using your code, what am I doing wrong?

     

    AWNY

     

    Friday, February 4, 2011 1:44 PM
  • User637182499 posted

    Interesting link! Thanks for sharing :)

    Sunday, February 6, 2011 12:40 AM
  • User1410450975 posted
    you can also use this 
    public static Image ScaleImage(Image image, int maxHeight) 
    {
       
    var ratio = (double)maxHeight / image.Height;

       
    var newWidth = (int)(image.Width * ratio);
       
    var newHeight = (int)(image.Height * ratio);

       
    var newImage = new Bitmap(newWidth, newHeight);
        using
    (var g = Graphics.FromImage(newImage))
       
    {
            g
    .DrawImage(image, 0, 0, newWidth, newHeight);
       
    }
       
    return newImage;
    }
    Thursday, August 2, 2012 12:46 PM