C#实现在底图上动态生成文字和图片
本文主要记录在图片上动态的生成需要添加的文字和把指定的图片加到底图上,直接上代码
//////在底图上画指定路径的图片 /// ///画板实例 /// 图片路径 /// 画区总长度 /// 画区总高度 /// 起点X坐标 /// 起点Y坐标 privatevoidFontPic(refGraphicsg,stringpath,inttotalWidth,inttotalHeight,intpx,intpy) { if(File.Exists(path)) { varpImg=Image.FromFile(path); //如果图片大于画布区域,则缩小 if(totalHeight =pImg.Width) { ImagenewPic=GetReducedImage(pImg,pImg.Width,totalHeight); if(newPic!=null) { DrawPic(refg,totalWidth,totalHeight,px,py,newPic); } } elseif(totalHeight>=pImg.Height&&totalWidth ///在图上画图片 /// /// 画板实例 /// 画区总长度 /// 画区总高度 /// 起点X坐标 /// 起点Y坐标 /// 要画的图片实例 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); } /// ///生成缩略图重载方法1,返回缩略图的Image对象 /// ///缩略图的宽度 /// 缩略图的高度 /// 缩略图的Image对象 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; } } //////比较两个值,得到给到给定值(判断是否越界) /// ///总长度 /// 指定长度 /// publicintGetSize(inttotal,intwidth) { if(total>width) { returnwidth; } else { returntotal; } } /// ///更加传入的值计算得到新值(计算点坐标) /// ///总长度 /// 指定长度 /// privateintGetValue(inttotal,intwidth) { return(total-width)/2; } /// ///在图片上画出文字 /// ///图片对象 /// 文字x坐标 /// 文字y坐标 /// 文字内容 /// 文本宽度 /// 文本高度 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); }
希望对需要这方面操作的朋友有所帮助。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。