简体   繁体   English

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

[英]What's the best way to “square” an image in .NET?

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. 我需要为一堆jpeg(200,000+)生成缩略图 ,但是我想确保所有拇指的高度和宽度均相等。 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)

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). 然后,当您放置调整大小后的图像时,只需根据较小的尺寸计算图像的位置,然后将该尺寸移动一半的差值(另一个保持为0)。

Wouldn't that work? 那行不通吗?

Like Vaibhav said, first paint the entire thumbnail area with black. 就像Vaibhav所说的那样,首先用黑色绘制整个缩略图区域。 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: 用于将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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.net网站上生成文本文件的最佳方法是什么? - What's the best way to generate a Text file in a .net website? 在VB.net中进行比较的最佳方法是什么 - What's the best way to compare this in VB.net 在 VB .NET 中计算目录大小的最佳方法是什么? - What’s the best way to calculate the size of a directory in VB .NET? 在VB.NET中实现“可选委托”的最佳方法是什么? - What's the best way to implement an “optional delegate” in VB.NET? 使用vb.net在大型excel文件上执行SQL查询的最佳方法是什么? - What's the best way to execute SQL Query on large excel file using vb.net? 在VB.NET中更新或插入SQL记录的最佳(简单)方法是什么 - What's the best (simple) way to UPDATE or INSERT a SQL record in VB.NET 在 VB.NET 中初始化类中的共享成员的最佳方法是什么? - What's the best way to initialize shared members in a class in VB.NET? 混合VB.NET的Option Strict和新的Option Infer指令的最佳方法是什么? - What is the best way to mix VB.NET's Option Strict and the new Option Infer directives? 在Visual Basic .NET中为视频游戏绘制图形的最佳方法是什么? - What's the best way to draw graphics for a video game in Visual Basic .NET? 在VB.NET中读取大型数据库表的最佳方法是什么? - What's the best way to read a very large database table in VB.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM