前段时间研究了JS+CSS的图片裁减,在FF正常,在IE下却提示JS参数错误.无解.只好老老实实搞缩略图.在网上搜索了一下.找到一个还算不错的代码(查看),但那个生成缩略图的函数不够完美,缩略图生成后某些部分裁剪掉了,不能实现完美的缩略.所以改写那个函数,终于成功鸟.
原图片的宽和高除以倍数后,如果宽比高长,除后的数四舍五入后一般都是等于缩图的宽.而如果高比宽长的话,除后的高也是等于缩略图的高.这个貌似是个规律.没研究过四边形的东东.还不大确定.我试了N张图片都是一样的结果.
一般原始图片的宽和高稍大于缩略图,裁剪后基本上差不多.反之,原始图片宽和高大过缩略图很多很多,缩放后的缩略图就越小.
aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_img.aspx.cs" Inherits="test_test_img" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>缩略图测试</title>
</head>
<body style="text-align:center;">
<form id="form1" runat="server">
原图:<br />
<asp:Image ID="Image1" runat="server" />
<br />
缩略图:<br />
<asp:Image ID="Image2" runat="server" />
<br />
</form>
</body>
</html> cs页,以下这个缩略图函数生成后空白部分是用背景色填充的.
1. using System;
2. using System.Data;
3. using System.Configuration;
4. using System.Collections;
5. using System.Web;
6. using System.Web.Security;
7. using System.Web.UI;
8. using System.Web.UI.WebControls;
9. using System.Web.UI.WebControls.WebParts;
10. using System.Web.UI.HtmlControls;
11. public partial class test_test_img : System.Web.UI.Page
12. {
13. protected void Page_Load(object sender, EventArgs e)
14. {
15. //原图片路径
16. string source = Server.MapPath("~/images/Source/test1.jpg");
17. string type = "";
18. //获取原图片格式
19. string fileExtension = System.IO.Path.GetExtension(source).ToLower();
20. switch (fileExtension)
21. {
22. case ".gif": type = ".gif"; break;
23. case ".jpg": type = ".jpg"; break;
24. case ".bmp": type = ".bmp"; break;
25. case ".png": type = ".png"; break;
26. }
27. //缩略图保存位置和文件名,图片格式
28. string small = Server.MapPath("~/images/Small/Small_test"+type);
29. MakeThumbnail(source, small, 180,130);
30. Image1.ImageUrl = source;
31. Image2.ImageUrl = small;
32. }
33. /**/ <summary>
34. /** 生成缩略图
35. /** </summary>
36. /** <param name="originalImagePath">源图路径(物理路径)</param>
37. /** <param name="thumbnailPath">缩略图路径(物理路径)</param>
38. /** <param name="width">缩略图宽度</param>
39. /** <param name="height">缩略图高度</param>
40. public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
41. {
42. //获取原始图片
43. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
44. //缩略图画布宽高
45. int towidth = width;
46. int toheight = height;
47. //原始图片写入画布坐标和宽高(用来设置裁减溢出部分)
48. int x = 0;
49. int y = 0;
50. int ow = originalImage.Width;
51. int oh = originalImage.Height;
52. //原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放)
53. int bg_x = 0;
54. int bg_y = 0;
55. int bg_w = towidth;
56. int bg_h = toheight;
57. //倍数变量
58. double multiple = 0;
59. //获取宽长的或是高长与缩略图的倍数
60. if (originalImage.Width >= originalImage.Height) multiple = (double)originalImage.Width / (double)width;
61. else multiple = (double)originalImage.Height / (double)height;
62. //上传的图片的宽和高小等于缩略图
63. if (ow <= width && oh <= height)
64. {
65. //缩略图按原始宽高
66. bg_w = originalImage.Width;
67. bg_h = originalImage.Height;
68. //空白部分用背景色填充
69. bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2);
70. bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2);
71. }
72. //上传的图片的宽和高大于缩略图
73. else
74. {
75. //宽高按比例缩放
76. bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
77. bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
78. //空白部分用背景色填充
79. bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2);
80. bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2);
81. }
82. //新建一个bmp图片,并设置缩略图大小.
83. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
84. //新建一个画板
85. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
86. //设置高质量插值法
87. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
88. //设置高质量,低速度呈现平滑程度
89. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
90. //清空画布并设置背景色
91. g.Clear(System.Drawing.ColorTranslator.FromHtml("#F2F2F2"));
92. //在指定位置并且按指定大小绘制原图片的指定部分
93. //第一个System.Drawing.Rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素
94. g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
95. try
96. {
97. //获取图片类型
98. string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
99. //按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
100. switch (fileExtension)
101. {
102. case ".gif": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); break;
103. case ".jpg": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
104. case ".bmp": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); break;
105. case ".png": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); break;
106. }
107. }
108. catch (System.Exception e)
109. {
110. throw e;
111. }
112. finally
113. {
114. originalImage.Dispose();
115. bitmap.Dispose();
116. g.Dispose();
117. }
118. }
119. } 不用背景色填充空白部分 ,只需要修改判断条件这一部分,和去掉画布背景色的设置这段代码(也可以不去掉,因为不填充空白部分^ ^)
1. //上传的图片的宽和高小等于缩略图
2. if (ow <= width && oh <= height)
3. {
4. //图片按缩略图宽高
5. bg_w = originalImage.Width;
6. bg_h = originalImage.Height;
7. //缩略图按原始宽高.
8. towidth = originalImage.Width;
9. toheight = originalImage.Height;
10. }
11. //上传的图片的宽和高大于缩略图
12. else
13. {
14. //宽高按比例缩放
15. bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
16. bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
17. //缩略图宽高按比例缩放.
18. towidth = Convert.ToInt32((double)originalImage.Width / multiple);
19. toheight = Convert.ToInt32((double)originalImage.Height / multiple);
20. //设置坐标
21. bg_x = 0;
22. bg_y = 0;
23. }