最佳调整大小和/或裁剪逻辑
我已经遇到过几次这个问题,并且认为将其放在那里会很好。您最好的图像调整大小和/或裁剪逻辑是什么。这个想法是用目标图像、尺寸和裁剪标志调用某种方法 - 这将返回或保存或任何所需的图像。
我的在下面。从 VB 转换为 C#,所以是的,会有小错误,但我们正在考虑逻辑。
// INIT
// On/off
bool WeAreCropping = true;
// Get some dimensions
int TargetWidth = RequestedWidth;
int TargetHeight = RequestedHeight;
int SourceWidth = SourceImage.Width;
int SourceHeight = SourceImage.Height;
int ResizeWidth = TargetWidth;
int ResizeHeight = TargetHeight;
// GET RESIZE VALUES
// Are we cropping?
if (WeAreCropping) {
// Get source and target aspect ratio
double SourceAspectRatio = SourceWidth / SourceHeight;
double TargetAspectRatio = TargetWidth / TargetHeight;
// Compare aspect ratios to find out if we should we resize by
// width or height or nothing
if (TargetAspectRatio < SourceAspectRatio) {
ResizeWidth = TargetHeight / SourceHeight * SourceWidth;
}
else if (TargetAspectRatio > SourceAspectRatio) {
ResizeHeight = TargetWidth / SourceWidth * SourceHeight;
}
else {
// Same aspect ratio
}
}
else {
// If the target image is bigger than the source
if (TargetWidth > SourceWidth && TargetHeight > SourceHeight) {
TargetWidth = SourceWidth;
TargetHeight = SourceHeight;
}
double Ratio = 0;
// What ratio should we resize it by
if (SourceWidth / TargetWidth > SourceHeight / TargetHeight) {
Ratio = SourceWidth / TargetWidth;
}
else {
Ratio = SourceHeight / TargetHeight;
}
ResizeWidth = Math.Ceiling(SourceWidth / Ratio);
ResizeHeight = Math.Ceiling(SourceHeight / Ratio);
}
// TIME TO DO SUMFINK
// Resize the image using ResizeWidth and ResizeHeight
// Do it
if (WeAreCropping) {
// Crop the resized image at the center TargetWidth and TargetHeight
// Do it
}
我怀疑这可能会更优雅,所以也许你可以做得更好。
I've come across this a few times and thought it would be good to put it out there. What's your best image resize and/or crop logic. The idea being that some method is called with a target image, dimensions and a crop flag - this would return or save off or whatever the desired image.
Mine is below. Converted from VB to C# so yes there'll be small bugs but logic is what we're looking at.
// INIT
// On/off
bool WeAreCropping = true;
// Get some dimensions
int TargetWidth = RequestedWidth;
int TargetHeight = RequestedHeight;
int SourceWidth = SourceImage.Width;
int SourceHeight = SourceImage.Height;
int ResizeWidth = TargetWidth;
int ResizeHeight = TargetHeight;
// GET RESIZE VALUES
// Are we cropping?
if (WeAreCropping) {
// Get source and target aspect ratio
double SourceAspectRatio = SourceWidth / SourceHeight;
double TargetAspectRatio = TargetWidth / TargetHeight;
// Compare aspect ratios to find out if we should we resize by
// width or height or nothing
if (TargetAspectRatio < SourceAspectRatio) {
ResizeWidth = TargetHeight / SourceHeight * SourceWidth;
}
else if (TargetAspectRatio > SourceAspectRatio) {
ResizeHeight = TargetWidth / SourceWidth * SourceHeight;
}
else {
// Same aspect ratio
}
}
else {
// If the target image is bigger than the source
if (TargetWidth > SourceWidth && TargetHeight > SourceHeight) {
TargetWidth = SourceWidth;
TargetHeight = SourceHeight;
}
double Ratio = 0;
// What ratio should we resize it by
if (SourceWidth / TargetWidth > SourceHeight / TargetHeight) {
Ratio = SourceWidth / TargetWidth;
}
else {
Ratio = SourceHeight / TargetHeight;
}
ResizeWidth = Math.Ceiling(SourceWidth / Ratio);
ResizeHeight = Math.Ceiling(SourceHeight / Ratio);
}
// TIME TO DO SUMFINK
// Resize the image using ResizeWidth and ResizeHeight
// Do it
if (WeAreCropping) {
// Crop the resized image at the center TargetWidth and TargetHeight
// Do it
}
I suspect this could be much more elegant so maybe you could do better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说你应该使用标准几何类型,例如 Rectangle 和 Size。那么您可能还应该支持一个用例,即当调用者想要将图像放入某个更大的矩形中,但仍然希望保持原始图像大小比例时。
您可以在 此处找到实现调整大小的一种方法。
I'd say you should utilize standard geometry types like
Rectangle
andSize
. Then you should probably also support a use case, when the caller wants to fit the image in some larger rectangle, but still wants to keep the original image sizes ratio.One way to implement the resizing you can find here.
这可能比您需要的要复杂一些,但是对于一些高级裁剪技术的示例,看看这个。
This is probably a bit more involved than what you need, but for an example of some advanced cropping techniques, check this out.