手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >c#生成缩略图不失真的方法实例分享
c#生成缩略图不失真的方法实例分享
摘要:复制代码代码如下://////获得缩微图//////publicboolGetThumbImg(){try{stringimgpath;//...

复制代码 代码如下:

/// <summary>

/// 获得缩微图

/// </summary>

/// <returns></returns>

public bool GetThumbImg()

{

try

{

string imgpath; //原始路径

if(imgsourceurl.IndexOf("",0)<0) //使用的是相对路径

{

imgpath = HttpContext.Current.Server.MapPath(imgsourceurl); //转化为物理路径

}

else

{

imgpath=imgsourceurl;

}

System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(imgpath);

int width = sourceImage.Width;

int height = sourceImage.Height;

if(thumbwidth <= 0)

{

thumbwidth = 120;

}

if(thumbwidth >= width)

{

return false;

}

else

{

(thumbwidth,thHeight*thumbwidth/thWidth,null,IntPtr.Zero);

Image imgThumb=new System.Drawing.Bitmap(thumbwidth,height*thumbwidth/width);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

g.DrawImage(sourceImage, new Rectangle(0, 0, thumbwidth,height*thumbwidth/width), 0, 0, width, height, GraphicsUnit.Pixel);

string thumbpath="";

sourceImage.Dispose();

if(thumburl=="")

{

thumbpath=imgpath;

}

if(thumbpath.IndexOf("",0)<0)//使用的是相对路径

{

thumbpath=HttpContext.Current.Server.MapPath(thumburl);//转化为物理路径

}

imgThumb.Save(thumbpath,ImageFormat.Jpeg);

imgThumb.Dispose();

return true;

}

}

catch

{

throw;

}

}

【c#生成缩略图不失真的方法实例分享】相关文章:

asp.net下生成99个不同的随机数

ASP.NET中 Execl导出的六种方法实例

asp.net字符串分割函数使用方法分享

上传图片后使用数据库保存图片的示例分享

C# 无限级分类的实现

c#生成缩略图不失真的方法

asp.net模板引擎Razor调用外部方法用法实例

C# 文件上传 默认最大为4M的解决方法

ASP.NET 输出缓存移除的实例代码

asp.net读取excel文件的三种方法示例

精品推荐
分类导航