C#里如何让尺寸太大的图片自动缩小? 你控制Image的大小就可以了,如果你只控制他的高或者宽回自动按比例缩小的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); } 我在codebehind里这样写:if (pic.width.value > 450) { pic.width = 450; }但不行啊!! originLogo=new FileStream(file, FileMode.Open, FileAccess.Read); 请调用我的方法GetZoomedLogo(fs, width, height, hr.OutputStream);其中,originLogo=new FileStream(file, FileMode.Open, FileAccess.Read);width为宽,height高System.Web.HttpResponse hr image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))width=image.Widthheight=image.height;if( width>height){newwidth=250;newheight=image.height/image.Width*newwidth;}else{newheight=250;newwidth=image.Width/image.height*newheight;} 一般使用JavaScript控制就可以了,不需要使用C#。如果是在ASP.Net2.0中,可以使用<asp:DynamicImage>控件(作用是动态输出图片)。 void Page_Load(object sender, EventArgs e) { pic.Attributes.Add("onload", "javascript:if(this.width>450){this.width=450;}" ); } 然后吧<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> 可以生成缩略图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); elsed1=(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> sql语句中的一个小疑问 大家帮我看一下。这句代码错在哪里?谢谢!! web.config中的数据库连接串的问题 如何在asp.net 服务器端获取屏幕分辨率 TreeView1_Data未定义的问题。 2.0怎么建立框架? 请问一个文件上传的问题 请问我想把我的软件放到我的网叶里使用,意思是打开网叶不用安装软件就向访问网叶一样能够用我的软件该怎样实现。 快拿分!!! 有没有办法不用VS03编译生成项目? asp转asp.net难不难? 请教:请大家推荐一下学习asp.net在vs.net环境下什么书比较好!非常感谢!
{
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();
}
if (pic.width.value > 450)
{
pic.width = 450;
}但不行啊!!
GetZoomedLogo(fs, width, height, hr.OutputStream);
其中,originLogo=new FileStream(file, FileMode.Open, FileAccess.Read);width为宽,height高
System.Web.HttpResponse hr
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;}
如果是在ASP.Net2.0中,可以使用<asp:DynamicImage>控件(作用是动态输出图片)。
{
pic.Attributes.Add("onload", "javascript:if(this.width>450){this.width=450;}" );
}
<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>
{
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>