這歸結爲一個簡單的圖像調整大小。對DPI的討論只是計算比例因子的輔助數據。
正如@Guffa所說,你應該在上傳的時候這樣做,以便你可以在你的查看器中提供靜態圖像。
這將是服務器上的負載:
- 加載完整圖像。這將爲您的3000x3000圖像提供約27 MB的內存。
- 調整大小。洛特的數學很懶惰(仍然是CPU密集型)。
- 壓縮。更多CPU +寫入驅動器的成本。
由於您已經花時間生成縮略圖,因此無需重複上述步驟1(請參閱代碼)即可分攤該成本和此成本。
當圖片上傳後,我會建議分離一個線程來完成這項工作。這確實是Web服務器上的一項負擔,但您唯一的選擇就是投入第二臺機器來執行這項工作。這將不得不最終完成。
下面是一些代碼來完成這項工作。最重要的線是這些:
OutputAsJpeg(Resize(big, 300.0, 72.0), new FileStream("ScreenView.jpg"));
OutputAsJpeg(Resize(big, bigSize, 64.0), new FileStream("Thumbnail.jpg"));
我們可以調整big
圖像。然而,我們所需要的。在第一行中,我們將其縮小一個固定比例(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"));
你有什麼問題?你說你製作了縮略圖 - DPI不重要,如果它只是爲了網絡 - 有些不起作用? – 2009-04-16 00:55:21
我認爲新的標題有點誤導,不是嗎?它讓我感覺像Virtual Earth和Google Earth這樣的東西可以拼湊出高分辨率的圖像,並將其作爲較小的圖塊提供。也許使用術語'縮放'或者沿着這些線可能會使它更相關? – 2009-04-16 01:41:28