服务器之家

服务器之家 > 正文

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

时间:2022-03-02 14:21     来源/作者:C#教程网

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
      }
    }
 
  }
}

制作网络下载的略缩图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// <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# 生成自定义图片方法的全部介绍,感谢大家对服务器之家的支持。

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部