华域联盟 .Net c#生成缩略图不失真的方法实例分享

c#生成缩略图不失真的方法实例分享

复制代码 代码如下:

/// <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#批量上传图片到服务器示例分享
  • C# 最齐全的上传图片方法
  • C#保存图片到数据库并读取显示图片的方法
  • C#中按指定质量保存图片的实例代码
  • C# 手动/自动保存图片的实例代码
  • c#读取图像保存到数据库中(数据库保存图片)
  • C#给图片添加水印完整实例
  • C#给图片加水印的简单实现方法
  • c#多图片上传并生成缩略图的实例代码
  • c#生成缩略图的实现方法
  • C#实现的上传图片、保存图片、加水印、生成缩略图功能示例

本文由 华域联盟 原创撰写:华域联盟 » c#生成缩略图不失真的方法实例分享

转载请保留出处和原文链接:https://www.cnhackhy.com/53253.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部