Click here to Skip to main content
10,500,000 members (63,519 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#3.0
i want to change the resolution of one uploaded image to small size .how to do this usng c#

if (fileUploadImage.HasFile)
 int fileSize = fileUploadImage.PostedFile.ContentLength;
 if (fileSize > Convert.ToInt32(ConfigurationManager.AppSettings["fileSize"]))
 lblUploadImage.Visible = true;
 String fileName = fileUploadImage.FileName.ToString();//Substring (0,fileUploadImage.FileName.IndexOf("."));
 string fileNameUrl = ConfigurationManager.AppSettings["imagepath"] + GenerateUniqueFileName(txtFname.Text, Path.GetExtension(fileName));
 Bitmap upBmp = (Bitmap)Bitmap.FromStream(fileUploadImage.PostedFile.InputStream);
Bitmap newBmp = new Bitmap(50,50, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
 Graphics newGraphic = Graphics.FromImage(newBmp);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upBmp, 500, 100, 500, 100);
//Show the uploaded resized picture in the image control
imgProfile.Src = fileNameUrl;
lblUploadImage.Visible = true;
 catch(Exception exp)
 // fileUploadImage.SaveAs(Server.MapPath(fileNameUrl));
 // HiddenField1.Value = fileNameUrl;
 // imgProfile.Src = fileNameUrl;
 // lblUploadImage.Visible = false;
Posted 18-Sep-12 22:34pm
Edited 18-Sep-12 22:54pm
Legor at 19-Sep-12 3:49am
To less information, Please elaborate.
Sergey Alexandrovich Kryukov at 19-Sep-12 3:56am
I'm pretty much sure you don't need to change image resolution; it has little use. Most likely, you need to re-sample the image. Up or down? (Any considerable up-scale means louse quality...)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can have a look at the SetResolution-Method:[^]

But as Sergey mentioned what you probably want to do is a Downsampling of the image.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hello, you have a little mistake in your code, but it's so helpfully.

you must save the upBmp object and not the newBmp object.

Thank you so much!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 180
2 Raje_ 155
3 OriginalGriff 147
4 Mehdi Gholam 120
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,199
3 Maciej Los 2,681
4 Richard Deeming 2,370

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 13 Oct 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100