首页 > 编程语言 >C#中使用GDI+绘制辉光效果文字

C#中使用GDI+绘制辉光效果文字

时间:2023-02-28 11:02:29浏览次数:34  
标签:set return get C# 辉光 int new GDI public


问题在这里:


C#中使用GDI+绘制辉光效果文字_assembly


@To fer_ba:

-------------------------
// http://www..com版权所有,勿作商用
-------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace BrawDraw.Com.PhotoFrame.Net.WinFormsUI
{
/// <summary>
/// BrawDrawAd 的摘要说明。
/// </summary>
public class BrawDrawAd
{
Color _lgbColorOne = Color.DeepSkyBlue;
Color _lgbColorTwo = Color.DarkBlue;
Size _clientSize = new Size(800,600);
Rectangle _clientRectangle = Rectangle.Empty;
Graphics g = null;
string _glowString = @"​​​http://www.B.Com​​​";
int _glowPenWidth = 8;

string _logoFileName = string.Empty;
int _logoWidth = 188;
int _logoHeight = 200;
int _addOffsetY = - 128;
bool _isUsingMinSize = true;
int _wordsFontSize = 66;
int _adjustImageOffsetY = 200;
Bitmap _imageLogo = null; public BrawDrawAd(Graphics graphics, Rectangle clientRect)
{
this.ClientSize = new Size(clientRect.Width, clientRect.Height);
this.g = graphics;
this.ClientRectangle = clientRect;
} #region Public Properties

/// <summary>
/// 获取或设置背景渐变色的起始色(上边)
/// </summary>
public Color LgbColorOne
{
get { return _lgbColorOne; }
set { _lgbColorOne = value; }
}

/// <summary>
/// 获取或设置背景渐变色的终止色(下边)
/// </summary>
public Color LgbColorTwo
{
get { return _lgbColorTwo; }
set { _lgbColorTwo = value; }
}

/// <summary>
/// 图像的文件路径及文件名称
/// </summary>
public string LogoFileName
{
get
{
return _logoFileName;
}
set
{
_logoFileName = value;
}
} /// <summary>
/// 获取或设置图像宽度
/// </summary>
public int LogoWidth
{
get
{
return _logoWidth;
}
set
{
_logoWidth = value;
}
}

/// <summary>
/// 获取或设置图像高度
/// </summary>
public int LogoHeight
{
get
{
return _logoHeight;
}
set
{
_logoHeight = value;
}
}

/// <summary>
/// 图像在垂直高度上的偏移大小
/// </summary>
public int AddOffsetY
{
get
{
return _addOffsetY;
}
set
{
_addOffsetY = value;
}
} /// <summary>
/// 获取或设置图像
/// </summary>
public Bitmap ImageLogo
{
get
{
if(this.LogoFileName != string.Empty)
{
_imageLogo = (Bitmap) Bitmap.FromFile(this.LogoFileName);
}
else
{
System.Reflection.Assembly assembly = this.GetType().Assembly;
_imageLogo = new Bitmap(assembly.GetManifestResourceStream("BrawDraw.Com.PhotoFrame.Net.WeifenLuo.WinFormsUI.Resources.BrawDraw_logo.emf"));
}
return _imageLogo;
}
set
{
_imageLogo = value;
}
}

/// <summary>
/// 是否使用给定尺寸、屏幕尺寸及图像实际尺寸中的最小值,以保证图像的最佳效果
/// </summary>
public bool IsUsingMinSize
{
get
{
return _isUsingMinSize;
}
set
{
_isUsingMinSize = value;
}
} /// <summary>
/// 获取或设置词语的大小
/// </summary>
public int WordsFontSize
{
get { return _wordsFontSize; }
set { _wordsFontSize = value; }
} /// <summary>
/// Logo图片垂直位置调整
/// </summary>
public int AdjustImageOffsetY
{
get { return _adjustImageOffsetY; }
set { _adjustImageOffsetY = value; }
} /// <summary>
/// 获取或设置ClientSize
/// </summary>
public Size ClientSize
{
get
{
return _clientSize;
}
set
{
_clientSize = value;
}
} /// <summary>
/// 获取或设置ClientRectangle
/// </summary>
public Rectangle ClientRectangle
{
get
{
return _clientRectangle;
}
set
{
_clientRectangle = value;
}
} /// <summary>
/// 获取或设置发光文字的字符串
/// </summary>
public string GlowString
{
get
{
return _glowString;
}
set
{
_glowString = value;
}
} /// <summary>
/// 获取或设置发光文字的辉光笔画宽度
/// </summary>
public int GlowPenWidth
{
get { return _glowPenWidth; }
set { _glowPenWidth = value; }
} #endregion

#region Public Method
public void DrawAd()
{
LinearGradientBrush lgb = new LinearGradientBrush(new Point(0,0), new Point(0,ClientRectangle.Height), _lgbColorOne, _lgbColorTwo);
g.FillRectangle(lgb, ClientRectangle);
this.DrawBrawDrawLogo();
this.DrawWords();
}
#endregion #region Private Method
private void DrawWords()
{
FontFamily fontFamily = new FontFamily("Times New Roman"); Bitmap bm = new Bitmap(this.ClientSize.Width/8, this.ClientSize.Height/8);
GraphicsPath pth = new GraphicsPath(); pth.AddString(this.GlowString, fontFamily,(int)FontStyle.Bold, _wordsFontSize, new Point(3, 3),StringFormat.GenericTypographic);
int w = Convert.ToInt32( pth.GetBounds().Width );
int h = Convert.ToInt32( pth.GetBounds().Height );
Graphics gWords = Graphics.FromImage(bm);
Matrix mx = new Matrix(1.0f/8, 0, 0, 1.0f/8, -1.0f/8, -1.0f/8); gWords.SmoothingMode = SmoothingMode.HighQuality;
gWords.Transform = mx; //Pen p=new Pen( this.GetRandomColor(), 8);
Pen p = new Pen(Color.Yellow, _glowPenWidth); gWords.DrawPath(p, pth);

gWords.FillPath(Brushes.Yellow, pth); gWords.Dispose();

g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic; int offsetX = Convert.ToInt32((ClientRectangle.Width - w)/ 2 );
int offsetY = Convert.ToInt32( ClientRectangle.Height / 2 ) - h + 20;
g.Transform = new Matrix(1, 0, 0, 1, offsetX, offsetY);
Rectangle destRect;
destRect = ClientRectangle;
g.TranslateTransform(0, _adjustImageOffsetY);
g.DrawImage(bm, destRect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel); LinearGradientBrush lgb = new LinearGradientBrush(new PointF(0,pth.GetBounds().Top), new PointF(0,pth.GetBounds().Bottom - pth.GetBounds().Height / 2), _lgbColorTwo, _lgbColorOne);
//g.FillPath(new SolidBrush( Color.Red ), pth);
g.FillPath(lgb, pth); pth.Dispose();
bm.Dispose();
} private void DrawBrawDrawLogo()
{
//屏幕尺寸
int screenWidth = this.ClientRectangle.Width;
int screenHeight = this.ClientRectangle.Height;
//图像实际尺寸
int width = this.ImageLogo.Width;
int height = this.ImageLogo.Height;
//指定尺寸
int paramWidth = this.LogoWidth;
int paramHeight = this.LogoHeight;
//求得指定尺寸与屏幕尺寸的合适值
int clipWidth;
int clipHeight; if(this.IsUsingMinSize)
{
clipWidth = Math.Min(Math.Min(screenWidth, paramWidth), width);
clipHeight = Math.Min(Math.Min(screenHeight, paramHeight), height);
}
else
{
clipWidth = Math.Min(screenWidth, paramWidth);
clipHeight = Math.Min(screenHeight, paramHeight);
}
//求得缩放比例
double scale = Math.Min(clipWidth * 1.0 / width, clipHeight * 1.0 / height); int newWidth = Convert.ToInt32(width * scale);
int newHeight = Convert.ToInt32(height * scale); int offsetX = (ClientRectangle.Width - newWidth) / 2;
int offsetY = (ClientRectangle.Height - newHeight) / 2 + AddOffsetY;
Rectangle destRect = new Rectangle(offsetX, offsetY, newWidth, newHeight);
g.DrawImage(this.ImageLogo, destRect, new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
}
#endregion
}
}


标签:set,return,get,C#,辉光,int,new,GDI,public
From: https://blog.51cto.com/JohnsonJu/6090451

相关文章

  • 用实例讲DynamicResource与StaticResource的区别
    之前我的博客文章"​​WPF中的资源(Resource)​​"中概略性地提到过DynamicResource与StaticResource的区别。其中有这么一句,确切地说是两句:静态资源在第一次编译后即确定其对......
  • ATC:一个能将主流开源框架模型转换为昇腾模型的神奇工具
    摘要:本文介绍了昇腾CANN提供的模型转换工具ATC,介绍了其功能、架构,并以具体样例介绍了该工具的基本使用方法以及常用设置。本文分享自华为云社区《使用ATC工具将主流开源框......
  • centos7的密码安全策略加固
    centos7操作系统在CentOS 7上实现密码复杂度策略设置一.使用login.defs文件解析:/etc/login.defs是设置新建用户帐号限制的文件。该文件里的配置对root用户无效。/etc......
  • Websocket 60秒断开,连接不稳定
    本地测试都是正常的,线上测试总是过一会就断开...线上新增了https协议,导致页面中的链接必须也是sslWebsocket链接地址从ws://ws.xxx.com改成了wss://ws.xxx.com最开始htt......
  • Codeforces Round #776 (Div
    CodeforcesRound#776(Div.3)CodeForces-1650DTwistthePermutation给定你数组a:123...n,一共有n次操作,每次操作可以把\(a_i\)移到最左边,然后对\(i+1\)位以......
  • 阿里前端二面常考react面试题(必备)
    说说React组件开发中关于作用域的常见问题。在EMAScript5语法规范中,关于作用域的常见问题如下。(1)在map等方法的回调函数中,要绑定作用域this(通过bind方法)。(2)父组件传递......
  • 升级到React-Router-v6
    前言近期完成了公司新项目的开发,相关的技术栈都用到了最新版本,reactrouter也使用了v6的版本,所以借这个机会自己再梳理下reactrouterv5与v6的区别,以及v6一些新......
  • 网络请求类型Content-Type的认识
    一、Content-Type是什么?在HTTP协议消息头中,使用Content-Type来表示媒体类型信息。它被用来告诉服务端如何处理请求的数据,以及告诉客户端(一般是浏览器)如何解析响应的数据,比......
  • 滴滴前端二面必会react面试题指南
    在React中如何处理事件为了解决跨浏览器的兼容性问题,SyntheticEvent实例将被传递给你的事件处理函数,SyntheticEvent是React跨浏览器的浏览器原生事件包装器,它还拥有......
  • java调用c++的几种方式
    jni类似c#调用c++的方式,定义java端的c++代码接口。packagecrayon.jni;publicclassJNITest{publicnativestaticvoidset(inti);publicna......