C#文字绘图源码

这是一个使用C#编写的代码,用于在图像上绘制文字并调整文字大小以适应图像,同时支持添加透明度和阴影效果。代码通过计算最佳填充比例来调整字体大小,并根据指定位置进行文字布局。
摘要由CSDN通过智能技术生成

//绘图源码(Begin)

using System.Drawing;
using System.Drawing.Imaging;
using System;


public class Overlay //yxy absolute
{
 // "Old" signature overloaded to call new method w/extra parameters
 public static Bitmap TextOverlay(string imgs,  string  OverlayText,  Font OverlayFont, 
  Color OverlayColor,  bool AddAlpha,  bool AddShadow)
 {
  return TextOverlay(imgs, OverlayText, OverlayFont, OverlayColor, AddAlpha, AddShadow,
   ContentAlignment.MiddleCenter, 0.8F);
 }

 // Draw text directly onto an image (scaled for best-fit)
 // Written to be called from a module (just on the form for simplicity)
 public  static Bitmap TextOverlay( string imgs,  string  OverlayText,  Font OverlayFont, 
  Color OverlayColor,  bool AddAlpha,  bool AddShadow, 
  System.Drawing.ContentAlignment Position,  float PercentFill)
 {
  Bitmap bmp = new Bitmap(imgs);
  if ( OverlayText != null && OverlayText.Length > 0 && PercentFill > 0 )
  {
   // create bitmap and graphics used for drawing
   // "clone" image but use 24RGB format

   Graphics g = Graphics.FromImage(bmp);
   g.DrawImage(bmp, 0, 0);

   int alpha = 255;
   if ( AddAlpha )
   {
    // Compute transparency: Longer text should be less transparent or it gets lost.
    alpha = 90 + (OverlayText.Length * 2);
    if ( alpha >= 255 )  alpha = 255;
   }
   // Create the brush based on the color and alpha
   SolidBrush  b = new SolidBrush(Color.FromArgb(alpha, OverlayColor));

   // Measure the text to render (unscaled, unwrapped)
   StringFormat strFormat = StringFormat.GenericTypographic;
   SizeF s = g.MeasureString(OverlayText, OverlayFont, 100000, strFormat);

   // Enlarge font to specified fill (estimated by AREA)
   //float zoom = OverlayFont.Size ;//(float)(Math.Sqrt(((double)(bmp.Width * bmp.Height) * PercentFill) / (double)(s.Width * s.Height)));
   FontStyle sty = OverlayFont.Style;
   Font  f = new Font(OverlayFont.FontFamily, ((float)OverlayFont.Size), sty);
   //Console.WriteLine(String.Format("Starting Zoom: {0}, Font Size: {1}, Alpha: {2}", zoom, f.Size, alpha));

   // Measure using new font size, allow to wrap as needed.
   // Could rotate the overlay at a 30-45 deg. angle (trig would give correct angle).
   // Of course, then the area covered would be less than "straight" text.
   // I'll leave those calculations for someone else....
   int  charFit;
   int linesFit;
   float SQRTFill = (float)(Math.Sqrt(PercentFill));
   strFormat.FormatFlags = StringFormatFlags.NoClip; //|| StringFormatFlags.LineLimit || StringFormatFlags.MeasureTrailingSpaces;
   strFormat.Trimming = StringTrimming.Word;
   SizeF  layout = new SizeF(((float)bmp.Width) * SQRTFill, ((float)bmp.Height) * 1.5F); // fit to width, allow height to go over<

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值