时间:2021-05-20
本文主要记录在图片上动态的生成需要添加的文字和把指定的图片加到底图上,直接上代码
/// <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); }希望对需要这方面操作的朋友有所帮助。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
生成文字图片://////生成文字图片////////////publicImageCreateImage(stringtext,boolisBold,intf
这个demo用c#实现图片裁剪和半透明融合的功能演示程序。功能挺简单的,就是把一张固定大小的图片先做边缘羽化,然后贴到一个圆形泡泡形状的底图上,最后把结果半透明
如何用C#编程实现动态生成Word文档并填充数据的效果呢?要使用C#操作word,首先要添加引用1、添加引用->COM->MicrosoftWord11.0Ob
1.需求背景需要实现一个动态加载但不显示出来的视图,且该视图上有个动态生成的二维码,最后用其去生成一张快照(也就是图片)。(常见这种情况是来源于“图片分享”的功
本文实例讲述了C#动态生成PictureBox并指定图片的方法。分享给大家供大家参考。具体如下:intNum=0;PictureBox[]pb;Num=6;pb