Resizing image proportionally in ASP.NET C# by specifying either Height or Width

后端 未结 5 782
一整个雨季
一整个雨季 2021-02-11 07:33

I need a code that will allow me to resize images, but with the following functionality:

1) resize image upon upload

2) Resize image proportionally by specifying

相关标签:
5条回答
  • 2021-02-11 07:56

    Although it seems like you should be able to copy and paste a snippet to do this, there are a ton of pitfalls you need to look out for if you're building your own image resizing system. It's better to use a proven, tested, and supported open-source library.

    To resize to a file directly from HttpPostedFile, call

    ImageBuilder.Current.Build(httpPostedFile, "img.jpg", new ResizeSettings("width=200&quality=90"));
    

    To resize an existing file, call

    ImageBuilder.Current.Build("orig.jpg", "img.jpg", new ResizeSettings("width=200&quality=90"));
    

    The ImageResizing.Net library is free, and MIT-licensed (no worries about licensing problems).

    0 讨论(0)
  • 2021-02-11 08:03

    This is how is did in my project

    On Button click while uploading file:

    System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream);
         System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81);
         objImage.Save(SaveLocation,ImageFormat.Png);
         lblmsg.Text = "The file has been uploaded.";
    

    public static System.Drawing.Image ScaleImage(System.Drawing.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;
            }
    

    More Detail Click here

    https://codepedia.info/how-to-resize-image-while-uploading-in-asp-net-using-c/

    0 讨论(0)
  • 2021-02-11 08:06

    Last day I found imageresizer and its great. and good API. Works Great. Downloaded from Visual studio 2010 Extension Manager: http://nuget.org/.

    Easy Steps to download API in VS-2010:

    1). Install Extension http://nuget.org/.

    enter image description here

    3). Find and Install ImageResizing
    enter image description here

    4).Then Code: (I m using here cropping. you can use any) Documentation on imageresizing.net

    string uploadFolder = Server.MapPath(Request.ApplicationPath + "images/");
    FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
    
    
    //The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
    ResizeSettings resizeCropSettings = new ResizeSettings("width=200&height=200&format=jpg&crop=auto");
    
    //Generate a filename (GUIDs are safest).
    string fileName = Path.Combine(uploadFolder, System.Guid.NewGuid().ToString());
    
    //Let the image builder add the correct extension based on the output file type (which may differ).
    fileName = ImageBuilder.Current.Build(uploadFolder + FileUpload1.FileName, fileName, resizeCropSettings, false, true);

    Try!!! its very awsumm and easy to use. thanks.

    0 讨论(0)
  • 2021-02-11 08:11

    Taken from this Stackoverflow answer, I come up with:

    public static Image Resize(this Image image, int maxWidth = 0, int maxHeight = 0)
        {
            if (maxWidth == 0)
                maxWidth = image.Width;
            if (maxHeight == 0)
                maxHeight = image.Height;
    
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);
    
            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);
    
            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }
    

    To resize Image specifying its maxWidth:

    var _image = Image.FromStream(Source);
    var _thumbImage = _image.Resize(100);
    

    To resize Image specifying its maxHeight:

    var _image = Image.FromStream(Source);
    var _thumbImage = _image.Resize(maxHeight: 100);
    
    0 讨论(0)
  • Code I use for image resizing: http://sietch.net/ViewNewsItem.aspx?NewsItemID=105

    0 讨论(0)
提交回复
热议问题