C#里如何让尺寸太大的图片自动缩小?

来源:互联网 发布:淘宝手机端链接在哪里 编辑:程序博客网 时间:2024/04/30 06:03

我这样写的:  
  <asp:Image   id="pic"   runat="server"   onload="javascript:if(this.width>450){this.width=450;}">  
   
  它报:编译器错误信息:   CS1026:   应输入   )  
  问题点数:50、回复次数:11Top

1 楼Meyer(断水)回复于 2004-07-18 16:40:42 得分 1

你控制Image的大小就可以了,如果你只控制他的高或者宽  
  回自动按比例缩小的Top

2 楼hnliruoyu(生活了无滋味)回复于 2004-07-18 16:45:36 得分 6

private   void   GetZoomedLogo(System.IO.Stream   originLogo,   int   width,   int   height,System.IO.Stream   outputStream)  
  {  
  int   drawWidth   =   0,   drawHeight   =   0;  
   
  System.Drawing.Image   origin   =   System.Drawing.Image.FromStream(originLogo);  
   
  if   (width   ==   0   &&   height   ==   0) //not   designate   width   and   height  
  {  
  width   =   origin.Width;  
  height   =   origin.Height;  
  }  
   
  if   (origin.Width   <   width   &&   origin.Height   <   height) //not   zoom   in  
  {  
  drawWidth   =   origin.Width;  
  drawHeight   =   origin.Height;  
  }  
  else  
  {  
  double   scaleX   =   (double)origin.Width   /   (double)width;  
  double   scaleY   =   (double)origin.Height   /   (double)   height;  
   
  double   scale   =   scaleX   >   scaleY   ?   scaleX   :   scaleY;  
  drawWidth   =   Convert.ToInt32(origin.Width   /   scale);  
  drawHeight   =   Convert.ToInt32(origin.Height   /   scale);  
  }  
  System.Drawing.Rectangle   destRect   =   new   Rectangle((width   -   drawWidth)   /   2,   (height   -   drawHeight)   /   2,   drawWidth,   drawHeight);  
   
  Bitmap   temp   =   new   Bitmap(width,   height);  
  Graphics   g   =   Graphics.FromImage(temp);  
   
  SolidBrush   brush   =   new   SolidBrush(Color.White);  
  g.FillRectangle(brush,   0,   0,   width,   height); //draw   background  
  // g.DrawLine(new   Pen(Color.Red,   10),   0,   0,   20,   20);  
   
  if   (width   >=   300   ||   height   >=   300)  
  {  
  g.DrawImage(origin,   destRect);  
  }  
  else  
  {  
  System.Drawing.Image   thumbnail   =   origin.GetThumbnailImage(drawWidth,   drawHeight,   null,   System.IntPtr.Zero);  
  g.DrawImage(thumbnail,   destRect);  
  }  
   
  temp.Save(outputStream,   System.Drawing.Imaging.ImageFormat.Jpeg);  
   
  origin.Dispose();  
  temp.Dispose();  
  g.Dispose();  
  }Top

3 楼ahfu(阿付)回复于 2004-07-18 16:45:54 得分 0

我在codebehind里这样写:  
  if   (pic.width.value   >   450)  
    {  
        pic.width   =   450;  
    }  
   
  但不行啊!!Top

4 楼hnliruoyu(生活了无滋味)回复于 2004-07-18 16:47:07 得分 0

originLogo=new   FileStream(file,   FileMode.Open,   FileAccess.Read);Top

5 楼hnliruoyu(生活了无滋味)回复于 2004-07-18 16:48:55 得分 0

请调用我的方法  
  GetZoomedLogo(fs,   width,   height,   hr.OutputStream);  
  其中,originLogo=new   FileStream(file,   FileMode.Open,   FileAccess.Read);  
   
  width为宽,height高  
  System.Web.HttpResponse   hrTop

6 楼sjzwinfor(蜘蛛侠)回复于 2004-07-18 16:56:05 得分 2

image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))  
  width=image.Width  
  height=image.height;  
  if(   width>height)  
  {  
  newwidth=250;  
  newheight=image.height/image.Width*newwidth;}  
  else  
  {  
  newheight=250;  
  newwidth=image.Width/image.height*newheight;}  
  Top

7 楼johnsuna(缘来是e)回复于 2004-07-18 17:03:49 得分 1

一般使用JavaScript控制就可以了,不需要使用C#。  
  如果是在ASP.Net2.0中,可以使用<asp:DynamicImage>控件(作用是动态输出图片)。Top

8 楼athossmth(athos)回复于 2004-07-18 17:11:59 得分 40

void   Page_Load(object   sender,   EventArgs   e)  
  {  
  pic.Attributes.Add("onload",   "javascript:if(this.width>450){this.width=450;}"   );  
  }Top

9 楼athossmth(athos)回复于 2004-07-18 17:12:43 得分 0

然后吧  
  <asp:Image   id="pic"   runat="server"   onload="javascript:if(this.width>450){this.width=450;}">  
   
  改成  
   
  <asp:Image   id="pic"   runat="server"   ImageUrl="a.jpg"></asp:Image>Top

10 楼ahfu(阿付)回复于 2004-07-18 17:38:06 得分 0

不行@_@Top

11 楼devfan(小星)回复于 2004-07-18 17:45:00 得分 0

可以生成缩略图  
   
  const   int   MaxLength=150;     //最大长度  
   
  private   void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  if   (Request.QueryString["filename"]   !=   null)  
  {  
  //取得原图  
  string   filename=Request.QueryString["filename"];  
  Bitmap   bmpOld=   new   Bitmap(Server.MapPath("images/"   +   filename));    
   
  //计算缩小比例  
  double   d1;  
  if   (bmpOld.Height>bmpOld.Width)  
  d1=(double)(MaxLength/(double)bmpOld.Width);  
  else  
  d1=(double)(MaxLength/(double)bmpOld.Height);  
   
  //产生缩图  
  Bitmap   bmpThumb=   new   Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1));  
   
  //   清除缓冲    
  Response.Clear();  
  //生成图片  
      bmpThumb.Save(Response.OutputStream,   ImageFormat.Jpeg);  
  Response.End();  
  //释放资源  
  bmpThumb.Dispose();  
  bmpOld.Dispose();  
  }  
  }  
   
  然后可以用URL引用这个页面动态生成缩略图  
  <asp:Image   id="pic"   runat="server"   ImageUrl="xxx.aspx?pic=a.jpg"></asp:Image>