C# 如何在 .NET 中按比例调整任何类型的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/703873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to proportional resize image of any type in .NET?
提问by eKek0
Is possible to resize image proportionally in a way independent of the image type (bmp, jpg, png, etc)?
是否可以以独立于图像类型(bmp、jpg、png 等)的方式按比例调整图像大小?
I have this code and know that something is missing (but don't know what):
我有这个代码并且知道缺少某些东西(但不知道是什么):
public bool ResizeImage(string fileName, string imgFileName,
ImageFormat format, int width, int height)
{
try
{
using (Image img = Image.FromFile(fileName))
{
Image thumbNail = new Bitmap(width, height, img.PixelFormat);
Graphics g = Graphics.FromImage(thumbNail);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle rect = new Rectangle(0, 0, width, height);
g.DrawImage(img, rect);
thumbNail.Save(imgFileName, format);
}
return true;
}
catch (Exception)
{
return false;
}
}
If not possible, how can I resize proportional a jpeg image?
如果不可能,我如何调整 jpeg 图像的比例?
I know that using this method, but don't know where to put this (!).
我知道使用这种方法,但不知道把它放在哪里(!)。
采纳答案by jerebear
First and foremost, you're not grabbing the CURRENT height and width of the image. In order to resize proportionately you'll need to grab the current height/width of the image and resize based on that.
首先,您没有获取图像的当前高度和宽度。为了按比例调整大小,您需要获取图像的当前高度/宽度并基于此调整大小。
From there, find the greatest attribute and resize proportionately based on that.
从那里,找到最大的属性并根据它按比例调整大小。
For instance, let's say the current image is 800 x 600 and you wanna resize proportionately within a 400 x 400 space. Grab the greatest proportion (800) and find it's ratio to the new size. 800 -> 400 = .5 Now take that ratio and multiply by the second dimension (600 * .5 = 300).
例如,假设当前图像是 800 x 600,并且您想在 400 x 400 空间内按比例调整大小。抓住最大的比例(800)并找到它与新尺寸的比例。800 -> 400 = .5 现在采用该比率并乘以第二个维度 (600 * .5 = 300)。
Your new size is 400 x 300. Here's a PHP example (sorry....you'll get it though)
您的新尺寸为 400 x 300。这是一个 PHP 示例(抱歉……不过您会明白的)
$thumb_width = 400;
$thumb_height = 400;
$orig_w=imagesx($src_img);
$orig_h=imagesy($src_img);
if ($orig_w>$orig_h){//find the greater proportion
$ratio=$thumb_width/$orig_w;
$thumb_height=$orig_h*$ratio;
}else{
$ratio=$thumb_height/$orig_h;
$thumb_width=$orig_w*$ratio;
}
回答by BFree
I think your code is fine, but taking in the width and the height as parameters is where you're going wrong in my opinion. Why should the caller of this method have to decide how big they want the width and the height? I would suggest changing it to a percentage:
我认为您的代码很好,但是在我看来,将宽度和高度作为参数是您出错的地方。为什么这个方法的调用者必须决定他们想要的宽度和高度有多大?我建议将其更改为百分比:
public bool ResizeImage(string fileName, string imgFileName,
ImageFormat format, int percent)
{
try
{
using (Image img = Image.FromFile(fileName))
{
int width = img.Width * (percent * .01);
int height = img.Height * (percent * .01);
Image thumbNail = new Bitmap(width, height, img.PixelFormat);
Graphics g = Graphics.FromImage(thumbNail);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle rect = new Rectangle(0, 0, width, height);
g.DrawImage(img, rect);
thumbNail.Save(imgFileName, format);
}
return true;
}
catch (Exception)
{
return false;
}
}