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

后端 未结 5 780
一整个雨季
一整个雨季 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 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/

提交回复
热议问题