VS2005 Web工程模版ClubSite中相册薄的一处BUG?
如果你正在学习VS2005的过程中,附带的模版Personal Web Site以及ClubSite无疑是最好的入门范例,虽然说不上是多么的复杂和经典,但VS2005的许多新特性都可以从中体会和借鉴。
我和大多数园子里的朋友们一样,在第一时间开始各Starter Kit之旅,对于VS2005的强大甚是赞叹!呵呵呵。。。
啰嗦开场,始入正题:
不知你是否注意到,在ClubSite的的首页,还有Album中,无论原始的图片尺寸是多大,缩略图显示的都是一个尺寸(69*69),这样导致很多图片发生变形!对于我来说(有些唯美)是不可接受的,特别是把自己MM的图片张贴上去时,更是难受。
第一反应是CSS设置或Html标记有问题,一路检索发现在显示图片的地方仅是:<asp:Image ID="Image1" runat="server" CssClass="photo" BorderWidth="1" />(ImageThumbnail.ascx文件中)这样一行!并未涉及长宽,追踪 CssClass类photo,也未提供长宽!?
这才发现问题没有预想简单。。。(之前的想法一直都以为是在取出图片数据呈现时设置不当!)。
继续检索,才发现这个问题是在图片数据入库时(upload)发生的:
代码如下:
具体代码就不一一繁述:):
贴出我的修改,应该有更好的设计,希望提出好的方案:
这段代码是对
public enum ImageSizes
{
Large = 0,
Thumb = 1,
FullSize = 2
}
取值为 1 (即缩略图模式) 时的图像处理过程,还有另外两种模式,就由你来完善吧!
我和大多数园子里的朋友们一样,在第一时间开始各Starter Kit之旅,对于VS2005的强大甚是赞叹!呵呵呵。。。
啰嗦开场,始入正题:
不知你是否注意到,在ClubSite的的首页,还有Album中,无论原始的图片尺寸是多大,缩略图显示的都是一个尺寸(69*69),这样导致很多图片发生变形!对于我来说(有些唯美)是不可接受的,特别是把自己MM的图片张贴上去时,更是难受。
第一反应是CSS设置或Html标记有问题,一路检索发现在显示图片的地方仅是:<asp:Image ID="Image1" runat="server" CssClass="photo" BorderWidth="1" />(ImageThumbnail.ascx文件中)这样一行!并未涉及长宽,追踪 CssClass类photo,也未提供长宽!?
这才发现问题没有预想简单。。。(之前的想法一直都以为是在取出图片数据呈现时设置不当!)。
继续检索,才发现这个问题是在图片数据入库时(upload)发生的:
代码如下:
1 public static byte[] MakeThumb(byte[] fullsize, int newwidth, int newheight)
2 {
3 Image iOriginal, iThumb;
4 double scaleH, scaleW;
5
6 Rectangle srcRect=new Rectangle();
7 iOriginal = Image.FromStream(new MemoryStream(fullsize));
8 scaleH = iOriginal.Height / newheight;
9 scaleW = iOriginal.Width / newwidth;
10 if (scaleH == scaleW)
11 {
12 srcRect.Width = iOriginal.Width;
13 srcRect.Height = iOriginal.Height;
14 srcRect.X = 0;
15 srcRect.Y = 0;
16 }
17 else if ((scaleH) > (scaleW))
18 {
19 srcRect.Width = iOriginal.Width;
20 srcRect.Height = Convert.ToInt32(newheight * scaleW);
21 srcRect.X = 0;
22 srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
23 }
24 else
25 {
26 srcRect.Width = Convert.ToInt32(newwidth * scaleH);
27 srcRect.Height = iOriginal.Height;
28 srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
29 srcRect.Y = 0;
30 }
31 iThumb = new Bitmap(newwidth, newheight);
32 Graphics g = Graphics.FromImage(iThumb);
33 g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
34 MemoryStream m = new MemoryStream();
35 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
36 return m.GetBuffer();
37 }
38
2 {
3 Image iOriginal, iThumb;
4 double scaleH, scaleW;
5
6 Rectangle srcRect=new Rectangle();
7 iOriginal = Image.FromStream(new MemoryStream(fullsize));
8 scaleH = iOriginal.Height / newheight;
9 scaleW = iOriginal.Width / newwidth;
10 if (scaleH == scaleW)
11 {
12 srcRect.Width = iOriginal.Width;
13 srcRect.Height = iOriginal.Height;
14 srcRect.X = 0;
15 srcRect.Y = 0;
16 }
17 else if ((scaleH) > (scaleW))
18 {
19 srcRect.Width = iOriginal.Width;
20 srcRect.Height = Convert.ToInt32(newheight * scaleW);
21 srcRect.X = 0;
22 srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
23 }
24 else
25 {
26 srcRect.Width = Convert.ToInt32(newwidth * scaleH);
27 srcRect.Height = iOriginal.Height;
28 srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
29 srcRect.Y = 0;
30 }
31 iThumb = new Bitmap(newwidth, newheight);
32 Graphics g = Graphics.FromImage(iThumb);
33 g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
34 MemoryStream m = new MemoryStream();
35 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
36 return m.GetBuffer();
37 }
38
具体代码就不一一繁述:):
贴出我的修改,应该有更好的设计,希望提出好的方案:
1 public static byte[] MakeThumb2(byte[] fullsize, int newwidth, int newheight)
2 {
3 int originalWidth;
4 int originalHeight;
5
6 int thumbWidth;
7 int thumbHeight;
8
9 using (MemoryStream tempStream = new MemoryStream(fullsize))
10 {
11 Bitmap tempBitmap = new Bitmap(tempStream);
12
13 originalWidth = tempBitmap.Width;
14 originalHeight = tempBitmap.Height;
15 }
16
17
18 double scaleH, scaleW;
19
20 scaleH = originalHeight / newheight;
21 scaleW = originalWidth / newwidth;
22
23 if (scaleH == scaleW)
24 {
25 thumbWidth = newwidth;
26 thumbHeight = newheight;
27 }
28 else if ((scaleH) > (scaleW))
29 {
30 thumbWidth = newwidth;
31 thumbHeight = Convert.ToInt32(originalHeight * newwidth / originalWidth);
32 }
33 else
34 {
35 thumbWidth = Convert.ToInt32(originalWidth * newheight / originalHeight);
36 thumbHeight = newheight;
37 }
38
39 Bitmap iThumb = new Bitmap( thumbWidth , thumbHeight);
40 Graphics g = Graphics.FromImage(iThumb);
41 g.DrawImage( Image.FromStream(new MemoryStream( fullsize )) , new Rectangle(0, 0, thumbWidth, thumbHeight));
42 MemoryStream m = new MemoryStream();
43 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
44 return m.GetBuffer();
45 }
2 {
3 int originalWidth;
4 int originalHeight;
5
6 int thumbWidth;
7 int thumbHeight;
8
9 using (MemoryStream tempStream = new MemoryStream(fullsize))
10 {
11 Bitmap tempBitmap = new Bitmap(tempStream);
12
13 originalWidth = tempBitmap.Width;
14 originalHeight = tempBitmap.Height;
15 }
16
17
18 double scaleH, scaleW;
19
20 scaleH = originalHeight / newheight;
21 scaleW = originalWidth / newwidth;
22
23 if (scaleH == scaleW)
24 {
25 thumbWidth = newwidth;
26 thumbHeight = newheight;
27 }
28 else if ((scaleH) > (scaleW))
29 {
30 thumbWidth = newwidth;
31 thumbHeight = Convert.ToInt32(originalHeight * newwidth / originalWidth);
32 }
33 else
34 {
35 thumbWidth = Convert.ToInt32(originalWidth * newheight / originalHeight);
36 thumbHeight = newheight;
37 }
38
39 Bitmap iThumb = new Bitmap( thumbWidth , thumbHeight);
40 Graphics g = Graphics.FromImage(iThumb);
41 g.DrawImage( Image.FromStream(new MemoryStream( fullsize )) , new Rectangle(0, 0, thumbWidth, thumbHeight));
42 MemoryStream m = new MemoryStream();
43 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
44 return m.GetBuffer();
45 }
这段代码是对
public enum ImageSizes
{
Large = 0,
Thumb = 1,
FullSize = 2
}
取值为 1 (即缩略图模式) 时的图像处理过程,还有另外两种模式,就由你来完善吧!