简体   繁体   English

C#中的图像边框操作

[英]Image border manipulation in C#

I have a need to resize pictures on a website I'm making for my company. 我需要在我为公司制作的网站上调整图片大小。 The pictures must be a very specific size, and if the proportions are not correct I must be able to pad the image with a border to make it 'fit'. 图片必须是非常特定的尺寸,如果比例不正确,我必须能够用边框填充图像以使其“适合”。 I'm not sure what the best way to approach this problem is. 我不确定解决这个问题的最佳方法是什么。 My knee-jerk was to simply add Rectangles to the image on the fly as I needed, but I'm unable to find a way to make a composite image like that. 我的下意识就是根据需要简单地将矩形添加到图像中,但是我无法找到一种方法来制作这样的合成图像。 Should I just make a suitable, blank Rectangle, and superimpose my image over it? 我应该制作一个合适的空白矩形,并在上面叠加我的图像吗? Which libraries or functions should I be most heavily looking at? 我应该最关注哪些库或函数?

The resizing and saving all work great - that is not the issue. 调整大小和保存所有工作都很棒 - 这不是问题。 Adding this padding is the only problem. 添加此填充是唯一的问题。

Create a new Bitmap of the proper size, fill it with the padding color you want, and draw the original image in the center : 创建一个适当大小的新Bitmap ,用您想要的填充颜色填充它,并在中心绘制原始图像:

Bitmap newImage = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(newImage))
{
    graphics.Clear(paddingColor);
    int x = (newImage.Width - originalImage.Width) / 2;
    int y = (newImage.Height - originalImage.Height) / 2;
    graphics.DrawImage(originalImage, x, y);
}

最简单的方法是从最终图像中所需尺寸的Bitmap开始,然后使用Graphics.Clear绘制所需的背景颜色,然后使用Graphics.DrawImage将原始图像复制到主Bitmap上,根据需要调整其大小在此步骤中,将InterpolationMode设置为HighQualityBicubic以获得最佳质量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM