欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现在底图上动态生成文字和图片

程序员文章站 2022-06-03 18:17:51
本文主要记录在图片上动态的生成需要添加的文字和把指定的图片加到底图上,直接上代码 /// /// 在底图上画指定路径的图片 /...

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。