请问我在数据库读取一段html,里面有文本,图片等等。
我怎么判断里面的图片的尺寸,然后把大于某尺寸的图片设置为某个比较小的尺寸呢??
谢谢~~~

解决方案 »

  1.   

    等比例缩放图片
    <script language="JavaScript">
    var flag=false;
    function DrawImage(ImgD,iwidth,iheight){
      var image=new Image();
      image.src=ImgD.src;
      if(image.width>0 && image.height>0){
      flag=true;
      if(image.width/image.height>= iwidth/iheight){
      if(image.width>iwidth){   
      ImgD.width=iwidth;
      ImgD.height=(image.height*iwidth)/image.width;
      }else{
      ImgD.width=image.width;   
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }
      else{
      if(image.height>iheight){   
      ImgD.height=iheight;
      ImgD.width=(image.width*iheight)/image.height;   
      }else{
      ImgD.width=image.width;   
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }  }
    }   
      

  2.   


    //通过正则获取图片地址
    Regex regImg = new Regex(@"(?i)<img[^>]*?src=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
    MatchCollection mc = regImg .Matches(html);
    foreach(Match m in mc)
    {
       string str = m.Groups["src"].Value
       //获取地址后用FileInfo f = new FileInfo(server.mappath(str))
       f.length;//大小

      

  3.   


    if(f.length>//多少的时候)
    {
      Image img = Image.FromFile(Server.mappath(str));
      Bitmap bit = new Bitmap(指定的宽,指定的高);
      Graphics g = Graphics.FromImage(bit);
      g.DrawImage(img,//...)
      bit.Save(Servr.mappath(str), System.Drawing.Imaging.ImageFormat.Jpeg)//在把原图覆盖掉
    }
      

  4.   

    <script language="JavaScript">
    var flag=false;
    function DrawImage(ImgD,iwidth,iheight){
      flag=true;
      if(image.width/image.height>= iwidth/iheight){
      if(image.width>iwidth){   
      ImgD.width=iwidth;
      ImgD.height=(image.height*iwidth)/image.width;
      }else{
      ImgD.width=image.width;  
      

  5.   

    这个不错。。但是好像这样是根据文件大小判断吧:f.length>//多少的时候
    我想根据图片长宽尺寸判断,然后大于某个长度或宽度图片就等比例缩小可以么?
      

  6.   

    Regex regImg = new Regex(@"(?i)<img[^>]*?src=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
    MatchCollection mc = regImg .Matches(html);
    foreach(Match m in mc)
    {
       string str = m.Groups["src"].Value
       //长宽地址也差不多
       System.Drawing.Image img = System.Drawing.FromFile(Server.mappath(str));
       int width = img.Width;
       int height = img.Height;
       if(width>200)
          width=200;
       if(height>200)
          height=200;
       BitMap bitmap = new Bitmap(200,200);
       Graphics g = Graphics.FromImage(bitmap);
       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
       g.Clear(Color.White);
       g.Drawimage(img,new Rectangle(0,0,200,200),New Rectangle(0,0.width,height)); 
       bitmap.Save(Server.Mappath(str)) ;
      

  7.   


    Regex regImg = new Regex(@"(?i)<img[^>]*?src=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
    MatchCollection mc = regImg .Matches(html);
    foreach(Match m in mc)
    {
      string str = m.Groups["src"].Value
      //长宽地址也差不多
      System.Drawing.Image img = System.Drawing.FromFile(Server.mappath(str));
      int width = img.Width;
      int height = img.Height;
      if(width>200)
      width=200;
      if(height>200)
      height=200;
      BitMap bitmap = new Bitmap(200,200);
      Graphics g = Graphics.FromImage(bitmap);
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
      g.Clear(Color.White);
      g.Drawimage(img,new Rectangle(0,0,200,200),New Rectangle(0,0.width,height));  
      bitmap.Save(Server.Mappath(str)) ;
    }  
      

  8.   

    先用正则取到img中的src,然后
    Image img = Image.FromFile(src);
    再通过img.Height和img.Width判断
      

  9.   

    用css实现<style>
    img
    {width:expression(this.offsetWidth>this.parentNode.offsetWidth?this.parentNode.offsetWidth:this.offsetWidth)}
    </style>