首页 > 其他分享 >个性印章及公章的画法及实现

个性印章及公章的画法及实现

时间:2023-02-28 12:32:26浏览次数:35  
标签:公章 画法 int Bitmap 印章 rectInner osm new Rectangle



先看效果:

1.公章

个性印章及公章的画法及实现_exception


 2. 个人印章效果:

个性印章及公章的画法及实现_float_02



这里粘出关键代码:


private void DrawSeal()
{
RetrieveAll();
if(this.SealType == SealType.OfficeSeal)
{
DrawOfficeSeal();
}
else
{
DrawPersonalSeal();
}
panelDisplayResultBitmap.AutoScroll = true;
panelDisplayResultBitmap.AutoScrollMinSize = new Size ((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
panelDisplayResultBitmap.Invalidate();
}//得到相关参数:
private void RetrieveAll()
{
int diameter = 220;
try
{
diameter = int.Parse(this.txtBoxDiameter.Text);
}
catch(Exception exc)
{
MessageBox.Show("输入错误的公章直径!/n请确保是整数");
}
if(diameter>1000 || diameter < 100 )
{
MessageBox.Show("公章直径输入太大或太小了!");
diameter = 220;
}
this.OfficeSealDiameter = diameter; string fontName = this.FontlistBox.Text;
if(fontName == string.Empty)
{
fontName = "Times New Roman";
}
this.SelectedFontName = fontName;
this.SelectedFontColorFromArgb = Color.FromArgb((int)this.FontColor_A.Value,(int)this.FontColor_R.Value,
(int)this.FontColor_G.Value,(int)this.FontColor_B.Value);
}

// 画公章: private void DrawOfficeSeal()
{
try
{
OfficeSealMaker osm = new OfficeSealMaker();
osm.FamilyName = this.SelectedFontName;
osm.IsBold = false;
osm.IsEmbedNotice = false; //是否保留“勿作他用”的提示信息
osm.Diameter = this.OfficeSealDiameter;
osm.Style = Style.PentagonalStars;//设置中间LOGO
osm.OfficeName = this.txtBoxCompanyName.Text;
osm.IsDrawInnerRect = false;
osm.WordsColor = this.SelectedFontColorFromArgb;
osm.BorderColor = this.SelectedFontColorFromArgb;
//osm.TextVerHeight = (float)this.numericUpDownFontHeight.Value;
osm.RatioX = (float)numericUpDownFontRatioX.Value / 100f;
m_Bitmap = osm.Draw();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message,"发生错误");
}
}

//画私章: private void DrawPersonalSeal()
{
//int stampWidth = int.Parse(this.txtBoxDiameter.Text);
PersonalSeal personalSeal = new PersonalSeal();
personalSeal.SealColor = this.SelectedFontColorFromArgb;
bool isYinSeal = false;

if(this.cmbBoxYinYang.SelectedItem != null && this.cmbBoxYinYang.SelectedItem.ToString() == "阴章")
{
isYinSeal = true;
}
personalSeal.IsYinSeal = isYinSeal;
string sealWords = this.txtBoxPersonalSealWords.Text.Trim();
if(sealWords.Length != 4)
{
sealWords = "美中印章";
}
personalSeal.SealWords = sealWords;
personalSeal.FontFamilyName = this.SelectedFontName;
personalSeal.IsDrawInnerBorder = false;
personalSeal.BorderWidth = 10;
personalSeal.Celling = 0; int personalSealWidth = 100;
try
{
personalSealWidth = int.Parse(this.txtBoxWidth.Text);
}
catch(Exception exc)
{
MessageBox.Show("输入有误,请输入整数!");
} int personalSealHeight = 100;
try
{
personalSealHeight = int.Parse(this.txtBoxHeight.Text);
}
catch(Exception exc)
{
MessageBox.Show("输入有误,请输入整数!");
} personalSeal.Width = personalSealWidth;
personalSeal.Height = personalSealHeight; m_Bitmap = (Bitmap)personalSeal.Draw();
}

//私人印章的画法:
public System.Drawing.Image Draw()
{
Bitmap m_Bitmap;
Color sealColor = Color.Red;
int sealWidth = this.Width;
int sealHeight = this.Height;
int celling = this.Celling;
int penWidth = this.BorderWidth;
int cornerRadius = this.CornerRadius;
Rectangle rect = new Rectangle(0, 0, sealWidth, sealHeight);
Rectangle rectInner = new Rectangle(penWidth + celling, penWidth + celling, sealWidth - (penWidth + celling) * 2, sealHeight - (penWidth + celling) * 2);
m_Bitmap = new Bitmap(sealWidth, sealHeight);
Graphics g = Graphics.FromImage(m_Bitmap);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//g.Clear(Color.White); Pen pen = new Pen(sealColor, penWidth);
pen.Alignment = PenAlignment.Inset; //画个人印章的外部圆角矩形
RoundRectangeHelper.DrawRoundRectangle(g, pen, rect, cornerRadius); pen = new Pen(this.SealColor, this.InnerBorderWidth);
if(this.IsDrawInnerBorder)
{
g.DrawRectangle(pen, rectInner);
} //画字
Font font;
try
{
font = new Font(this.FontFamilyName, 20f);
}
catch(Exception exc)
{
font = new Font("Times New Roman",20f);
}
Color fillColor;
if(this.IsYinSeal)
{
fillColor = Color.White;
g.FillRectangle(new SolidBrush(this.SealColor), rectInner);
}
else
{
fillColor = this.SealColor;
} string word = this.SealWords;
int[] orderSrc = {1,2,3,4};
int[] orderDest = {2,4,1,3};
char[] words = word.ToCharArray();
char[] wordsTmp = new char[words.Length];
words.CopyTo(wordsTmp, 0);
for(int i = 0; i < orderSrc.Length; i++)
{
words[orderDest[i] - 1] = wordsTmp[orderSrc[i] - 1];
} Rectangle rect1 = new Rectangle(rectInner.X, rectInner.Y, rectInner.Width/2, rectInner.Height/2);
Rectangle rect2 = new Rectangle(rectInner.X + rectInner.Width/2, rectInner.Y, rectInner.Width/2, rectInner.Height/2);
Rectangle rect3 = new Rectangle(rectInner.X, rectInner.Y + rectInner.Height/2, rectInner.Width/2, rectInner.Height/2);
Rectangle rect4 = new Rectangle(rectInner.X + rectInner.Width/2, rectInner.Y + rectInner.Height/2, rectInner.Width/2, rectInner.Height/2);
Rectangle[] rects = new Rectangle[]
{
rect1, rect2, rect3, rect4
}; for(int i = 0; i < 4; i++)
{
DrawWordIntoRectangle(g, words[i].ToString(), font, fillColor, rects[i]);
} pen.Dispose();
g.Dispose(); return m_Bitmap;
}



(只贴了关键代码, 全部源码将放在我正在写的《来点创意嘛!.Net图形图像快乐程序设计
——GDI+之Windows应用程序和Web程序实战》(暂定名)一书中.)

标签:公章,画法,int,Bitmap,印章,rectInner,osm,new,Rectangle
From: https://blog.51cto.com/JohnsonJu/6090750

相关文章