C# Graphics绘图 picBox

需求:

  1             Bitmap bm = new Bitmap(picboxPreview.Width, picboxPreview.Height);
  2             using (Graphics g = Graphics.FromImage(bm))
  3             {
  4                 //绘制外面的矩形边框
  5                 Pen pen = new Pen(Color.Black, 2.0f);
  6                 g.DrawRectangle(pen, new Rectangle(new Point(0, 0), picboxPreview.Size));
  7                 //绘制左边的位图
  8                 if (cbxPhoto.CheckedStatus && picboxPhoto.Image != null)
  9                 {
 10                     g.DrawImage(picboxPhoto.Image, new Rectangle(new Point(5, 5), new Size(picboxPreview.Height - 5, picboxPreview.Height - 10)));
 11                 }
 12                 //绘制右边的位图
 13                 if (cbxLogo.CheckedStatus && picboxLogo.Image != null)
 14                 {
 15                     g.DrawImage(picboxLogo.Image, new Rectangle(new Point(picboxPreview.Width - (picboxPreview.Height - 5) - 5, 5), new Size(picboxPreview.Height - 5, picboxPreview.Height - 10)));
 16                 }
 17                 //绘制Name、Designation、Department、Institution/Company
 18                 StringFormat sf = new StringFormat();
 19                 sf.Alignment = StringAlignment.Center;
 20                 sf.LineAlignment = StringAlignment.Center;
 21                 Font fA = new Font("Arial", 15, FontStyle.Bold);
 22                 Font fB = new Font("Arial", 12, FontStyle.Regular);
 23                 //1.绘制4行 Name Designation Department Institution/Company
 24                 //2.绘制3行 Name Designation Department
 25                 //3.        Name Designation Institution/Company
 26                 //4         Name Department Institution/Company
 27                 //5         Designation Department Institution/Company
 28                 //6.绘制2行 Name Designation
 29                 //7         Name Department
 30                 //8         Name Institution/Company
 31                 //9         Designation Department
 32                 //10        Designation Institution/Company
 33                 //11        Department Institution/Company 
 34                 //12.绘制1行Name   
 35                 //13.       Designation   
 36                 //14.       Department  
 37                 //15.       Institution/Company
 38                 bool EnableName = cbxName.CheckedStatus && !string.IsNullOrEmpty(txtName.Text.Trim());
 39                 bool EnableDesignation = cbxDesignation.CheckedStatus && !string.IsNullOrEmpty(txtDesignation.Text.Trim());
 40                 bool EnableDepartment = cbxDepartment.CheckedStatus && !string.IsNullOrEmpty(txtDepartment.Text.Trim());
 41                 bool EnableInstitution = cbxInstitution.CheckedStatus && !string.IsNullOrEmpty(txtInstitution.Text.Trim());
 42                 //float drawX;
 43                 float drawY;
 44                 SizeF sizefName = g.MeasureString(txtName.Text.Trim(), new Font("Arial", 15, FontStyle.Bold));
 45                 SizeF sizefDesignation = g.MeasureString(txtDesignation.Text.Trim(), new Font("Arial", 12, FontStyle.Regular));
 46                 SizeF sizefDepartment = g.MeasureString(txtDepartment.Text.Trim(), new Font("Arial", 12, FontStyle.Regular));
 47                 SizeF sizefInstitution = g.MeasureString(txtInstitution.Text.Trim(), new Font("Arial", 12, FontStyle.Regular));
 48                 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
 49                 bool EnableRows = EnableName && EnableDesignation && EnableDepartment && EnableInstitution;
 50                 if (EnableName && EnableDesignation && EnableDepartment && EnableInstitution)
 51                 {//1.绘制4行 Name Designation Department Institution/Company
 52                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
 53                     DrawStringOfName(g, sizefName, drawY);
 54 
 55                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefName.Height;
 56                     DrawStringOfDesignation(g, sizefDesignation, drawY);
 57 
 58                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefName.Height + sizefDesignation.Height;
 59                     DrawStringOfDepartment(g, sizefDepartment, drawY);
 60 
 61                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefName.Height + sizefDesignation.Height + sizefDepartment.Height;
 62                     DrawStringOfInstitution(g, sizefInstitution, drawY);
 63                 }
 64                 else if (!EnableName && EnableDesignation && EnableDepartment && EnableInstitution)
 65                 {
 66                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
 67                     DrawStringOfDesignation(g, sizefDesignation, drawY);
 68 
 69                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefDesignation.Height;
 70                     DrawStringOfDepartment(g, sizefDepartment, drawY);
 71 
 72                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefDesignation.Height + sizefDepartment.Height;
 73                     DrawStringOfInstitution(g, sizefInstitution, drawY);
 74                 }
 75                 else if (EnableName && !EnableDesignation && EnableDepartment && EnableInstitution)
 76                 {
 77                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
 78                     DrawStringOfName(g, sizefName, drawY);
 79 
 80                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefName.Height;
 81                     DrawStringOfDepartment(g, sizefDepartment, drawY);
 82 
 83                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefName.Height + sizefDepartment.Height;
 84                     DrawStringOfInstitution(g, sizefInstitution, drawY);
 85                 }
 86                 else if (EnableName && EnableDesignation && !EnableDepartment && EnableInstitution)
 87                 {
 88                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / 2;
 89                     DrawStringOfName(g, sizefName, drawY);
 90 
 91                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / 2 + sizefName.Height;
 92                     DrawStringOfDesignation(g, sizefDesignation, drawY);
 93 
 94                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / 2 + sizefName.Height + sizefDesignation.Height;
 95                     DrawStringOfInstitution(g, sizefInstitution, drawY);
 96                 }
 97                 else if (EnableName && EnableDesignation && EnableDepartment && !EnableInstitution)
 98                 {
 99                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / 2;
100                     DrawStringOfName(g, sizefName, drawY);
101 
102                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / 2 + sizefName.Height;
103                     DrawStringOfDesignation(g, sizefDesignation, drawY);
104 
105                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / 2 + sizefName.Height + sizefDesignation.Height;
106                     DrawStringOfDepartment(g, sizefDepartment, drawY);
107                 }
108                 else if (!EnableName && !EnableDesignation && EnableDepartment && EnableInstitution)
109                 {
110                     drawY = (picboxPreview.Height - (sizefDepartment.Height + sizefInstitution.Height)) / 2;
111                     DrawStringOfDepartment(g, sizefDepartment, drawY);
112 
113                     drawY = (picboxPreview.Height - (sizefDepartment.Height + sizefInstitution.Height)) / 2 + sizefDepartment.Height;
114                     DrawStringOfInstitution(g, sizefInstitution, drawY);
115                 }
116                 else if (!EnableName && EnableDesignation && !EnableDepartment && EnableInstitution)
117                 {
118                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefInstitution.Height)) / 2;
119                     DrawStringOfDesignation(g, sizefDesignation, drawY);
120 
121                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefInstitution.Height)) / 2 + sizefDesignation.Height;
122                     DrawStringOfInstitution(g, sizefInstitution, drawY);
123                 }
124                 else if (!EnableName && EnableDesignation && EnableDepartment && !EnableInstitution)
125                 {
126                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height)) / 2;
127                     DrawStringOfDesignation(g, sizefDesignation, drawY);
128 
129                     drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height)) / 2 + sizefDesignation.Height;
130                     DrawStringOfDepartment(g, sizefDepartment, drawY);
131                 }
132                 else if (EnableName && !EnableDesignation && !EnableDepartment && EnableInstitution)
133                 {
134                     drawY = (picboxPreview.Height - (sizefName.Height + sizefInstitution.Height)) / 2;
135                     DrawStringOfName(g, sizefName, drawY);
136 
137                     drawY = (picboxPreview.Height - (sizefName.Height + sizefInstitution.Height)) / 2 + sizefName.Height;
138                     DrawStringOfInstitution(g, sizefInstitution, drawY);
139                 }
140                 else if (EnableName && !EnableDesignation && EnableDepartment && !EnableInstitution)
141                 {
142                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height)) / 2;
143                     DrawStringOfName(g, sizefName, drawY);
144 
145                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height)) / 2 + sizefName.Height;
146                     DrawStringOfDepartment(g, sizefDepartment, drawY);
147                 }
148                 else if (EnableName && EnableDesignation && !EnableDepartment && !EnableInstitution)
149                 {
150                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height)) / 2;
151                     DrawStringOfName(g, sizefName, drawY);
152 
153                     drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height)) / 2 + sizefName.Height;
154                     DrawStringOfDesignation(g, sizefDesignation, drawY);
155                 }
156                 else if (!EnableName && !EnableDesignation && !EnableDepartment && EnableInstitution)
157                 {
158                     drawY = (picboxPreview.Height - sizefInstitution.Height) / 2;
159                     DrawStringOfInstitution(g, sizefInstitution, drawY);
160                 }
161                 else if (!EnableName && !EnableDesignation && EnableDepartment && !EnableInstitution)
162                 {
163                     drawY = (picboxPreview.Height - sizefDepartment.Height) / 2;
164                     DrawStringOfDepartment(g, sizefDepartment, drawY);
165                 }
166                 else if (!EnableName && EnableDesignation && !EnableDepartment && !EnableInstitution)
167                 {
168                     drawY = (picboxPreview.Height - sizefDesignation.Height) / 2;
169                     DrawStringOfDesignation(g, sizefDesignation, drawY);
170                 }
171                 else if (EnableName && !EnableDesignation && !EnableDepartment && !EnableInstitution)
172                 {
173                     drawY = (picboxPreview.Height - sizefName.Height) / 2;
174                     DrawStringOfName(g, sizefName, drawY);
175                 }
176             }
177             picboxPreview.SizeMode = PictureBoxSizeMode.Normal;
178             picboxPreview.Image = bm;    
 1         #region DrawString Name|Designation|Department|Institution
 2         private void DrawStringOfName(Graphics g, SizeF sizefName, float drawY)
 3         {
 4             string drawString = txtName.Text.Trim();
 5             Font drawFont = new Font("Arial", 15, FontStyle.Bold);
 6             Brush drawBrush = Brushes.Black;
 7             float drawX = (picboxPreview.Width - sizefName.Width) / 2;
 8             //drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
 9             g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
10         }
11         private void DrawStringOfDesignation(Graphics g, SizeF sizefDesignation, float drawY)
12         {
13             string drawString = txtDesignation.Text.Trim();
14             Font drawFont = new Font("Arial", 12, FontStyle.Regular);
15             Brush drawBrush = Brushes.Gray;
16             float drawX = (picboxPreview.Width - sizefDesignation.Width) / 2;
17             //drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
18             g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
19         }
20         private void DrawStringOfDepartment(Graphics g, SizeF sizefDepartment, float drawY)
21         {
22             string drawString = txtDepartment.Text.Trim();
23             Font drawFont = new Font("Arial", 12, FontStyle.Regular);
24             Brush drawBrush = Brushes.Gray;
25             float drawX = (picboxPreview.Width - sizefDepartment.Width) / 2;
26             //drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
27             g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
28         }
29         private void DrawStringOfInstitution(Graphics g, SizeF sizefInstitution, float drawY)
30         {
31             string drawString = txtInstitution.Text.Trim();
32             Font drawFont = new Font("Arial", 12, FontStyle.Regular);
33             Brush drawBrush = Brushes.Gray;
34             float drawX = (picboxPreview.Width - sizefInstitution.Width) / 2;
35             //drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
36             g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
37         }
38         #endregion

 结果:

posted @ 2016-05-06 15:12  zhyue93  阅读(1373)  评论(0编辑  收藏  举报