C#GDI +图像resizefunction

所以我的逻辑存在缺陷,我需要一种更好,更正确的方法来调整c#app中的图像大小

我需要一个类似于这个设置的function

public void ResizeImageForWeb(string OriginalFile, string NewFile, int MaxWidth, int MaxHeight, int Quality) { // Resize Code } 

基本上,我是一个网页设计师,试图编写桌面应用程序。

谷歌的快速搜索找到了这个小片段 。

这是我用来调整用户上传的图像的代码,用于创建缩略图或仅强制执行大小限制。 它没有解决图像质量,但它是一个开始。

 // uses System.Drawing namespace public class ImageResizer { public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth) { return this.ResizeImage(fullFileName, maxHeight, maxWidth, fullFileName); } public bool ResizeImage(string fullFileName, int maxHeight, int maxWidth, string newFileName) { using (Image originalImage = Image.FromFile(fullFileName)) { int height = originalImage.Height; int width = originalImage.Width; int newHeight = maxHeight; int newWidth = maxWidth; if (height > maxHeight || width > maxWidth) { if (height > maxHeight) { newHeight = maxHeight; float temp = ((float)width / (float)height) * (float)maxHeight; newWidth = Convert.ToInt32(temp); height = newHeight; width = newWidth; } if (width > maxWidth) { newWidth = maxWidth; float temp = ((float)height / (float)width) * (float)maxWidth; newHeight = Convert.ToInt32(temp); } Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback); using (Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero)) { resizedImage.Save(newFileName); } return true; } else if (fullFileName != newFileName) { // no resizing necessary, but need to create new file originalImage.Save(newFileName); } } return false; } private bool ThumbnailCallback() { return false; } } 

我肯定不会使用GetThumbnailImage因为它会令人震惊 – 如果不使用DX或OpenL等获得良好的分辨率,我会使用类似下面的内容(来自我在许多Windows应用程序中使用的图形库 – 我已经分享了一些之前的时间可能会有变化浮在网上)。 这里有3种方法 – GetNonIndexedPixelFormat方法用于在传递无法处理的像素格式时停止GDI崩溃(注释解释它)。 第一个允许缩放因子(缩放),最后一个允许固定大小重新缩放,同时保持纵横比(但如果你想改变它,可以很容易地修改)。 请享用:

  ///  /// Scale Image By A Percentage - Scale Factor between 0 and 1. ///  /// Image: Image to scale /// Float: Sclae Value - 0 to 1 are the usual values /// Image: Scaled Image public static Image ScaleByPercent(Image originalImg, float ZoomFactor) { int destWidth = (int)((float)originalImg.Width * ZoomFactor); int destHeight = (int)((float)originalImg.Height * ZoomFactor); Bitmap bmPhoto = new Bitmap(destWidth, destHeight, GetNonIndexedPixelFormat(originalImg)); // PixelFormat.Format24bppRgb); bmPhoto.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(originalImg, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, originalImg.Width, originalImg.Height), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; } ///  /// Gets the closest non-indexed pixel format ///  /// Image: Original image /// PixelFormat: Closest non-pixel image format public static PixelFormat GetNonIndexedPixelFormat(Image originalImage) { /* * These formats cause an error when creating a GDI Graphics Oblect, so must be converted to non Indexed * Error is "A graphics object cannot be created from an image that has an indexed pixel format" * PixelFormat.Undefined PixelFormat.DontCare PixelFormat.1bppIndexed PixelFormat.4bppIndexed PixelFormat.8bppIndexed PixelFormat.16bppGrayScale PixelFormat.16bppARGB1555 * * An attempt is made to use the closest (ie smallest fitting) format that will hold the palette. */ switch (originalImage.PixelFormat) { case PixelFormat.Undefined: //This is also the same Enumation as PixelFormat.DontCare: return PixelFormat.Format24bppRgb; case PixelFormat.Format1bppIndexed: return PixelFormat.Format16bppRgb555; case PixelFormat.Format4bppIndexed: return PixelFormat.Format16bppRgb555; case PixelFormat.Format8bppIndexed: return PixelFormat.Format16bppRgb555; case PixelFormat.Format16bppGrayScale: return PixelFormat.Format16bppArgb1555; case PixelFormat.Format32bppArgb: return PixelFormat.Format24bppRgb; default: return originalImage.PixelFormat; } } ///  /// Resize image keeping aspect ratio. ///  /// Image: Image to scale /// Int: Required width in pixels /// Int: Required height in pixels /// Color: Background colour /// Image: Scaled Image public static Image Resize(Image originalImg, int Width, int Height, Color BackgroundColour) { int destX = 0; int destY = 0; float nPercent = 0f; float nPercentW = ((float)Width / (float)originalImg.Width); float nPercentH = ((float)Height / (float)originalImg.Height); if (nPercentH < nPercentW) { nPercent = nPercentH; destX = System.Convert.ToInt16(((float)Width - ((float)originalImg.Width * nPercent)) / 2f); } else { nPercent = nPercentW; destY = System.Convert.ToInt16(((float)Height - ((float)originalImg.Height * nPercent)) / 2f); } int destWidth = (int)(originalImg.Width * nPercent); int destHeight = (int)(originalImg.Height * nPercent); Bitmap bmPhoto = new Bitmap(Width, Height, GetNonIndexedPixelFormat(originalImg)); // PixelFormat.Format24bppRgb); bmPhoto.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.Clear(BackgroundColour); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(originalImg, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, originalImg.Width, originalImg.Height), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; } 

使用Graphics.DrawImage()。 GetThumbnailImage()将从jpeg文件返回一个120×120(或更小)的嵌入式缩略图。 对于那么大的东西,这将是可怕的。

有关要使用的相应设置,请参见http://nathanaeljones.com/163/20-image-resizing-pitfalls/ 。