使用GDI+畫一個電子印章,初次使用,請多多指教。
以下是Form代碼,大家應該都會用,項目檔案就不上傳了。
效果圖
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
pictureBox1.Size = new Size(W + penWith , W + penWith);
}
/// <summary>
/// 畫筆寬度
/// </summary>
const int penWith = 4;
const int W = 200;
const int H = W;
/// <summary>
/// 半徑
/// </summary>
const int Radius = W / 2;
/// <summary>
/// 直徑
/// </summary>
const int Diameter = W;
/// <summary>
/// 公司名字型
/// </summary>
Font f = new System.Drawing.Font("宋體", 24, FontStyle.Bold);
/// <summary>
/// 公司号碼字型
/// </summary>
Font nf = new System.Drawing.Font("宋體", 9, FontStyle.Bold);
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Creat();
}
Bitmap Creat()
{
//建立位圖
Bitmap bt = new Bitmap(W + penWith, H + penWith );
//建立畫闆
Graphics g = Graphics.FromImage(bt);
g.SmoothingMode = SmoothingMode.AntiAlias;//消除繪制圖形的鋸齒
g.Clear(Color.White);//以白色清空背景
//文本反鋸齒
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Pen p = new System.Drawing.Pen(Color.Red, penWith);
Rectangle rec = new Rectangle(new System.Drawing.Point(penWith, penWith), new Size(W - penWith * 2, H - penWith * 2));
rec = new Rectangle(penWith/2, penWith/2, W, W);
//畫外接正方形
//g.DrawRectangle(new Pen(Color.Black, penWith), rec);
g.DrawEllipse(new Pen(Color.Red, penWith), rec);
string star = "★";
Font star_Font = new Font("宋體", 50, FontStyle.Regular);//設定星号的字型樣式
SizeF star_Size = g.MeasureString(star, star_Font);//對指定字元串進行測量
//要指定的位置繪制星号
PointF star_xy = new PointF(W / 2 - star_Size.Width / 2, W / 2 - star_Size.Height / 2);
g.DrawString(star, star_Font, Brushes.Red, star_xy);
//設定坐标原點為圓心
g.TranslateTransform(Radius, Radius);
//畫十字
//g.DrawLine(p, 0, -Radius, 0, Radius);
//g.DrawLine(p, -Radius, 0, Radius, 0);
string name = "衆達電子(美國)有限公司";
int total=0;
for (int i = 0; i < name.Length; i++)
{
string str = name[i].ToString();
if (str == "(" || str == ")")//對于括号特殊處理,使文本更加緊湊
{
total += 18;
}
else
{
total += 24;
}
}
total -= 34;//偏移量矯正
g.RotateTransform(-total/2);//坐标系旋轉
for (int i = 0; i < name.Length; i++)
{
//此循環每次都是畫字元到坐标系的正上方,每次畫完後旋轉相應角度
string str = name[i].ToString();
SizeF sf = g.MeasureString(str, f);
g.DrawString(str, f, Brushes.Red, -sf.Width / 2, -Radius + sf.Height / 8);
if ((str == ")") || (i < name.Length - 2 && name[i + 1].ToString() == ")")||(str == "(") || (i < name.Length - 2 && name[i + 1].ToString() == "("))//發現目前是括号或者下一個是括号,則移動角度小點
{
g.RotateTransform(18);
}
else
{
g.RotateTransform(24);
}
}
g.ResetTransform();
g.TranslateTransform(Radius, Radius);
string num = "3105345027698";
int numRotate = 7;
g.RotateTransform(numRotate * num.Length / 2 - numRotate/2);
for (int i = 0; i < num.Length; i++)
{
//此循環每次都是畫字元到坐标系的正上方,每次畫完後旋轉相應角度
string str = num[i].ToString();
SizeF sf = g.MeasureString(str, nf);
g.DrawString(str, nf, Brushes.Red, 0-sf.Width/2, Radius - sf.Height);
g.RotateTransform(-numRotate);
}
g.Save();
g.Dispose();
return bt;
}
}