c# - 在不丢失图像质量的ASP.NET 中,C# 调整图像的大小

  显示原文与译文双语对照的内容
98 4

我正在开发一个 ASP.NET 3.5 web应用程序,我允许用户上传 jpeg 。gif 。bmp或者png图像。上传图像尺寸大于 103,我想把上传的图像调整到 103 。我已经经读过一些博客文章和文章,也尝试了一些代码样本,但看起来没有什么好处。有谁成功了?

时间:原作者:0个回答

84 1

我也有同样的问题,并以这种方式处理:

private Image RezizeImage(Image img, int maxWidth, int maxHeight)
{
 if(img.Height <maxHeight && img.Width <maxWidth) return img;
 using (img)
 {
 Double xRatio = (double)img.Width/maxWidth;
 Double yRatio = (double)img.Height/maxHeight;
 Double ratio = Math.Max(xRatio, yRatio);
 int nnx = (int)Math.Floor(img.Width/ratio);
 int nny = (int)Math.Floor(img.Height/ratio);
 Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
 using (Graphics gr = Graphics.FromImage(cpy))
 {
 gr.Clear(Color.Transparent);
//This is said to give best quality when resizing images
 gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
 gr.DrawImage(img,
 new Rectangle(0, 0, nnx, nny),
 new Rectangle(0, 0, img.Width, img.Height),
 GraphicsUnit.Pixel);
 }
 return cpy;
 }
}
private MemoryStream BytearrayToStream(byte[] arr)
{
 return new MemoryStream(arr, 0, arr.Length);
}
private void HandleImageUpload(byte[] binaryImage)
{
 Image img = RezizeImage(Image.FromStream(BytearrayToStream(binaryImage)), 103, 32);
 img.Save("IMAGELOCATION.png", System.Drawing.Imaging.ImageFormat.Gif);
}

我只是读到这是获得最高质量的方法。

原作者:
88 3

这是我使用的代码。它支持旋转,并将图像分辨率设置为 72 dpi@24-bit色( 默认情况下,GDI+ 以 96 dpi@32-bit颜色保存图像)的JPEG标准。它还修正了一些人在调整图像大小时遇到的黑色/灰色边界问题。

///<summary>
///Resizes and rotates an image, keeping the original aspect ratio. Does not dispose the original
///Image instance.
///</summary>
///<param name="image">Image instance</param>
///<param name="width">desired width</param>
///<param name="height">desired height</param>
///<param name="rotateFlipType">desired RotateFlipType</param>
///<returns>new resized/rotated Image instance</returns>
public static Image Resize(Image image, int width, int height, RotateFlipType rotateFlipType)
{
//clone the Image instance, since we don't want to resize the original Image instance
 var rotatedImage = image.Clone() as Image;
 rotatedImage.RotateFlip(rotateFlipType);
 var newSize = CalculateResizedDimensions(rotatedImage, width, height);
 var resizedImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppArgb);
 resizedImage.SetResolution(72, 72);
 using (var graphics = Graphics.FromImage(resizedImage))
 {
//set parameters to create a high-quality thumbnail
 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
 graphics.SmoothingMode = SmoothingMode.AntiAlias;
 graphics.CompositingQuality = CompositingQuality.HighQuality;
 graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//use an image attribute in order to remove the black/gray border around image after resize
//(most obvious on white images), see this post for more information:
//http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
 using (var attribute = new ImageAttributes())
 {
 attribute.SetWrapMode(WrapMode.TileFlipXY);
//draws the resized image to the bitmap
 graphics.DrawImage(rotatedImage, new Rectangle(new Point(0, 0), newSize), 0, 0, rotatedImage.Width, rotatedImage.Height, GraphicsUnit.Pixel, attribute);
 }
 }
 return resizedImage;
}
///<summary>
///Calculates resized dimensions for an image, preserving the aspect ratio.
///</summary>
///<param name="image">Image instance</param>
///<param name="desiredWidth">desired width</param>
///<param name="desiredHeight">desired height</param>
///<returns>Size instance with the resized dimensions</returns>
private static Size CalculateResizedDimensions(Image image, int desiredWidth, int desiredHeight)
{
 var widthScale = (double)desiredWidth/image.Width;
 var heightScale = (double)desiredHeight/image.Height;
//scale to whichever ratio is smaller, this works for both scaling up and scaling down
 var scale = widthScale <heightScale? widthScale : heightScale;
 return new Size
 {
 Width = (int) (scale * image.Width),
 Height = (int) (scale * image.Height)
 };
}
原作者:
...