c# 将图像大小调整为不同大小,同时保留纵横比
我正在尝试调整图像大小,同时保留原始图像的纵横比,以便新图像看起来不会被压扁。
例如:
将 150*100 图像转换为 150*150 图像。
高度的额外 50 像素需要用白色背景色填充。
这是我当前使用的代码。
它非常适合调整大小,但更改原始图像的纵横比会挤压新图像。
private void resizeImage(string path, string originalFilename,
int width, int height)
{
Image image = Image.FromFile(path + originalFilename);
System.Drawing.Image thumbnail = new Bitmap(width, height);
System.Drawing.Graphics graphic =
System.Drawing.Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
System.Drawing.Imaging.ImageCodecInfo[] info =
ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
100L);
thumbnail.Save(path + width + "." + originalFilename, info[1],
encoderParameters);
}
编辑:我想填充图像而不是裁剪图像
I'm trying to resize an image while preserving the aspect ratio from the original image so the new image doesn't look squashed.
eg:
Convert a 150*100 image into a 150*150 image.
The extra 50 pixels of the height need to be padded with a white background color.
This is the current code I am using.
It works well for resizing but changing the aspect ratio of the original image squashes the new image.
private void resizeImage(string path, string originalFilename,
int width, int height)
{
Image image = Image.FromFile(path + originalFilename);
System.Drawing.Image thumbnail = new Bitmap(width, height);
System.Drawing.Graphics graphic =
System.Drawing.Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
System.Drawing.Imaging.ImageCodecInfo[] info =
ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
100L);
thumbnail.Save(path + width + "." + originalFilename, info[1],
encoderParameters);
}
EDIT: I'd like to have the image padded instead of cropped
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这应该可以做到。
编辑添加:
那些想要改进此代码的人应该将其放在评论中,或者一个新的答案中。不要直接编辑此代码。
This should do it.
Edited to add:
Those who want to improve this code should put it in the comments, or a new answer. Don't edit this code directly.
我通过学习这篇 CodeProject 文章。
I found out how to resize AND pad the image by learning from this this CodeProject Article.
我使用以下方法来计算所需的图像大小:
这将宽高比和尺寸的问题放在单独的方法中。
参数是什么
bool扩大= false
?通常你只缩小图像,因为放大意味着质量损失 - 所以如果你想这样做的话我添加了这个可选参数
I use the following method to calculate the desired image size:
This puts the problem of aspect ratio and dimensions in a separate method.
What does the parameter
bool enlarge = false
?usually you do only shrinking of images because enlarging means quality loss - so I added this optional parameter if you want to do it anyway
为了更快地得到结果,获取大小的函数可以在
resultSize
中找到:使用
Linq
来最小化变量和重新计算,以及不必要的if/else
语句To get a faster result, the function that obtains the size could be found in
resultSize
:Uses
Linq
to minimize variable and recalculations, as well as unnecesaryif/else
statements只需将其概括为宽高比和尺寸,图像内容就可以在此函数之外完成
Just generalizing it down to aspect ratios and sizes, image stuff can be done outside of this function
我也将在这里添加我的代码。此代码将允许您在强制或不强制纵横比的情况下调整图像大小,或者使用填充调整图像大小。这是egrunin 代码的修改版本。
I'm going to add my code here too. This code will allow you resize an image with or without the aspect ratio being enforced or to resize with padding. This is a modified version of egrunin's code.
这是一个不太具体的扩展方法,它适用于图像,而不是为您进行加载和保存。它还允许您指定插值方法并在使用 NearestNeighbour 插值时正确渲染边缘。
图像将在您指定的区域范围内渲染,因此您始终知道输出的宽度和高度。例如:
Here's a less specific extension method that works with Image rather than doing the loading and saving for you. It also allows you to specify interpolation method and correctly renders edges when you use NearestNeighbour interpolation.
The image will be rendered within the bounds of the area you specify so you always know your output width and height. e.g:
注意:此代码调整大小并删除宽高比之外的所有内容,而不是填充它。
Note: this code resizes and removes everything outside the aspect ratio instead of padding it..
我创建了一个比发布的答案简单得多的扩展方法。
并且应用宽高比而不裁剪图像。
用法示例:
I created a extension method that is much simpiler than the answers that are posted.
and the aspect ratio is applied without cropping the image.
Example usage:
保持方面配给并消除信箱和邮筒。
Maintain aspect Ration and eliminate letterbox and Pillarbox.
我刚刚写这篇文章是因为这里已经有的答案都不够简单。您可以将硬编码的 128 替换为您想要的任何内容,或者根据原始图像的大小进行调整。我想要的只是将图像重新缩放为 128x128 图像,保持纵横比并将结果集中在新图像中。
I just wrote this cos none of the answers already here were simple enough. You can replace the hardcoded 128 for whatever you want or base them off the size of the original image. All i wanted was to rescale the image into a 128x128 image, keeping the aspect ratio and centering the result in the new image.
我知道这是一个较旧的线程。然而,这是谷歌搜索该主题的第一个结果。
没有一个答案符合我的要求。因此,这是我通过了解旧解决方案组合而成的解决方案,但将结果图像大小限制为最合适的高度和宽度。
I know this is an older thread. However, this is the first result in a Google search on the topic.
None of the answers fit my requirement. So, here's a solution I put together by understanding the older solutions, but limiting the result image size to the most suitable height and width.