ASP.NET 图像大小调整

发布于 2024-08-07 01:05:39 字数 470 浏览 6 评论 0原文

我们需要能够优雅地将任何照片/图像的大小调整为精确的宽度/高度,而不会使图像倾斜。我们可以使用第三方解决方案,也可以使用内置的 .NET 功能来处理此问题。我认为必须有一个简单的解决方案来解决这个问题,而无需编写复杂的算法。

示例场景(我们希望将所有上传的大小调整为 200x100 像素)
尺寸为 1250x800 的风景照片:
将宽度调整为 200 像素会按比例将高度调整为 128 像素,这样顶部和底部就会剪掉额外的 28 像素。

尺寸为 1250x500 的风景照片:
按比例将宽度调整为 200 像素,将高度调整为 80 像素,因此我们需要先捕捉到这一点并按高度调整大小。将高度设置为 100 像素将按比例将宽度设置为 250 像素。额外的 50 像素需要从照片的两侧剪掉。

尺寸为 800x950 的肖像照片:
将宽度调整为 200 像素会按比例将高度设置为 238 像素,这样顶部和底部就会剪掉额外的 138 像素。

We need the ability to gracefully resize any photo/image to an exact width/height without skewing the image. We can use either a third-party solution, or we can use the built-in .NET functionality to handle this. I thought there had to be an easy solution to this without having to program a complex algorithm.

Example Scenario (We want all uploads to be resized to 200x100 pixels)
Landscape photo with dimensions at 1250x800:
Resizing the width to 200px would proportionately put the height at 128px so that extra 28px would be cropped off of the top and bottom.

Landscape photo with dimensions at 1250x500:
Resizing the width to 200px proportionately put the heigth at 80px so we would need to catch that and resize by height first. Putting the height at 100px would proportionately put the width at 250px. The extra 50px would need to be cropped off of the sides of the photo.

Portrait photo with dimensions at 800x950:
Resizing the width to 200px would proportionately put the height at 238px so that extra 138px would be cropped off of the top and bottom.

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

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

发布评论

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

评论(6

蒗幽 2024-08-14 01:05:39

如果我在家,我会给你处理这个问题的代码。

但是,您可以在这里找到与您要查找的内容类似的内容:
http://www.codeproject.com/KB/aspnet/resizeimg_emanuele_briano.aspx

编辑

这是一个更好的链接。
http://www.codeproject.com/KB/graphics/ImageResizingManager.aspx

If I were at home I'd give you my code for handling this.

However, you can find something similar to what you're looking for here:
http://www.codeproject.com/KB/aspnet/resizeimg_emanuele_briano.aspx

Edit

This is a better link.
http://www.codeproject.com/KB/graphics/ImageResizingManager.aspx

半岛未凉 2024-08-14 01:05:39

您可以使用 Bitmap 类 来执行此操作,例如this:

using (Bitmap original= new Bitmap(...)) {   //A stream or a filename
    //Calculate the desired size using original.Height and original.Width

    Bitmap newImage = new Bitmap(newWidth, newHeight);

    using(Graphics g = Graphics.FromImage(newImage) {

调用 g.DrawImage 的此重载来绘制剪切部分将原始图像转换为新图像。

    }
}

You can do this using the Bitmap class, like this:

using (Bitmap original= new Bitmap(...)) {   //A stream or a filename
    //Calculate the desired size using original.Height and original.Width

    Bitmap newImage = new Bitmap(newWidth, newHeight);

    using(Graphics g = Graphics.FromImage(newImage) {

Call this overload of g.DrawImage to draw the clipped portion of the original image to the new image.

    }
}
聆听风音 2024-08-14 01:05:39

GDI+ 在 ASP.NET 中运行良好。 (WPF 过去是这样,但现在不是)。

将图像拟合到给定矩形的算法并不太棘手:计算出图像和目标的宽度/高度比率,然后根据宽度或高度调整大小。显然,您选择哪个取决于您是想要适应目标还是覆盖目标。

另外,我发现基于堆栈的方法可以很好地表示转换:

http://www.hackification.com/2008/10/29/stack-based-processing-part-2/

GDI+ works fine in ASP.NET. (WPF used to, but doesn't now).

The algorithm for fitting an image into a given rectangle isn't too tricky: work out the ratios of width/height for both the image and the target, then resize based on either width or height. Obviously which you choose depends on whether you want to fit in the target, or cover the target.

Also, I found that a stack-based approach worked nicely for representing the transforms:

http://www.hackification.com/2008/10/29/stack-based-processing-part-2/

只想待在家 2024-08-14 01:05:39

我不会裁剪任何东西。例如,在第一种情况下,我会首先调整高于所需纵横比的边缘大小。如果你以如此小的尺寸改变纵横比,东西就无法很好地缩放,所以我不会弄乱它。

将 1250x800 图像大小调整为 156.25x100。然后,生成的图像可以在 200x100 帧内居中。如果这是执行任务的用户,您可以使用一些预览选项,让他们查看缩放后的图像是否符合所需的范围

I wouldn't crop anything. For example in your first case I would resize the edge that is higher than the desired aspect ratio first. Stuff wont scale well if you change the aspect ratio at such a small size, so I wouldn't mess with it.

Resize the 1250x800 image to 156.25x100. The resulting image can then be centered within the 200x100 frame. If this is a user doing the task you can have some preview option that lets them see if their scaled image fits within the desired bounds

向日葵 2024-08-14 01:05:39

我不久前遇到过同样的事情。这是我用来使用.net调整高质量图像大小的文章。 JPG 图像效果最好。 GIF 图像的效果不佳。

我进行了大量搜索以找到高质量调整大小的解决方案。如果您的分辨率很奇怪,那么许多代码都会创建像素化图像。

您可以选择裁剪或留白。我选择裁剪是因为我不喜欢空白。看起来很丑。如果您将图像居中并裁剪顶部和底部或左侧和右侧,则大多数情况下效果很好。

这是一篇关于裁剪图像的简短文章。

我有当前使用的工作代码,但目前无法使用它。也许稍后我会添加我自己的代码。

I ran into this exact same thing sometime ago. Here's the article I used to do high quality image resize with .net. JPG images work the best. Didn't have good results with GIF images.

I've searched around a lot to find solutions for high quality resize. Many of the code that's out there creates pixelated image if you have odd resolution.

It is your choice to either crop or have white space. I chose to do crop because I didn't like white space. Looks ugly. It works well most of the time if you have the image centered and crop either top&bottom or left&right.

Here's a short article on cropping image.

I have working code that I currently use, but I cannot get to it at the moment. Maybe I'll add my own code later.

驱逐舰岛风号 2024-08-14 01:05:39

您可以在这篇文章中看到有人刚刚创建了示例应用程序来调整图像大小。

他们还提供了 Class Image Resizer 供参考。

C# 中的示例图像调整器
C# 图像缩放器类

You can see on this post someone just created sample application to resize image.

They also provide the Class Image Resizer for reference.

Sample Image Resizer in C#
Image Resizer Class C#

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