如何使用C#以低分辨率形式提供高分辨率图像 [英] How to serve high-resolution imagery in a low-resolution form using C#

查看:387
本文介绍了如何使用C#以低分辨率形式提供高分辨率图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用300dpi tif图像在网络上显示。目前,当用户上传图片时,我正在动态创建缩略图。如果创建的页面引用宽度为500x500px的高分辨率图像,我是否可以使用相同的功能即时转换为gif / jpg。什么是即将创建的jpg解决方案?

Trying to use 300dpi tif images for display on the web. At the moment, when the user uploads an image, I am dynamically creating a thumbnail. If a page is created referencing the high-res image with a width of 500x500px, can I use the same functionality to convert to a gif/jpg on the fly. What is the impending resolution of the jpg that would be created?

编辑:

为了进一步说明用法,用户上传了大约3000x3000像素的300dpi图像。用户正在使用这些图像来创建将用于pdf打印的目录页面。当他们创建页面时,我们只需要72dpi图像显示在屏幕上,但是对于打印,需要300dpi图像。显然,他们不想在页面上添加3000x3000px图像,因此需要将其调整到正确的查看区域,例如500x500px等。

To further explain the usage, the user uploads 300dpi images which are approx 3000x3000 pixels. The user is using these images to create a catalog page which will be used for pdf printing. When they are creating the page, we only need 72dpi images to display to the screen, but for print, need the 300dpi images. Obviously they do not want to add a 3000x3000px image to the page, so it needs to be resized to the correct viewing area, e.g. 500x500px etc.

推荐答案

这归结为简单的图像大小调整。关于DPI的讨论只是计算比例因子的辅助数据。

This boils down to a simple image resize. The discussion of DPIs is just ancillary data to calculate the scale factor.

正如@Guffa所说,你应该在上传时这样做,这样你就可以服务您的查看器中的静态图片。

As @Guffa said, you should do this at the time of the upload so that you can just serve static images in your viewer.

这将是服务器上的负载:

This will be a load on the server:


  1. 加载完整图像。这将是您的3000x3000图像大约27 MB的内存。

  2. 调整大小。懒散的数学(仍然是CPU密集型)。

  3. 压缩。更多CPU +写入驱动器的成本。

由于您已经花时间生成缩略图,因此您可以分摊该费用并且这个成本不需要重复上面的步骤1(参见代码)。

Since you are already taking the time to generate a thumbnail, you can amortize that cost and this cost by not having to repeat Step 1 above (see the code).

图像上传后,我建议分离一个线程来完成这项工作。这肯定是Web服务器上的一个负载,但是你只有其他选择就是投入第二台机器来执行工作。它最终必须完成。

After an image is uplaoded, I would recommend spinning off a thread to do this work. It's a load on the web server for sure, but you're only other option is to devote a second machine to performing the work. It will have to be done eventually.

这是一些代码来完成这项工作。重要的是这些:

Here is some code to do the job. The important lines are these:

OutputAsJpeg(Resize(big, 300.0, 72.0), new FileStream("ScreenView.jpg"));
OutputAsJpeg(Resize(big, bigSize, 64.0), new FileStream("Thumbnail.jpg"));

我们可以调整图像的尺寸,但是我们可以需要。在第一行中,我们只是按固定比例(72.0 / 300.0)缩小。在第二行,我们强制图像的最终最大尺寸为64(比例因子= 64.0 / 3000.0)。

We can resize the big image however we need. In the first line we just scale it down by a fixed scale (72.0 / 300.0). On the second line, we force the image to have a final max dimension of 64 (scale factor = 64.0 / 3000.0).

using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

BitmapSource Resize(BitmapSource original,
                    double originalScale,
                    double newScale) {
    double s = newScale / originalScale;
    return new TransformedBitmap(original, new ScaleTransform(s, s));
}

void OutputAsJpeg(BitmapSource src, Stream out) {
    var encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(src));
    encoder.Save(out);
}

// Load up your bitmap from the file system or whatever,
// then dump it out to a smaller version and a thumbnail.
// Assumes thumbnails have a max dimension of 64
BitmapSource big = new BitmapImage(new Uri("BigPage0.png",
                                           UriKind. RelativeOrAbsolute));
double bigSize = Math.Max(big.PixelWidth, big.PixelHeight);
OutputAsJpeg(Resize(big, 300.0, 72.0), new FileStream("ScreenView.jpg"));
OutputAsJpeg(Resize(big, bigSize, 64.0), new FileStream("Thumbnail.jpg"));

这篇关于如何使用C#以低分辨率形式提供高分辨率图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆