VS2005 Web工程模版ClubSite中相册薄的一处BUG?
  如果你正在学习VS2005的过程中,附带的模版Personal Web Site以及ClubSite无疑是最好的入门范例,虽然说不上是多么的复杂和经典,但VS2005的许多新特性都可以从中体会和借鉴。
我和大多数园子里的朋友们一样,在第一时间开始各Starter Kit之旅 ,对于VS2005的强大甚是赞叹!呵呵呵。。。
,对于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的强大甚是赞叹!呵呵呵。。。
,对于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)
    public static byte[] MakeThumb(byte[] fullsize, int newwidth, int newheight)
2 {
    {
3 Image iOriginal, iThumb;
        Image iOriginal, iThumb;
4 double scaleH, scaleW;
        double scaleH, scaleW;
5
6 Rectangle srcRect=new Rectangle();
        Rectangle srcRect=new Rectangle();
7 iOriginal = Image.FromStream(new MemoryStream(fullsize));
        iOriginal = Image.FromStream(new MemoryStream(fullsize));
8 scaleH = iOriginal.Height / newheight;
        scaleH = iOriginal.Height / newheight;
9 scaleW = iOriginal.Width / newwidth;
        scaleW = iOriginal.Width / newwidth;
10 if (scaleH == scaleW)
        if (scaleH == scaleW)
11 {
        {
12 srcRect.Width = iOriginal.Width;
            srcRect.Width = iOriginal.Width;
13 srcRect.Height = iOriginal.Height;
            srcRect.Height = iOriginal.Height;
14 srcRect.X = 0;
            srcRect.X = 0;
15 srcRect.Y = 0;
            srcRect.Y = 0;
16 }
        }
17 else if ((scaleH) > (scaleW))
        else if ((scaleH) > (scaleW))
18 {
        {
19 srcRect.Width = iOriginal.Width;
            srcRect.Width = iOriginal.Width;
20 srcRect.Height = Convert.ToInt32(newheight * scaleW);
            srcRect.Height = Convert.ToInt32(newheight * scaleW);
21 srcRect.X = 0;
            srcRect.X = 0;
22 srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
            srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
23 }
        }
24 else
        else
25 {
        {
26 srcRect.Width = Convert.ToInt32(newwidth * scaleH);
            srcRect.Width = Convert.ToInt32(newwidth * scaleH);
27 srcRect.Height = iOriginal.Height;
            srcRect.Height = iOriginal.Height;
28 srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
            srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
29 srcRect.Y = 0;
            srcRect.Y = 0;
30 }
        }
31 iThumb = new Bitmap(newwidth, newheight);
        iThumb = new Bitmap(newwidth, newheight);
32 Graphics g = Graphics.FromImage(iThumb);
        Graphics g = Graphics.FromImage(iThumb);
33 g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
        g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
34 MemoryStream m = new MemoryStream();
        MemoryStream m = new MemoryStream();
35 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
        iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
36 return m.GetBuffer();
        return m.GetBuffer();
37 }
    }
38
 public static byte[] MakeThumb(byte[] fullsize, int newwidth, int newheight)
    public static byte[] MakeThumb(byte[] fullsize, int newwidth, int newheight)2
 {
    {3
 Image iOriginal, iThumb;
        Image iOriginal, iThumb;4
 double scaleH, scaleW;
        double scaleH, scaleW;5

6
 Rectangle srcRect=new Rectangle();
        Rectangle srcRect=new Rectangle();7
 iOriginal = Image.FromStream(new MemoryStream(fullsize));
        iOriginal = Image.FromStream(new MemoryStream(fullsize));8
 scaleH = iOriginal.Height / newheight;
        scaleH = iOriginal.Height / newheight;9
 scaleW = iOriginal.Width / newwidth;
        scaleW = iOriginal.Width / newwidth;10
 if (scaleH == scaleW)
        if (scaleH == scaleW)11
 {
        {12
 srcRect.Width = iOriginal.Width;
            srcRect.Width = iOriginal.Width;13
 srcRect.Height = iOriginal.Height;
            srcRect.Height = iOriginal.Height;14
 srcRect.X = 0;
            srcRect.X = 0;15
 srcRect.Y = 0;
            srcRect.Y = 0;16
 }
        }17
 else if ((scaleH) > (scaleW))
        else if ((scaleH) > (scaleW))18
 {
        {19
 srcRect.Width = iOriginal.Width;
            srcRect.Width = iOriginal.Width;20
 srcRect.Height = Convert.ToInt32(newheight * scaleW);
            srcRect.Height = Convert.ToInt32(newheight * scaleW);21
 srcRect.X = 0;
            srcRect.X = 0;22
 srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
            srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);23
 }
        }24
 else
        else25
 {
        {26
 srcRect.Width = Convert.ToInt32(newwidth * scaleH);
            srcRect.Width = Convert.ToInt32(newwidth * scaleH);27
 srcRect.Height = iOriginal.Height;
            srcRect.Height = iOriginal.Height;28
 srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
            srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);29
 srcRect.Y = 0;
            srcRect.Y = 0;30
 }
        }31
 iThumb = new Bitmap(newwidth, newheight);
        iThumb = new Bitmap(newwidth, newheight);32
 Graphics g = Graphics.FromImage(iThumb);
        Graphics g = Graphics.FromImage(iThumb);33
 g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
        g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);34
 MemoryStream m = new MemoryStream();
        MemoryStream m = new MemoryStream();35
 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
        iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);36
 return m.GetBuffer();
        return m.GetBuffer();37
 }
    }38

具体代码就不一一繁述:):
贴出我的修改,应该有更好的设计,希望提出好的方案:
 1 public static byte[] MakeThumb2(byte[] fullsize, int newwidth, int newheight)
    public static byte[] MakeThumb2(byte[] fullsize, int newwidth, int newheight)
2 {
    {
3 int originalWidth;
        int originalWidth;
4 int originalHeight;
        int originalHeight;
5
6 int thumbWidth;
        int thumbWidth;
7 int thumbHeight;
        int thumbHeight;
8
9 using (MemoryStream tempStream = new MemoryStream(fullsize))
        using (MemoryStream tempStream = new MemoryStream(fullsize))
10 {
        {
11 Bitmap tempBitmap = new Bitmap(tempStream);
            Bitmap tempBitmap = new Bitmap(tempStream);
12
13 originalWidth = tempBitmap.Width;
            originalWidth = tempBitmap.Width;
14 originalHeight = tempBitmap.Height;
            originalHeight = tempBitmap.Height;
15 }
        }
16
17
18 double scaleH, scaleW;
        double scaleH, scaleW;
19
20 scaleH = originalHeight / newheight;
        scaleH = originalHeight / newheight;
21 scaleW = originalWidth / newwidth;
        scaleW = originalWidth / newwidth;
22
23 if (scaleH == scaleW)
        if (scaleH == scaleW)
24 {
        {
25 thumbWidth = newwidth;
            thumbWidth = newwidth;
26 thumbHeight = newheight;
            thumbHeight = newheight;
27 }
        }
28 else if ((scaleH) > (scaleW))
        else if ((scaleH) > (scaleW))
29 {
        {
30 thumbWidth = newwidth;
            thumbWidth = newwidth;
31 thumbHeight = Convert.ToInt32(originalHeight * newwidth / originalWidth);
            thumbHeight = Convert.ToInt32(originalHeight * newwidth / originalWidth);
32 }
        }
33 else
        else
34 {
        {
35 thumbWidth = Convert.ToInt32(originalWidth * newheight / originalHeight);
            thumbWidth = Convert.ToInt32(originalWidth * newheight / originalHeight);
36 thumbHeight = newheight;
            thumbHeight = newheight;
37 }
        }
38
39 Bitmap  iThumb = new Bitmap( thumbWidth , thumbHeight);
        Bitmap  iThumb = new Bitmap( thumbWidth , thumbHeight);
40 Graphics g = Graphics.FromImage(iThumb);
        Graphics g = Graphics.FromImage(iThumb);
41 g.DrawImage( Image.FromStream(new MemoryStream( fullsize )) , new Rectangle(0, 0, thumbWidth, thumbHeight));
        g.DrawImage( Image.FromStream(new MemoryStream( fullsize )) , new Rectangle(0, 0, thumbWidth, thumbHeight));
42 MemoryStream m = new MemoryStream();
        MemoryStream m = new MemoryStream();
43 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
        iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
44 return m.GetBuffer();
        return m.GetBuffer();
45 }
    }
 public static byte[] MakeThumb2(byte[] fullsize, int newwidth, int newheight)
    public static byte[] MakeThumb2(byte[] fullsize, int newwidth, int newheight)2
 {
    {3
 int originalWidth;
        int originalWidth;4
 int originalHeight;
        int originalHeight;5

6
 int thumbWidth;
        int thumbWidth;7
 int thumbHeight;
        int thumbHeight;8

9
 using (MemoryStream tempStream = new MemoryStream(fullsize))
        using (MemoryStream tempStream = new MemoryStream(fullsize))10
 {
        {11
 Bitmap tempBitmap = new Bitmap(tempStream);
            Bitmap tempBitmap = new Bitmap(tempStream);12

13
 originalWidth = tempBitmap.Width;
            originalWidth = tempBitmap.Width;14
 originalHeight = tempBitmap.Height;
            originalHeight = tempBitmap.Height;15
 }
        }16

17

18
 double scaleH, scaleW;
        double scaleH, scaleW;19

20
 scaleH = originalHeight / newheight;
        scaleH = originalHeight / newheight;21
 scaleW = originalWidth / newwidth;
        scaleW = originalWidth / newwidth;22

23
 if (scaleH == scaleW)
        if (scaleH == scaleW)24
 {
        {25
 thumbWidth = newwidth;
            thumbWidth = newwidth;26
 thumbHeight = newheight;
            thumbHeight = newheight;27
 }
        }28
 else if ((scaleH) > (scaleW))
        else if ((scaleH) > (scaleW))29
 {
        {30
 thumbWidth = newwidth;
            thumbWidth = newwidth;31
 thumbHeight = Convert.ToInt32(originalHeight * newwidth / originalWidth);
            thumbHeight = Convert.ToInt32(originalHeight * newwidth / originalWidth);32
 }
        }33
 else
        else34
 {
        {35
 thumbWidth = Convert.ToInt32(originalWidth * newheight / originalHeight);
            thumbWidth = Convert.ToInt32(originalWidth * newheight / originalHeight);36
 thumbHeight = newheight;
            thumbHeight = newheight;37
 }
        }38

39
 Bitmap  iThumb = new Bitmap( thumbWidth , thumbHeight);
        Bitmap  iThumb = new Bitmap( thumbWidth , thumbHeight);40
 Graphics g = Graphics.FromImage(iThumb);
        Graphics g = Graphics.FromImage(iThumb);41
 g.DrawImage( Image.FromStream(new MemoryStream( fullsize )) , new Rectangle(0, 0, thumbWidth, thumbHeight));
        g.DrawImage( Image.FromStream(new MemoryStream( fullsize )) , new Rectangle(0, 0, thumbWidth, thumbHeight));42
 MemoryStream m = new MemoryStream();
        MemoryStream m = new MemoryStream();43
 iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
        iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);44
 return m.GetBuffer();
        return m.GetBuffer();45
 }
    }这段代码是对
public enum ImageSizes
{
Large = 0,
Thumb = 1,
FullSize = 2
}
取值为 1 (即缩略图模式) 时的图像处理过程,还有另外两种模式,就由你来完善吧
 !
! 
                    
                     
                    
                 
                    
                
 
 
    
 
         
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号