C#怎么实现在底图上动态生成文字和图片-创新互联

这篇文章主要介绍“C#怎么实现在底图上动态生成文字和图片”,在日常操作中,相信很多人在C#怎么实现在底图上动态生成文字和图片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么实现在底图上动态生成文字和图片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、网页空间、营销软件、网站建设、陆河网站维护、网站推广。

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

/// /// 在底图上画指定路径的图片/// /// 画板实例/// 图片路径/// 画区总长度/// 画区总高度/// 起点X坐标/// 起点Y坐标  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);    }   }  }  ///   /// 在图上画图片  ///   /// 画板实例  /// 画区总长度  /// 画区总高度  /// 起点X坐标  /// 起点Y坐标  /// 要画的图片实例  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);  }  ///    /// 生成缩略图重载方法1,返回缩略图的Image对象   ///    /// 缩略图的宽度   /// 缩略图的高度   /// 缩略图的Image对象   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;   }  }  ///   /// 比较两个值,得到给到给定值(判断是否越界)  ///   /// 总长度  /// 指定长度  ///   public int GetSize(int total, int width)  {   if (total > width)   {    return width;   }   else   {    return total;   }  }  ///   /// 更加传入的值计算得到新值(计算点坐标)  ///   /// 总长度  /// 指定长度  ///   private int GetValue(int total, int width)  {   return (total - width) / 2;  }  ///   /// 在图片上画出文字  ///   /// 图片对象  /// 文字x坐标  /// 文字y坐标  /// 文字内容  /// 文本宽度  /// 文本高度  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#怎么实现在底图上动态生成文字和图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


当前标题:C#怎么实现在底图上动态生成文字和图片-创新互联
路径分享:http://myzitong.com/article/ddeiid.html