asp.net中 缩略图怎么使用啊?
请问 asp.net中 缩略图怎么使用,最好能够详细点!谢谢了!
------解决方案--------------------
public Image GetThumbnailImage(
int thumbWidth,
int thumbHeight,
Image..::..GetThumbnailImageAbort callback,
IntPtr callbackData
)
生成
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW":
break;
case "W":
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H":
towidth = originalImage.Width * height / 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 * height / 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.High;
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);
try
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (
System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
------解决方案--------------------
C# code
private void MakeThumbImage(string path, string stpath, int width, int height, string mode)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
int tw = width;
int th = height;
///原始图片的宽度和高度
int sw = image.Width;
int sh = image.Height;
int x = 0, y = 0;
switch (mode)
{
case "HW": ///指定高宽缩放
break;
case "W": ///指定图片的宽度,计算图片的高度
th = image.Height * width / image.Width; break;
case "H": ///指定图片的高度,计算图片的宽度
tw = image.Width * height / image.Height; break;
case "CUT":
///计算缩略图的大小
if ((double)tw / (double)th < (double)width / (double)height)
{
sw = image.Width;
sh = image.Width * height / tw;
x = 0;
y = (image.Height - sh) / 2;
}
else
{
sh = image.Height;
sw = image.Height * tw / th;
y = 0;
x = (image.Width - sw) / 2;
}
break;
default: break;
}
///创建bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th);
///创建Graphics对象g
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
///设置图像的插补模式
g.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.High;
///设置图像的平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
///清空画布,并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
///在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, tw, th),
new System.Drawing.Rectangle(x, y, sw, sh),
System.Drawing.GraphicsUnit.Pixel);
try
{ ///采用jpg格式保存缩略图
bitmap.Save(stpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write(ex.Message); return;
}
finally
{ ///释放资源
image.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
MakeThumbImage(Server.MapPath("Images/xp.jpg"), Server.MapPath("Images/xp_01.jpg"), 80, 60, "CUT");