如何调整图像大小并保存在文件夹中?

我试过这个:

string str = System.IO.Path.GetFileName(txtImage.Text); string pth = System.IO.Directory.GetCurrentDirectory() + "\\Subject"; string fullpath = pth + "\\" + str; Image NewImage = clsImage.ResizeImage(fullpath, 130, 140, true); NewImage.Save(fullpath, ImageFormat.Jpeg); 

 public static Image ResizeImage(string file, int width, int height, bool onlyResizeIfWider) { if (File.Exists(file) == false) return null; try { using (Image image = Image.FromFile(file)) { // Prevent using images internal thumbnail image.RotateFlip(RotateFlipType.Rotate180FlipNone); image.RotateFlip(RotateFlipType.Rotate180FlipNone); if (onlyResizeIfWider == true) { if (image.Width  height) { // Resize with height instead width = image.Width * height / image.Height; newHeight = height; } Image NewImage = image.GetThumbnailImage(width, newHeight, null, IntPtr.Zero); return NewImage; } } catch (Exception ) { return null; } } 

运行上面的代码,我得到的图像大小为4-5 KB,图像质量非常差。 原始图像文件不超过1.5 MB。 如何提高结果的图像质量?

我认为你应该使用Image Resizer ,它的免费和调整图像大小很容易使用它。

 var settings = new ResizeSettings { MaxWidth = thumbnailSize, MaxHeight = thumbnailSize, Format = "jpg" }; settings.Add("quality", quality.ToString()); ImageBuilder.Current.Build(inStream, outStream, settings); resized = outStream.ToArray(); 

您也可以使用Nuget包管理器安装它。

 PM> Install-Package ImageResizer