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

解决方案 »

  1.   

    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();
    }
      

  2.   

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

  3.   

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

  4.   

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

  5.   

    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;}
      

  6.   

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

  7.   

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

  8.   

    然后吧
    <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>
      

  9.   

    可以生成缩略图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>