網(wǎng)站制作NEWS
c# 如何畫出任意角度傾斜的字體
你新建一個(gè)窗口應(yīng)用程序,把下面的代碼貼進(jìn)去,然后弄個(gè)按鈕,按鈕事件里面寫 Render(); 運(yùn)行點(diǎn)按鈕看效果吧。 private SolidBrush RndBrush(Color BackColor,Random rnd)
{
int r, g, b;
do
{
r = rnd.Next(255);
} while (Math.Abs(r - BackColor.R) < 50); //這是為了控制顏色不要和背景色太接近
do
{
g = rnd.Next(255);
} while (Math.Abs(g - BackColor.G) < 50);
do
{
b = rnd.Next(255);
} while (Math.Abs(b - BackColor.B) < 50);
return new SolidBrush(Color.FromArgb(255,r,g,b));
}
private void Render(){
Graphics g = this.CreateGraphics(); char[] code = "Page 31.".ToCharArray(); //文字
Font myFont = new Font("宋體", 20);
float angle = 60; //角度
float l=0;
Random rnd = new Random();
g.Clear(this.BackColor);
for (int i = 0; i < code.Length; i++)
{
string s=new string(code[i],1);
g.ResetTransform();
SizeF size = g.MeasureString(s, myFont);
g.TranslateTransform(l + size.Width / 2, size.Height / 2); //設(shè)置旋轉(zhuǎn)中心為文字中心
g.RotateTransform((float)(rnd.NextDouble() * angle * 2 - angle)); //旋轉(zhuǎn)
g.DrawString(s, myFont, RndBrush(this.BackColor,rnd), new PointF(-size.Width / 2, -size.Height / 2));
l+=size.Width;
}
}
}
多重隨機(jī)標(biāo)簽