Csharp/C#教程:C#实现在底图上动态生成文字和图片分享

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

///<summary> ///在底图上画指定路径的图片 ///</summary> ///<paramname="g">画板实例</param> ///<paramname="path">图片路径</param> ///<paramname="totalWidth">画区总长度</param> ///<paramname="totalHeight">画区总高度</param> ///<paramname="px">起点X坐标</param> ///<paramname="py">起点Y坐标</param> privatevoidFontPic(refGraphicsg,stringpath,inttotalWidth,inttotalHeight,intpx,intpy) { if(File.Exists(path)) { varpImg=Image.FromFile(path); //如果图片大于画布区域,则缩小 if(totalHeight<pImg.Height&&totalWidth<pImg.Width) { ImagenewPic=GetReducedImage(pImg,totalWidth,totalHeight); if(newPic!=null) { DrawPic(refg,totalWidth,totalHeight,px,py,newPic); } } elseif(totalHeight<pImg.Height&&totalWidth>=pImg.Width) { ImagenewPic=GetReducedImage(pImg,pImg.Width,totalHeight); if(newPic!=null) { DrawPic(refg,totalWidth,totalHeight,px,py,newPic); } } elseif(totalHeight>=pImg.Height&&totalWidth<pImg.Width) { ImagenewPic=GetReducedImage(pImg,totalWidth,pImg.Height); if(newPic!=null) { DrawPic(refg,totalWidth,totalHeight,px,py,newPic); } } else { DrawPic(refg,totalWidth,totalHeight,px,py,pImg); } } } ///<summary> ///在图上画图片 ///</summary> ///<paramname="g">画板实例</param> ///<paramname="totalWidth">画区总长度</param> ///<paramname="totalHeight">画区总高度</param> ///<paramname="px">起点X坐标</param> ///<paramname="py">起点Y坐标</param> ///<paramname="pImg">要画的图片实例</param> privatevoidDrawPic(refGraphicsg,inttotalWidth,inttotalHeight,intpx,intpy,ImagepImg) { px+=GetValue(totalWidth,pImg.Width); py+=GetValue(totalHeight,pImg.Height); g.DrawImage(newBitmap(pImg,newSize(GetSize(totalWidth,pImg.Width),GetSize(totalHeight,pImg.Height))), newRectangle(px,py,totalWidth,totalHeight), 0,0,totalWidth,totalHeight,GraphicsUnit.Pixel); } ///<summary> ///生成缩略图重载方法1,返回缩略图的Image对象 ///</summary> ///<paramname="width">缩略图的宽度</param> ///<paramname="height">缩略图的高度</param> ///<returns>缩略图的Image对象</returns> publicImageGetReducedImage(ImageresourceImage,intwidth,intheight) { try { Imagedata=null; //用指定的大小和格式初始化Bitmap类的新实例 using(Bitmapbitmap=newBitmap(width,height,PixelFormat.Format32bppArgb)) { //从指定的Image对象创建新Graphics对象 using(Graphicsgraphics=Graphics.FromImage(bitmap)) { //清除整个绘图面并以透明背景色填充 //graphics.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片对象 graphics.DrawImage(resourceImage,newRectangle(0,0,width,height)); } data=newBitmap(bitmap); } returndata; } catch(Exceptione) { throwe; } } ///<summary> ///比较两个值,得到给到给定值(判断是否越界) ///</summary> ///<paramname="total">总长度</param> ///<paramname="width">指定长度</param> ///<returns></returns> publicintGetSize(inttotal,intwidth) { if(total>width) { returnwidth; } else { returntotal; } } ///<summary> ///更加传入的值计算得到新值(计算点坐标) ///</summary> ///<paramname="total">总长度</param> ///<paramname="width">指定长度</param> ///<returns></returns> privateintGetValue(inttotal,intwidth) { return(total-width)/2; } ///<summary> ///在图片上画出文字 ///</summary> ///<paramname="g">图片对象</param> ///<paramname="pointX">文字x坐标</param> ///<paramname="pointY">文字y坐标</param> ///<paramname="word">文字内容</param> ///<paramname="textWidth">文本宽度</param> ///<paramname="textHeight">文本高度</param> privatestaticvoidDrawStringWord(Graphicsg,intpointX,intpointY,stringword,inttextWidth,inttextHeight,intfontSize=30) { Fontfont=newFont("微软雅黑",fontSize,(FontStyle.Regular)); RectangleFtextArea=newRectangleF(pointX,pointY,textWidth,textHeight); Brushbrush=newSolidBrush(Color.Black); g.DrawString(word,font,brush,textArea); }

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

您可能感兴趣的文章:C#(.net)水印图片的生成完整实例asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例c#.net生成图片验证码的代码C#动态生成PictureBox并指定图片的方法C#生成条形码图片的简单方法c#生成图片缩略图的类(2种实现思路)c#多图片上传并生成缩略图的实例代码C#实现的pdf生成图片文字水印类实例c#生成自定义图片方法代码实例

标签: 动态 图片

C#利用PrintDocument定制打印单据的小例子

C#生成Word文件(图片、文字)

上述就是C#学习教程:C#实现在底图上动态生成文字和图片分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/908415.html

(0)
上一篇 2021年10月25日
下一篇 2021年10月25日

精彩推荐