网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

这篇文章主要介绍了C#如何实现在底图上动态生成文字和图片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联公司是一家以重庆网站建设公司、网页设计、品牌设计、软件运维、seo优化、小程序App开发等移动开发为一体互联网公司。已累计为成都发电机维修等众行业中小客户提供优质的互联网建站和软件开发服务。

代码:

/// 
/// 在底图上画指定路径的图片
/// 
/// 画板实例
/// 图片路径
/// 画区总长度
/// 画区总高度
/// 起点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#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。

感谢你能够认真阅读完这篇文章,希望小编分享的“C#如何实现在底图上动态生成文字和图片”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:C#如何实现在底图上动态生成文字和图片-创新互联
链接URL:http://bjjierui.cn/article/eeoeh.html

其他资讯