.net 缩略图 宽高比 .js缩略图 宽高比
.net 缩略图
1: #region 生成缩略图
2:
3: /// <summary>
4: /// 生成缩略图
5: /// </summary>
6: /// <param name="originalImagePath">源图路径(物理路径)</param>
7: /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
8: /// <param name="width">缩略图宽度</param>
9: /// <param name="height">缩略图高度</param>
10: /// <param name="mode">生成缩略图的方式</param>
11: public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height,
12: string mode)
13: {
14: Image originalImage = Image.FromFile(originalImagePath);
15:
16: int towidth = width;
17: int toheight = height;
18:
19: int x = 0;
20: int y = 0;
21: int ow = originalImage.Width;
22: int oh = originalImage.Height;
23:
24: switch (mode)
25: {
26: case "HW": //指定高宽缩放(可能变形)
27: break;
28: case "W": //指定宽,高按比例
29: toheight = originalImage.Height*width/originalImage.Width;
30: break;
31: case "H": //指定高,宽按比例
32: towidth = originalImage.Width*height/originalImage.Height;
33: break;
34: case "Cut": //指定高宽裁减(不变形)
35: if ((double) originalImage.Width/(double) originalImage.Height > (double) towidth/(double) toheight)
36: {
37: oh = originalImage.Height;
38: ow = originalImage.Height*towidth/toheight;
39: y = 0;
40: x = (originalImage.Width - ow)/2;
41: }
42: else
43: {
44: ow = originalImage.Width;
45: oh = originalImage.Width*height/towidth;
46: x = 0;
47: y = (originalImage.Height - oh)/2;
48: }
49: break;
50: default:
51: break;
52: }
53:
54: //新建一个bmp图片
55: Image bitmap = new Bitmap(towidth, toheight);
56:
57: //新建一个画板
58: Graphics g = Graphics.FromImage(bitmap);
59:
60: //设置高质量插值法
61: g.InterpolationMode = InterpolationMode.High;
62:
63: //设置高质量,低速度呈现平滑程度
64: g.SmoothingMode = SmoothingMode.HighQuality;
65:
66: //清空画布并以透明背景色填充
67: g.Clear(Color.Transparent);
68:
69: //在指定位置并且按指定大小绘制原图片的指定部分
70: g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
71: new Rectangle(x, y, ow, oh),
72: GraphicsUnit.Pixel);
73:
74: try
75: {
76: //以jpg格式保存缩略图
77: bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
78: }
79: catch (Exception e)
80: {
81: throw e;
82: }
83: finally
84: {
85: originalImage.Dispose();
86: bitmap.Dispose();
87: g.Dispose();
88: }
89: }
90:
91: #endregion
92:
.js缩略图显示
1: function DrawImage(imgD, iwidth, iheight) {
2:
3: //参数(图片,允许的宽度,允许的高度)
4: var image = new Image();
5: image.src = imgD.src;
6:
7: if (image.width > 0 && image.height > 0) {
8: if (image.width / image.height >= iwidth / iheight) {
9: if (image.width > iwidth) {
10: imgD.width = iwidth;
11: imgD.height = (image.height * iwidth) / image.width;
12: } else {
13: imgD.width = image.width;
14: imgD.height = image.height;
15: }
16: } else {
17: if (image.height > iheight) {
18: imgD.height = iheight;
19: imgD.width = (image.width * iheight) / image.height;
20: } else {
21: imgD.width = image.width;
22: imgD.height = image.height;
23: }
24: }
25: }
26: }
27:
js调用如下
1: <asp:Literal ID="litImg" runat="server" Visible="false"></asp:Literal>
1: litImg.Visible = true;
2: litImg.Text = "<img width='200' onload='DrawImage(this,400,320)' src='" + msgModel.Photourl + "'>";