本文介绍了更改图像分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想将一个上传图片的分辨率更改为小尺寸。如何使用c#asp.net if (fileUploadImage.HasFile) { int fileSize = fileUploadImage.PostedFile。的ContentLength; if (fileSize > Convert.ToInt32(ConfigurationManager.AppSettings [ fileSize])) { lblUploadImage.Visible = 真; } else { 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); 位图newBmp = 新位图( 50 , 50 ,System.Drawing.Imaging.PixelFormat.Format24bppRgb); newBmp.SetResolution( 72 , 72 ); Graphics newGraphic = Graphics.FromImage(newBmp); 尝试 { newGraphic.Clear(Color。白色); newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; newGraphic.DrawImage(upBmp, 500 , 100 , 500 , 100 ); newBmp.Save(MapPath(fileNameUrl)); // 在图像控件中显示已上传的已调整大小的图片 imgProfile.Src = fileNameUrl; lblUploadImage.Visible = true ; } catch (Exception exp) {} // fileUploadImage.SaveAs(Server.MapPath(fileNameUrl)); // HiddenField1.Value = fileNameUrl; // imgProfile.Src = fileNameUrl; // lblUploadImage.Visible = false; } 解决方案 您可以查看SetResolution-Method: http://msdn.microsoft.com/de-de/library/ system.drawing.bitmap.setresolution.aspx [ ^ ] 但是,谢尔盖提到你可能想要做的是图像的下采样。 你好,你的代码中有一点错误,但它是如此有用。 你必须保存upBmp对象而不是newBmp对象。 非常感谢!! i want to change the resolution of one uploaded image to small size .how to do this usng c# asp.netif (fileUploadImage.HasFile) { int fileSize = fileUploadImage.PostedFile.ContentLength; if (fileSize > Convert.ToInt32(ConfigurationManager.AppSettings["fileSize"])) { lblUploadImage.Visible = true; } else { 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);try {newGraphic.Clear(Color.White);newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;newGraphic.DrawImage(upBmp, 500, 100, 500, 100);newBmp.Save(MapPath(fileNameUrl));//Show the uploaded resized picture in the image controlimgProfile.Src = fileNameUrl;lblUploadImage.Visible = true; } catch(Exception exp) { } // fileUploadImage.SaveAs(Server.MapPath(fileNameUrl)); // HiddenField1.Value = fileNameUrl; // imgProfile.Src = fileNameUrl; // lblUploadImage.Visible = false; } 解决方案 You can have a look at the SetResolution-Method:http://msdn.microsoft.com/de-de/library/system.drawing.bitmap.setresolution.aspx[^]But as Sergey mentioned what you probably want to do is a Downsampling of the image.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!! 这篇关于更改图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-17 08:28