我需要生成缩略图对于一堆jpegs(200,000+),但我想确保我的所有拇指都有相同的高度和宽度。但是,我不想更改图像的比例,因此我需要将空白空间添加到较短的尺寸以“正方形”。空白区域的背景颜色是可变的。

这是我用来生成大拇指的代码片段。什么是做平方的最好方法?

     Dim imgDest As System.Drawing.Bitmap = New Bitmap(ScaleWidth, ScaleHeight)
     imgDest.SetResolution(TARGET_RESOLUTION, TARGET_RESOLUTION)  
     Dim grDest As Graphics = Graphics.FromImage(imgDest)

     grDest.DrawImage(SourceImage, 0, 0, imgDest.Width, imgDest.Height)
有帮助吗?

解决方案

这个怎么样?也许你应该首先在Bitmap上绘制一个黑色(或任何颜色)矩形。

然后当您放置已调整大小的图像时,只需根据较短的尺寸计算图像的位置,然后将该尺寸移动差异的一半(并将另一个保持为0)。

那不行吗?

其他提示

Vaibhav 所说,首先用黑色绘制整个缩略图区域。这比首先将图像拟合到缩略图中然后确定要绘制黑色的矩形以实现 pillarboxing <更简单/ a>或 letterboxing

用于将 imageWidth x imageHeight 图像拟合到 thumbWidth x thumbHeight 的通用解决方案的伪代码(不一定是方形)区域:

imageRatio = imageWidth / imageHeight;
thumbRatio = thumbWidth / thumbHeight;

zoomFactor = imageRatio >= thumbRatio
    ? thumbWidth / imageWidth 
    : thumbHeight / imageHeight;

destWidth = imageWidth * zoomFactor;
destHeight = imageHeight * zoomFactor;

drawImage(
    (thumbWidth - destWidth) >> 1,
    (thumbHeight - destHeight) >> 1,
    destWidth,
    destHeight);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top