C#中的图像边框操作

4 .net c# image

我需要在我为公司制作的网站上调整图片大小.图片必须是非常特定的尺寸,如果比例不正确,我必须能够用边框填充图像以使其"适合".我不确定解决这个问题的最佳方法是什么.我的下意识只是根据需要简单地将矩形添加到图像中,但是我无法找到制作这样的合成图像的方法.我应该制作一个合适的空白矩形,并在上面叠加我的图像吗?我应该最关注哪些库或函数?

调整大小和保存所有工作都很棒 - 这不是问题.添加此填充是唯一的问题.

Tho*_*que 8

创建一个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);
}
Run Code Online (Sandbox Code Playgroud)

  • Dispose()/ using()警报! (2认同)

Mus*_*sis 1

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