gpt4 book ai didi

c# - 在不损失图像质量的情况下在 asp.net 中调整图像大小

转载 作者:IT王子 更新时间:2023-10-29 04:03:14 25 4
gpt4 key购买 nike

我正在开发一个 ASP.NET 3.5 Web 应用程序,我允许我的用户在其中上传 jpeg、gif、bmp 或 png 图像。如果上传的图像尺寸大于 103 x 32,我想将上传图像的大小调整为 103 x 32。我阅读了一些博客文章和文章,也尝试了一些代码示例,但似乎没有任何效果。有没有人成功做到这一点?

最佳答案

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

/// <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)
};
}

关于c# - 在不损失图像质量的情况下在 asp.net 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2319983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com