欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#生成自定义图片方法代码实例

程序员文章站 2023-12-09 19:05:57
本篇文章给大家带来的内容是关于c# 如何生成自定义图片?c# 生成自定义图片方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 using sy...

本篇文章给大家带来的内容是关于c# 如何生成自定义图片?c# 生成自定义图片方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

using system.drawing;using system.io;using system.drawing.imaging;using system;namespace treads
{  /// <summary>
  /// 生成略缩图  /// </summary>
  public class class2
  {    private image srcimage;    private string srcfilename= @"x";//获取图片的路径
    private string srcfilename1 = @"x";//要保持图片的新路径
 
    /// <summary>
    /// 回调    /// </summary>
    /// <returns></returns>
    public bool thumbnailcallback()
    {      return false;
    }    /// <summary>
    /// 保存缩略图    /// </summary>
    /// <param name="width"></param>
    /// <param name="height"></param>
    public void savethumbnailimage(int width, int height)
    {      switch (path.getextension(srcfilename).tolower())
      {        case ".png":
          saveimage(width, height, imageformat.png);          break;        case ".gif":
          saveimage(width, height, imageformat.gif);          break;        default:
          saveimage(width, height, imageformat.jpeg);          break;
      }
    }    /// <summary>
    /// 生成缩略图并保存    /// </summary>
    /// <param name="width">缩略图的宽度</param>
    /// <param name="height">缩略图的高度</param>
    /// <param name="imgformat">保存的图像格式</param>
    /// <returns>缩略图的image对象</returns>
    public void saveimage(int width, int height, imageformat imgformat)
    {
      srcimage = image.fromfile(srcfilename);      if (imgformat != imageformat.gif && (srcimage.width > width) || (srcimage.height > height))
      {
        image img;
        image.getthumbnailimageabort callb = new image.getthumbnailimageabort(thumbnailcallback);
        img = srcimage.getthumbnailimage(width, height, callb, intptr.zero);
        srcimage.dispose();
        img.save(srcfilename1, imgformat);
        img.dispose();
      }
    }
 
  }
}

制作网络下载的略缩图

/// <summary>
   /// 制作远程缩略图    /// </summary>
   /// <param name="url">图片url</param>
   /// <param name="newfilename">新图路径</param>
   /// <param name="maxwidth">最大宽度</param>
   /// <param name="maxheight">最大高度</param>
   public static void makeremotethumbnailimage(string url, string newfilename, int maxwidth, int maxheight)
   {
     stream stream = getremoteimage(url);      if (stream == null)        return;
     image original = image.fromstream(stream);
     stream.close();
     makethumbnailimage(original, newfilename, maxwidth, maxheight);
   }  /// <summary>
   /// 获取图片流    /// </summary>
   /// <param name="url">图片url</param>
   /// <returns></returns>
   private static stream getremoteimage(string url)
   {
     httpwebrequest request = (httpwebrequest)httpwebrequest.create(url);
     request.method = "get";
     request.contentlength = 0;
     request.timeout = 20000;
     httpwebresponse response = null;      try
     {
       response = (httpwebresponse)request.getresponse();        return response.getresponsestream();
     }      catch
     {        return null;
     }
   } /// <summary>
   /// 裁剪图片并保存    /// </summary>
   /// <param name="filename">源图路径(绝对路径)</param>
   /// <param name="newfilename">缩略图路径(绝对路径)</param>
   /// <param name="maxwidth">缩略图宽度</param>
   /// <param name="maxheight">缩略图高度</param>
   /// <param name="cropwidth">裁剪宽度</param>
   /// <param name="cropheight">裁剪高度</param>
   /// <param name="x">x轴</param>
   /// <param name="y">y轴</param>
   public static bool makethumbnailimage(string filename, string newfilename, int maxwidth, int maxheight, int cropwidth, int cropheight, int x, int y)
   {      byte[] imagebytes = file.readallbytes(filename);
     image originalimage = image.fromstream(new system.io.memorystream(imagebytes));
     bitmap b = new bitmap(cropwidth, cropheight);      try
     {        using (graphics g = graphics.fromimage(b))
       {          //设置高质量插值法
         g.interpolationmode = interpolationmode.highqualitybicubic;          //设置高质量,低速度呈现平滑程度
         g.smoothingmode = smoothingmode.antialias;
         g.pixeloffsetmode = pixeloffsetmode.highquality;          //清空画布并以透明背景色填充          g.clear(color.transparent);          //在指定位置并且按指定大小绘制原图片的指定部分
         g.drawimage(originalimage, new rectangle(0, 0, cropwidth, cropheight), x, y, cropwidth, cropheight, graphicsunit.pixel);
         image displayimage = new bitmap(b, maxwidth, maxheight);
         saveimage(displayimage, newfilename, getcodecinfo("image/" + getformat(newfilename).tostring().tolower()));          return true;
       }
     }      catch (system.exception e)
     {        throw e;
     }      finally
     {
       originalimage.dispose();
       b.dispose();
     }
   }

以上就是对c# 如何生成自定义图片?c# 生成自定义图片方法的全部介绍,感谢大家对的支持。