简体   繁体   中英

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 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以获得最佳质量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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