“平方”的最佳方法是什么? .NET 中的图像?

发布于 2024-07-08 04:40:27 字数 572 浏览 4 评论 0原文

我需要生成缩略图一堆 jpeg(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)

I need generate thumbnails for a bunch of jpegs (200,000+) but I want to make sure all of my thumbs have a equal height and width. However, I don't want to change the proportions of the image so I need to add empty space to the shorter dimension to "square it up". The empty space's background color is variable.

Here's the code snippet I'm using to generate the thumbs. What's the best way to do the squaring?

     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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま昔日黯然 2024-07-15 04:40:27

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

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

那行不通?

How about this. Maybe you should draw a black (or whichever color) rectangle on the Bitmap first.

And then when you are placing the resized image, just calculate the placement of the image based on whichever dimension is shorter, and then move that dimension by half the difference (and keep the other on 0).

Wouldn't that work?

无悔心 2024-07-15 04:40:27

正如 Vaibhav 所说,首先将整个缩略图区域涂成黑色。 这比首先将图像适合缩略图,然后确定将哪些矩形涂成黑色以实现 pillarboxing< 更简单/a> 或 信箱

用于将 imageWidth x imageHeight 图像适配到 thumbWidth x thumbHeight 的通用解决方案的伪代码(确实如此) t 必须是正方形)面积:

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);

Like Vaibhav said, first paint the entire thumbnail area with black. This will be simpler than first fitting the image into the thumbnail and then determining which rectangles to paint black to achieve pillarboxing or letterboxing.

Pseudo-code for a general solution to fit an imageWidth x imageHeight image into a thumbWidth x thumbHeight (doesn't have to be a square) area:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文