<script language="javascript" type="text/javascript"> 
<!-- 
// 说明:用 JavaScript 实现网页图片等比例缩放 
function DrawImage(ImgD,FitWidth,FitHeight) 

var image=new Image(); 
image.src=ImgD.src; 
if(image.width>0 && image.height>0) 

if(image.width/image.height>= FitWidth/FitHeight) 

if(image.width>FitWidth) 

ImgD.width=FitWidth; 
ImgD.height=(image.height*FitWidth)/image.width; 

else 

ImgD.width=image.width; 
ImgD.height=image.height; 


else 

if(image.height>FitHeight) 

ImgD.height=FitHeight; 
ImgD.width=(image.width*FitHeight)/image.height; 

else 

ImgD.width=image.width; 
ImgD.height=image.height; 




//--> 
<script> 
调用方式: 
<img src="1148202890.jpg" alt="自动缩放后的效果" onload="javascript:DrawImage(this,200,200);" /> 
如果图片较大,建议在图片标签里面同时设置期望的图片大小,这样不会导致页面在加载中撑开,该大小不会影响最终缩放效果。
可以修改上面的代码为: 
<img src="1148202890.jpg" alt="自动缩放后的效果" width="200" height="200" onload="javascript:DrawImage(this,200,200);" /> 
上面的这段代码,在IE6 7中显示的图片显小 在IE8 GG FF浏览器中都显示正常 请问是怎么回事?我想做的功能是在发帖时复制过来的图片,发完显示帖子后.都按比例缩放,onload="javascript:DrawImage(this,200,200);"  这段没地方加的啊???

解决方案 »

  1.   

            public void GetThumbnailImage(string picFilePath, int width, int height)
            {
                //添加small100的前缀大小
           //程序内相对的服务器路径小图片
                string savepath = "~/enterpriseImage/IntegralImg/Comm/";
                string strRelativeSmallPath = savepath + "001";            string SmallPath100 = Server.MapPath(strRelativeSmallPath + picFilePath);
                //string SmallPath100 = GetUrl();
                string machpath =Server.MapPath(savepath+picFilePath);
                string isDir = SmallPath100.Substring(0,SmallPath100.LastIndexOf('\\'));
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(isDir);
                if (!di.Exists)
                {
                    di.Create();
                }
                Bitmap img = new Bitmap(machpath);  //read picture to memory            int h = img.Height;
                int w = img.Width;
                int ss, os;// source side and objective side
                double temp1, temp2;
                //compute the picture's proportion
                temp1 = (h * 1.0D) / height;
                temp2 = (w * 1.0D) / width;
                if (temp1 < temp2)
                {
                    ss = w;
                    os = width;
                }
                else
                {
                    ss = h;
                    os = height;
                }            double per = (os * 1.0D) / ss;
                if (per < 1.0D)
                {
                    h = (int)(h * per);
                    w = (int)(w * per);
                }
                System.Drawing.Image.GetThumbnailImageAbort myCallback =
          new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                System.Drawing.Image imag2 = img.GetThumbnailImage(w, h, ThumbnailCallback, IntPtr.Zero);
                Bitmap tempBitmap = new Bitmap(w, h);
                System.Drawing.Image tempImg = System.Drawing.Image.FromHbitmap(tempBitmap.GetHbitmap());
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempImg);
                g.Clear(Color.White);            int x, y;            x = (tempImg.Width - imag2.Width) / 2;
                y = (tempImg.Height - imag2.Height) / 2;            g.DrawImage(imag2, x, y, imag2.Width, imag2.Height);            try
                {
                    if (img != null)
                        img.Dispose();
                    if (imag2 != null)
                        imag2.Dispose();
                    if (tempBitmap != null)
                        tempBitmap.Dispose();                string fileExtension = System.IO.Path.GetExtension(machpath).ToLower(); 
                    //按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.  
                    switch (fileExtension)
                    {
                        case ".gif": tempImg.Save(SmallPath100, ImageFormat.Gif); break;
                        case ".jpg": tempImg.Save(SmallPath100, ImageFormat.Jpeg); break;
                        case ".bmp": tempImg.Save(SmallPath100, ImageFormat.Bmp); break;
                        case ".png": tempImg.Save(SmallPath100, ImageFormat.Png); break;
                    }  
                                  
                }
                catch
                {
                    throw new Exception("生成缩微图失败");
                }
                finally
                {
                    //释放内存
                    if (tempImg != null)
                        tempImg.Dispose();
                    if (g != null)
                        g.Dispose();
                }
            }
      

  2.   

    <script type="text/javascript">
    function DrawImage(ImgD,FitWidth,FitHeight) 

    var image=new Image(); 
    image.src=ImgD.src; 
    if(image.width>0 && image.height>0) 

       if(image.width/image.height>= FitWidth/FitHeight) 
       { 
        if(image.width>FitWidth) 
        { 
         ImgD.width=FitWidth; 
         ImgD.height=(image.height*FitWidth)/image.width; 
        } 
        else 
        { 
         ImgD.width=image.width; 
         ImgD.height=image.height; 
        } 
       } 
        else 
        { 
        if(image.height>FitHeight) 
        { 
         ImgD.height=FitHeight; 
         ImgD.width=(image.width*FitHeight)/image.height; 
        } 
        else 
        { 
         ImgD.width=image.width; 
         ImgD.height=image.height; 
        } 
        } 

    } </script> <img src='' onload="javascript:DrawImage(this,150,120);">