asp.net图片上传生成缩略图的注意事项
所属分类:
网络编程 / ASP.NET
阅读数:
551
收藏 0赞 0分享
bitmap.Save(imgPath,ImageFormat.Jpeg);
//这是保存缩略图的一段代码,其中的ImageFormat.Jpeg一定不能省略,即使你保存的文件本来就是jpg格式的,也不能去掉。因为如果去掉的话,生成的缩略图比原始图片还要大!
//另外,imgPath必须首先创建,否则会产生GDI+的一般性错误。
path=System.Web.HttpContext.Current.Server.MapPath(path);
使用if(!System.IO.Directory.Exists(path))System.IO.Directiory.CreateDirectory(path);
//生成缩略图,不要使用GetThumbnailImage方法,这个方法产生的缩略图质量奇差无比,不能使用!
//简单代码如下:
string path=System.Web.HttpContext.Current.Server.MapPath(strpath);
sourcePath=System.Web.HttpContext.Current.Server.MapPath(sourcePath);
if(!System.IO.Directory.Exists(path))System.IO.Directory.CreateDirectory(path);
string sourceImage =sourcePath + fileName;
string thumbImage = path + fileName;
//原图(引用)
Image img=Image.FromFile(sourceImage,true);
//实际缩略图大小
System.Drawing.Size size=Pic.ImgSize(maxW,maxH,img.Width,img.Height);
int w=size.Width;
int h=size.Height;
//绘制缩略图
Bitmap bitmap=new Bitmap(w,h);
Graphics g=Graphics.FromImage(bitmap);
//设定缩略图呈现质量
g.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//清洁背景
g.Clear(Color.White);
Rectangle thumbRect=new Rectangle(0,0,w,h);
g.DrawImage(img,thumbRect);
//保存缩略图
bitmap.Save(thumbImage,ImageFormat.Jpeg);
//释放内存
bitmap.Dispose();
img.Dispose();
g.Dispose();
ASP.NET中获取URL重写前的原始地址详解
在ASP.NET中,如果你使用了URL重写,通过HttpContext.Request获取到的是重写后的地址。如果这个地址要返回给客户端(比如Redirect),我们一般希望是重写前的友好地址。
收藏 0赞 0分享
用WPF实现屏幕文字提示的实现方法
本文介绍WPF应用程序实现在屏幕上显示一行或多行文字通知。它没有标题栏和最大化最小化等按钮,可以有半透明背景以使文字的显示更清晰,鼠标点击后提示消失。
收藏 0赞 0分享
查看更多