Download and resize image in Aspx

I have a page that allows the user to upload photos. The problem is that my old users cannot resize the image themselves. I want to allow them to upload any image size, and then when the server receives it, it will create a small copy of this image.

+4
source share
3 answers

There are so many approaches to resizing images, but I like this

System.Drawing.Image image = System.Drawing.Image.FromFile("FilePath"); int newwidthimg = 160; float AspectRatio = (float)image.Size.Width / (float)image.Size.Height; int newHeight = Convert.ToInt32(newwidthimg / AspectRatio); Bitmap thumbnailBitmap = new Bitmap(newwidthimg, newHeight); Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap); thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality; thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality; thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; var imageRectangle = new Rectangle(0, 0, newwidthimg, newHeight); thumbnailGraph.DrawImage(image, imageRectangle); thumbnailBitmap.Save("FilePath", ImageFormat.Jpeg); thumbnailGraph.Dispose(); thumbnailBitmap.Dispose(); image.Dispose(); 

I am fixing the width because I want all my images to have a width of 160 and a height according to the aspect ratio

+14
source

You can use some kind of resizeMethod .

 protected void Button1_Click(object sender, EventArgs e) { HttpPostedFile pf = FileUpload1.PostedFile; System.Drawing.Image bm = System.Drawing.Image.FromStream(pf.InputStream); bm = ResizeBitmap((Bitmap) bm, 100, 100); /// new width, height bm.Save(Path.Combine(YOURUPLOADPATH, pf.FileName); } private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight) { Bitmap result = new Bitmap(nWidth, nHeight); using (Graphics g = Graphics.FromImage((System.Drawing.Image)result)) g.DrawImage(b, 0, 0, nWidth, nHeight); return result; } 

On the other hand, uploading , for example, a 3 MB file and subsequently * resizing * in a 20 KB image does not sound very good to me. Perhaps you can limit the size of the download file.

+8
source

This is a fairly recent decision. Hope this helps.

http://forums.asp.net/t/1657138.aspx/1?C+Resize+Profile+Image

 protected void UploadButton_Click(object sender, EventArgs e) { if (File1.HasFile) { string fileName = Server.HtmlEncode(File1.FileName); string extension = System.IO.Path.GetExtension(fileName); System.Drawing.Image image_file = System.Drawing.Image.FromStream(File1.PostedFile.InputStream); int image_height = image_file.Height; int image_width = image_file.Width; int max_height = 100; int max_width = 100; image_height = (image_height * max_width) / image_width; image_width = max_width; if (image_height > max_height) { image_width = (image_width * max_height) / image_height; image_height = max_height; } Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height); System.IO.MemoryStream stream = new System.IO.MemoryStream(); bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Position = 0; byte[] data = new byte[stream.Length + 1]; stream.Read(data, 0, data.Length); Profile.Picture = data; Profile.PictureType = "image/png"; } } 
+1
source

Source: https://habr.com/ru/post/1343287/


All Articles