c# - 在不丟失圖像質量的ASP.NET 中,C# 調整圖像的大小

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

我正在開發一個 ASP.NET 3.5 web應用程序,我允許用戶上傳 jpeg 。gif 。bmp或者png圖像。 上傳圖像尺寸大於 103,我想把上傳的圖像調整到 103 。 我已經讀過一些博客文章和文章,也嘗試了一些代碼樣本,但看起來沒有什麼好處。 有誰成功了?

时间: 原作者:

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)


 };


}



原作者:
...