服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现在底图上动态生成文字和图片

C#实现在底图上动态生成文字和图片

2022-07-24 17:44寒冰屋 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// <summary>
/// 在底图上画指定路径的图片
/// </summary>
/// <param name="g">画板实例</param>
/// <param name="path">图片路径</param>
/// <param name="totalWidth">画区总长度</param>
/// <param name="totalHeight">画区总高度</param>
/// <param name="px">起点X坐标</param>
/// <param name="py">起点Y坐标</param>
  private void FontPic(ref Graphics g, string path, int totalWidth, int totalHeight, int px, int py)
  {
   if (File.Exists(path))
   {
    var pImg = Image.FromFile(path);
    //如果图片大于画布区域,则缩小
    if (totalHeight < pImg.Height && totalWidth < pImg.Width)
    {
     Image newPic = GetReducedImage(pImg, totalWidth, totalHeight);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else if (totalHeight < pImg.Height && totalWidth >= pImg.Width)
    {
     Image newPic = GetReducedImage(pImg, pImg.Width, totalHeight);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else if (totalHeight >= pImg.Height && totalWidth < pImg.Width)
    {
     Image newPic = GetReducedImage(pImg, totalWidth, pImg.Height);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else
    {
     DrawPic(ref g, totalWidth, totalHeight, px, py, pImg);
    }
   }
  }
  /// <summary>
  /// 在图上画图片
  /// </summary>
  /// <param name="g">画板实例</param>
  /// <param name="totalWidth">画区总长度</param>
  /// <param name="totalHeight">画区总高度</param>
  /// <param name="px">起点X坐标</param>
  /// <param name="py">起点Y坐标</param>
  /// <param name="pImg">要画的图片实例</param>
  private void DrawPic(ref Graphics g, int totalWidth, int totalHeight, int px, int py, Image pImg)
  {
   px += GetValue(totalWidth, pImg.Width);
   py += GetValue(totalHeight, pImg.Height);
 
   g.DrawImage(new Bitmap(pImg, new Size(GetSize(totalWidth, pImg.Width), GetSize(totalHeight, pImg.Height))),
    new Rectangle(px, py, totalWidth, totalHeight),
    0, 0, totalWidth, totalHeight, GraphicsUnit.Pixel);
  }
  /// <summary>
  /// 生成缩略图重载方法1,返回缩略图的Image对象
  /// </summary>
  /// <param name="width">缩略图的宽度</param>
  /// <param name="height">缩略图的高度</param>
  /// <returns>缩略图的Image对象</returns>
  public Image GetReducedImage(Image resourceImage, int width, int height)
  {
   try
   {
    Image data = null;
    //用指定的大小和格式初始化Bitmap类的新实例
    using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
    {
     //从指定的Image对象创建新Graphics对象
     using (Graphics graphics = Graphics.FromImage(bitmap))
     {
      //清除整个绘图面并以透明背景色填充
      //graphics.Clear(Color.Transparent);
      //在指定位置并且按指定大小绘制原图片对象
      graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
     }
     data = new Bitmap(bitmap);
    }
    return data;
   }
   catch (Exception e)
   {
    throw e;
   }
  }
  /// <summary>
  /// 比较两个值,得到给到给定值(判断是否越界)
  /// </summary>
  /// <param name="total">总长度</param>
  /// <param name="width">指定长度</param>
  /// <returns></returns>
  public int GetSize(int total, int width)
  {
   if (total > width)
   {
    return width;
   }
   else
   {
    return total;
   }
  }
  /// <summary>
  /// 更加传入的值计算得到新值(计算点坐标)
  /// </summary>
  /// <param name="total">总长度</param>
  /// <param name="width">指定长度</param>
  /// <returns></returns>
  private int GetValue(int total, int width)
  {
   return (total - width) / 2;
  }
  /// <summary>
  /// 在图片上画出文字
  /// </summary>
  /// <param name="g">图片对象</param>
  /// <param name="pointX">文字x坐标</param>
  /// <param name="pointY">文字y坐标</param>
  /// <param name="word">文字内容</param>
  /// <param name="textWidth">文本宽度</param>
  /// <param name="textHeight">文本高度</param>
  private static void DrawStringWord(Graphics g, int pointX, int pointY, string word, int textWidth, int textHeight, int fontSize = 30)
  {
   Font font = new Font("微软雅黑", fontSize, (FontStyle.Regular));
   RectangleF textArea = new RectangleF(pointX, pointY, textWidth, textHeight);
   Brush brush = new SolidBrush(Color.Black);
   g.DrawString(word, font, brush, textArea);
  }

希望对需要这方面操作的朋友有所帮助。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/mzl87/article/details/81877939

延伸 · 阅读

精彩推荐
  • C#C#使用Redis的基本操作

    C#使用Redis的基本操作

    这篇文章主要介绍了C#使用Redis的基本操作,需要的朋友可以参考下...

    请叫我小冯哥哥3782022-01-11
  • C#c#中SqlTransaction——事务详解

    c#中SqlTransaction——事务详解

    这篇文章主要介绍了c#中SqlTransaction——事务详解 ,具有一定的参考价值,有兴趣的可以了解一下。...

    停留的风4452021-12-11
  • C#详解MongoDB for C#基础入门

    详解MongoDB for C#基础入门

    本篇文章主要介绍了MongoDB for C#基础入门,具体介绍了c#中关于对MongoDB的连接,插入,查询等,有需要的可以了解一下。...

    y-z-f7472021-12-14
  • C#利用C#编写Linux守护进程实例代码

    利用C#编写Linux守护进程实例代码

    如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件,下面这篇文章主要给大家介绍了关于利用C#编写Linux守护进程的相关...

    Chaunce7282022-02-19
  • C#C#中的多线程小试牛刀

    C#中的多线程小试牛刀

    这篇文章主要给大家介绍了关于C#中多线程的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们...

    BUTTERAPPLE8422022-07-22
  • C#Winform中进行MD5加密的实例

    Winform中进行MD5加密的实例

    下面小编就为大家带来一篇Winform中进行MD5加密的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7132021-12-21
  • C#C# 设计模式系列教程-简单工厂模式

    C# 设计模式系列教程-简单工厂模式

    简单工厂模式职责单一,实现简单,且实现了客户端代码与具体实现的解耦。...

    Wang Juqiang9822021-11-23
  • C#非常实用的C#字符串操作处理类StringHelper.cs

    非常实用的C#字符串操作处理类StringHelper.cs

    这篇文章主要为大家详细介绍了非常实用的C#字符串操作处理类StringHelper.cs,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    sanler7792022-01-17