希望這篇文章能給對於需要經常生成縮略圖的朋友提供幫助!
購吧網目前擁有4000余種商品,在售商品超過2000萬,其中圖片量截至目前已有8G。
目前我們的方案是用單獨的文件服務器存放商品的圖片,通過file.365goba.com訪問。
文件服務器上架一個用於上傳圖片的WCF服務對上傳的圖片進行縮略並保存。
購吧網前期的縮略算法用的是網略上廣泛流傳的三線性插值算法(效果並不是很好),代碼如下:
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace Ants.Tools
{
public class Image
{
public int Width { get; set; }
public int Height { get; set; }
private Image() { }
public Image(int width, int height)
{
this.Width = width;
this.Height = height;
}
public MemoryStream getHightThumb(Stream imgData, string Mode_HW_W_H_Cut)
{
MemoryStream result = new MemoryStream();
System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack);
try
{
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(imgData);
int X, Y;
X = Width;
Y = Height;
int towidth = X;
int toheight = Y;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (Mode_HW_W_H_Cut)
{
case "HW": //指定高寬縮放(可能變形) break;
case "W"://指定寬,高按比例
toheight = originalImage.Height * X / originalImage.Width;
break;
case "H//指定高,寬按比例 towidth = originalImage.Width * Y / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
&n