需求:
Bitmap bm = new Bitmap(picboxPreview.Width, picboxPreview.Height);
using (Graphics g = Graphics.FromImage(bm))
{
//绘制外面的矩形边框
Pen pen = new Pen(Color.Black, 2.0f);
g.DrawRectangle(pen, new Rectangle(new Point(, ), picboxPreview.Size));
//绘制左边的位图
if (cbxPhoto.CheckedStatus && picboxPhoto.Image != null)
{
g.DrawImage(picboxPhoto.Image, new Rectangle(new Point(, ), new Size(picboxPreview.Height - , picboxPreview.Height - )));
}
//绘制右边的位图
if (cbxLogo.CheckedStatus && picboxLogo.Image != null)
{
g.DrawImage(picboxLogo.Image, new Rectangle(new Point(picboxPreview.Width - (picboxPreview.Height - ) - , ), new Size(picboxPreview.Height - , picboxPreview.Height - )));
}
//绘制Name、Designation、Department、Institution/Company
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Font fA = new Font("Arial", , FontStyle.Bold);
Font fB = new Font("Arial", , FontStyle.Regular);
//1.绘制4行 Name Designation Department Institution/Company
//2.绘制3行 Name Designation Department
//3. Name Designation Institution/Company
//4 Name Department Institution/Company
//5 Designation Department Institution/Company
//6.绘制2行 Name Designation
//7 Name Department
//8 Name Institution/Company
//9 Designation Department
//10 Designation Institution/Company
//11 Department Institution/Company
//12.绘制1行Name
//13. Designation
//14. Department
//15. Institution/Company
bool EnableName = cbxName.CheckedStatus && !string.IsNullOrEmpty(txtName.Text.Trim());
bool EnableDesignation = cbxDesignation.CheckedStatus && !string.IsNullOrEmpty(txtDesignation.Text.Trim());
bool EnableDepartment = cbxDepartment.CheckedStatus && !string.IsNullOrEmpty(txtDepartment.Text.Trim());
bool EnableInstitution = cbxInstitution.CheckedStatus && !string.IsNullOrEmpty(txtInstitution.Text.Trim());
//float drawX;
float drawY;
SizeF sizefName = g.MeasureString(txtName.Text.Trim(), new Font("Arial", , FontStyle.Bold));
SizeF sizefDesignation = g.MeasureString(txtDesignation.Text.Trim(), new Font("Arial", , FontStyle.Regular));
SizeF sizefDepartment = g.MeasureString(txtDepartment.Text.Trim(), new Font("Arial", , FontStyle.Regular));
SizeF sizefInstitution = g.MeasureString(txtInstitution.Text.Trim(), new Font("Arial", , FontStyle.Regular));
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
bool EnableRows = EnableName && EnableDesignation && EnableDepartment && EnableInstitution;
if (EnableName && EnableDesignation && EnableDepartment && EnableInstitution)
{//1.绘制4行 Name Designation Department Institution/Company
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDesignation.Height + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && EnableDesignation && EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefDesignation.Height + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && !EnableDesignation && EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDesignation.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / + sizefName.Height + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (!EnableName && !EnableDesignation && EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefDepartment.Height + sizefInstitution.Height)) / + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefInstitution.Height)) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefInstitution.Height)) / + sizefDesignation.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height)) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height)) / + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (EnableName && !EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && !EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height)) / + sizefName.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (EnableName && EnableDesignation && !EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY);
}
else if (!EnableName && !EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - sizefInstitution.Height) / ;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && !EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - sizefDepartment.Height) / ;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (!EnableName && EnableDesignation && !EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - sizefDesignation.Height) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY);
}
else if (EnableName && !EnableDesignation && !EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - sizefName.Height) / ;
DrawStringOfName(g, sizefName, drawY);
}
}
picboxPreview.SizeMode = PictureBoxSizeMode.Normal;
picboxPreview.Image = bm;
#region DrawString Name|Designation|Department|Institution
private void DrawStringOfName(Graphics g, SizeF sizefName, float drawY)
{
string drawString = txtName.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Bold);
Brush drawBrush = Brushes.Black;
float drawX = (picboxPreview.Width - sizefName.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
private void DrawStringOfDesignation(Graphics g, SizeF sizefDesignation, float drawY)
{
string drawString = txtDesignation.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Regular);
Brush drawBrush = Brushes.Gray;
float drawX = (picboxPreview.Width - sizefDesignation.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
private void DrawStringOfDepartment(Graphics g, SizeF sizefDepartment, float drawY)
{
string drawString = txtDepartment.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Regular);
Brush drawBrush = Brushes.Gray;
float drawX = (picboxPreview.Width - sizefDepartment.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
private void DrawStringOfInstitution(Graphics g, SizeF sizefInstitution, float drawY)
{
string drawString = txtInstitution.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Regular);
Brush drawBrush = Brushes.Gray;
float drawX = (picboxPreview.Width - sizefInstitution.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
#endregion
结果: