您的位置:首页 > 编程语言 > C#

C#清晰的图片缩略方案[转载]

2010-04-20 10:25 183 查看
C#清晰的图片缩略方案_.net教程网
http://www.soaspx.com/dotnet/csharp/csharp_20091118_1677.html

希望这篇文章能给对于需要经常生成缩略图的朋友提供帮助!

购吧网目前拥有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;
oh = originalImage.Width * Y / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}

System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

g.Clear(System.Drawing.Color.Transparent);

g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
bitmap.Save(result, System.Drawing.Imaging.ImageFormat.Jpeg);

}
catch
{
//do something
}
return result;
} }
}

此算法可以满足一般的网站的需求,但是作为一个电子商务网站,商品的图片的清晰度直接影响到消费都对此商品的购买欲望。

为了找到更好的方案,终于让我们找到了一个好的组件:MagickNet 这个组件是用C++写的,不过网络上已经有可用的C#调用版,文章的
后我也会给出这个DLL文件,值得一提的是这个组件是开源的。大家可以去研究下。

MagickNet 的功能很多,我这里就贴出一下他的缩略方法的用法(MagickNet 的资料在网上很难早)

using System;
using System.Collections.Generic;
using System.Text;

namespace KeChenDLL
{
public class UploadFile
{
private string _path;//要处理的图片(原图)地址
public UploadFile(string path)
{
this._path = path;
}
private UploadFile()
{

}
public void ReSize(int width, int height,string SaveToPath)
{
MagickNet.Image img = new MagickNet.Image(_path);
img.Quality = 100;

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(_path);
int x = bitmap.Width;
int y = bitmap.Height;
float rank = (float) x/y;
if (x > y)
{
height =Convert.ToInt32(height / rank);
}
else
{
width =Convert.ToInt32(width * rank);
}

img.Resize(new System.Drawing.Size(width, height));
img.Write(SaveToPath);
img.Dispose();
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: