如何使用C#以低分辨率形式提供高分辨率圖像

[英]How to serve high-resolution imagery in a low-resolution form using C#


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?

嘗試使用300dpi tif圖像在網絡上顯示。目前,當用戶上傳圖片時,我正在動態創建縮略圖。如果創建的頁面引用寬度為500x500px的高分辨率圖像,我是否可以使用相同的功能即時轉換為gif / jpg。什么是即將解決的jpg將被創建?

EDIT:

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.

為了進一步解釋該用法,用戶上傳了大約3000×3000像素的300dpi圖像。用戶正在使用這些圖像來創建將用於pdf打印的目錄頁面。當他們創建頁面時,我們只需要72dpi圖像顯示在屏幕上,但是對於打印,需要300dpi圖像。顯然,他們不想在頁面上添加3000x3000px圖像,因此需要將其調整到正確的查看區域,例如500x500px等

4 个解决方案

#1


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

這歸結為簡單的圖像調整大小。對DPI的討論只是計算比例因子的輔助數據。

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

正如@Guffa所說,您應該在上傳時執行此操作,以便您可以在查看器中提供靜態圖像。

This will be a load on the server:

這將是服務器上的負載:

  1. Load the full image. This will be about 27 MB of memory for your 3000x3000 images.
  2. 加載完整圖像。對於3000x3000圖像,這將是大約27 MB的內存。

  3. Resize. Lot's of math done lazily (still CPU intensive).
  4. 調整。很多數學懶散(仍然是CPU密集型)。

  5. Compress. More CPU + Cost of writing to your drive.
  6. 壓縮。更多CPU +寫入驅動器的成本。

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).

由於您已經花時間生成縮略圖,因此您無需重復上述步驟1即可分攤該成本和此成本(請參閱代碼)。

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.

圖像上傳后,我會建議旋轉一個線程來完成這項工作。這肯定是Web服務器上的一個負載,但是你只有其他選擇就是投入第二台機器來執行工作。它最終必須完成。

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"));

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).

我們可以根據需要調整大圖像的大小。在第一行中,我們只是按固定比例(72.0 / 300.0)縮小。在第二行,我們強制圖像的最終最大尺寸為64(比例因子= 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"));

#2


If I understand what you want - you're trying to make a gif or jpg thumbnail of a very high resolution tif, for web display - if not, I apologize in advance....

如果我明白你想要什么 - 你正在嘗試制作一個非常高分辨率的gif或jpg縮略圖,用於網頁顯示 - 如果沒有,我提前道歉....


If you want the thumbnail to be 500x500px, that is the resolution of the jpg/gif you'll want to create - 500x500, or at least 500x<500 or <500x500 (to fit in the box, unless you want distorted images).

如果您希望縮略圖為500x500px,那就是您要創建的jpg / gif的分辨率 - 500x500,或至少500x <500或<500x500(除非您想要扭曲的圖像,否則要放入框中)。

For display on the web, the DPI does not matter. Just use the pixel resolution you wish directly.

要在網絡上顯示,DPI無關緊要。只需使用您想要的像素分辨率即可。

#3


Technically the JPG/GIF is created at 72-DPI by the Image classes in .NET. But the DPI really doesn't have any meaning to the browser - it just uses the dimensions 500x500.

從技術上講,JPG / GIF是由.NET中的Image類在72-DPI創建的。但DPI對瀏覽器沒有任何意義 - 它只使用尺寸500x500。

#4


When displaying an image on a web, the dpi (or more correctly ppi) setting is irrelevant. It's only the size in pixels that is relevant.

在Web上顯示圖像時,dpi(或更正確的ppi)設置無關緊要。它只是像素的大小相關。

You can convert an image on the fly, but it is very work intensive for the server to do that every time the image is displayed. You should create the sizes that you need when the user uploads the image.

您可以動態轉換圖像,但每次顯示圖像時服務器都需要這么做。您應該在用戶上傳圖像時創建所需的尺寸。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/15/720fbac6d2bb79e5d6246bc637aa9d0a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com