ホームページ  >  記事  >  バックエンド開発  >  C#で歪みのないサムネイルを生成するメソッドの共有例

C#で歪みのないサムネイルを生成するメソッドの共有例

黄舟
黄舟オリジナル
2017-01-19 10:41:541303ブラウズ

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# で歪みのないサムネイルを生成する方法の例の共有です。 PHP 中国語 Web サイト (www.php .cn)!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。