把最近在做的项目的相关知识进行分享,也为以后在用到时可以及时的找到~~

此篇主要记录在图片上动态的生成需要添加的文字和把指定的图片加到底图上,不多废话,直接上代码

        /// <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);}

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

C# -- 在底图上动态生成文字和图片相关推荐

  1. php多图片生成flash,PHP_为php4加入动态flash文件的生成的支持,想象过在网站上动态生成flash - phpStudy...

    为php4加入动态flash文件的生成的支持 想象过在网站上动态生成flash动画吗,本文就让你步入php+flash的激动时代. php真是个好东西呀,真是无法预料她明天会变的怎样...好了,言归正 ...

  2. 复制那些复制不了的屏幕上的网页文字和图片文字

    复制那些复制不了的屏幕上的网页文字和图片文字 在平常工作学习中,经常会遇到一些图片上的.网站上的文字不能复制,让人束手无策.我们习惯的做法是一个一个的将字按照屏幕上显示的,一点点重新输入一遍,这既浪费 ...

  3. android 图片上动态添加文字,摘抄 android图片中添加文字水印

    * 添加文字到图片,类似水印文字. * @param gContext * @param gResId * @param gText * @return */ public static Bitmap ...

  4. java生成文字与图片_java生成word(文字和图片)

    1.整体思路 利用xml模板,在模板中预留占位标识(${yourContent}),然后将xml转为ftl文件,通过Map传值填充对应的内容即可,word其实和html一样,也有自己的xml标签,表头 ...

  5. Java-BufferedImage动态生成一个表格图片

    需求:一个word模板,里边有一个表格,需要根据需求动态填充数据,需要转成pdf,但是我发现转成pdf的方式并不方便,想着还不如将动态的部分生成图片,然后由前端整合其他部分用于展示,也满足了需求. w ...

  6. html创建字母头像,JavaScript动态生成文字头像

    html> //引入jQuery //图像显示容器,使用css控制样式,这里我写的圆形 //绘图元素 //js核心代码 js代码如下: $(function() { textToImg(&quo ...

  7. java生成word 带表格_【java】Freemarker 动态生成word(带图片表格)

    1.添加freemarker.jar 到java项目. 2.新建word文档. 3.将文档另存为xml 格式. 4.将xml格式化后打开编辑(最好用notepad,有格式),找到需要替换的内容,将内容 ...

  8. java生成word 可变表格_【java】Freemarker 动态生成word(带图片表格)

    1.添加freemarker.jar 到java项目. 2.新建word文档. 3.将文档另存为xml 格式. 4.将xml格式化后打开编辑(最好用notepad,有格式),找到需要替换的内容,将内容 ...

  9. 在QLabel上同时显示文字和图片的方法

    有两种方法. 1.打开UI文件,在界面右键单击QLabel对象,选改变多信息文本 选择图片再确定,左侧问号就是图片. 2.直接在QLabel写富文本 <html><head/> ...

最新文章

  1. Java 输入流与输出流
  2. R package XML安装
  3. UVa133 - The Dole Queue
  4. POI操作Excel:cell的背景颜色类型
  5. mysql数据库引擎博客_mysql 数据库引擎常识全集
  6. leetcode 676. Implement Magic Dictionary | 676. 实现一个魔法字典(DFS+Trie 前缀树)
  7. abap调用Linux命令,ABAP中输入write命令使用
  8. 投影串口测试程序_串口测试方法和步骤
  9. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
  10. 翻译 - 【Dojo Tutorials】Getting Started with dojox/app
  11. [node.js]小释
  12. C#调用C++编写的COM DLL
  13. JQuery之拖拽插件
  14. RtlZeroMemory
  15. 机器人摘果子看图写话_作文:摘苹果(看图写话)
  16. Mac 常用软件推荐
  17. pfv和php,【Retina China 2019】工欲善其事必先利其器——影像学在眼底病中的应用与发展...
  18. ubuntu 16换源方法
  19. reactrouter4路由钩子_React-Router4.x中文文档
  20. 华为ensp搭建习题

热门文章

  1. python列表的特点_python基础(一)列表、集合和元组的基本特性
  2. 电商年底年终促销页面,这种创意你收好
  3. 淘宝设计万能PSD分层模板(简约页面设计——少即是多)
  4. win7计算机文件夹打开慢,win7开机很慢怎么办 win7电脑开机慢的优化教程
  5. halcon区域腐蚀膨胀算子_Halcon 形态学膨胀腐蚀应用举例
  6. 内存类型范围寄存器 (MTRR)
  7. Linux虚拟化KVM-Qemu分析(一)
  8. html 显图片宽度100,HTML – 如何显示内联几个 100%宽度?
  9. python图像数据是几维数据_python实现读取类别频数数据画水平条形图案例
  10. Django访问java建立的数据库