“平方”的最佳方法是什么? .NET 中的图像?
我需要生成缩略图一堆 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个怎么样。 也许您应该首先在位图上绘制一个黑色(或任何颜色)矩形。
然后,当您放置调整大小的图像时,只需根据较短的尺寸计算图像的位置,然后将该尺寸移动差值的一半(并将另一个保持为 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?
正如 Vaibhav 所说,首先将整个缩略图区域涂成黑色。 这比首先将图像适合缩略图,然后确定将哪些矩形涂成黑色以实现 pillarboxing< 更简单/a> 或 信箱。
用于将
imageWidth
ximageHeight
图像适配到thumbWidth
xthumbHeight
的通用解决方案的伪代码(确实如此) t 必须是正方形)面积: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
ximageHeight
image into athumbWidth
xthumbHeight
(doesn't have to be a square) area: