3 // Advanced text drawing and formatting sample
6 // Jordi Mas i Hernandez <jordi@ximian.com>
11 using System.Drawing.Text;
12 using System.Drawing.Imaging;
14 namespace Font1Sample {
15 public class FontDrawing {
17 static string flagProcessing(StringFormat format)
21 switch (format.Alignment) {
22 case StringAlignment.Far:
23 str = "halign: Far - ";
25 case StringAlignment.Near:
26 str = "halign: Near - ";
28 case StringAlignment.Center:
29 str = "halign: Center - ";
35 switch (format.LineAlignment) {
36 case StringAlignment.Far:
37 str += "valign: Far - ";
39 case StringAlignment.Near:
40 str += "valign: Near - ";
42 case StringAlignment.Center:
43 str += "valign: Center - ";
49 switch (format.Trimming) {
50 case StringTrimming.Character:
51 str += "trimm: Char - ";
53 case StringTrimming.EllipsisCharacter:
54 str += "trimm: EllipsisChar - ";
56 case StringTrimming.EllipsisPath:
57 str += "trimm: EllipsisPath - ";
59 case StringTrimming.EllipsisWord:
60 str += "trimm: EllipsisWord - ";
62 case StringTrimming.None:
63 str += "trimm: None - ";
65 case StringTrimming.Word:
66 str += "trimm: Word - ";
72 switch (format.FormatFlags) {
73 case StringFormatFlags.NoWrap:
76 case StringFormatFlags.DirectionVertical:
79 case StringFormatFlags.DirectionRightToLeft:
90 static Rectangle calcRect(Rectangle rect)
92 return new Rectangle (rect.X, rect.Y+rect.Height+10, rect.Width,200);
95 public static void Main( )
98 float height = 1000.0F;
104 Font f1 = new Font("Arial",12);
105 Font f2 = new Font("Verdana", 8);
106 Font f3 = new Font("Courier New", 14);
107 Font f4 = new Font(FontFamily.GenericSansSerif, 14);
108 Font f5 = new Font(FontFamily.GenericMonospace, 14);
109 Font f6 = new Font(FontFamily.GenericSerif, 16);
110 Font fonttxt= new Font("Verdana", 8);
111 SolidBrush brushtxt = new SolidBrush(Color.Pink);
113 StringFormat strfmt1 = new StringFormat();
114 StringFormat strfmt2 = new StringFormat();
115 StringFormat strfmt3 = new StringFormat();
116 StringFormat strfmt4 = new StringFormat();
117 StringFormat strfmt5 = new StringFormat();
118 StringFormat strfmt6 = new StringFormat();
119 StringFormat strfmttxt = new StringFormat();
121 Bitmap bmp = new Bitmap((int)width, (int)height);
122 Graphics gr = Graphics.FromImage(bmp);
123 SolidBrush br = new SolidBrush(Color.White);
124 SolidBrush colorRed = new SolidBrush(Color.Red);
126 strfmttxt.Alignment = StringAlignment.Near;
127 strfmttxt.LineAlignment = StringAlignment.Near;
128 strfmttxt.Trimming = StringTrimming.Character;
130 strfmt1.Alignment = StringAlignment.Center;
131 strfmt1.LineAlignment = StringAlignment.Near;
132 strfmt1.Trimming = StringTrimming.Character;
133 strfmt1.HotkeyPrefix = HotkeyPrefix.Show;
135 strfmt2.Alignment = StringAlignment.Far;
136 strfmt2.LineAlignment = StringAlignment.Center;
137 strfmt2.Trimming = StringTrimming.Character;
139 strfmt3.Alignment = StringAlignment.Far;
140 strfmt3.LineAlignment = StringAlignment.Near;
141 strfmt3.Trimming = StringTrimming.None;
143 strfmt4.Alignment = StringAlignment.Far;
144 strfmt4.LineAlignment = StringAlignment.Far;
145 strfmt4.Trimming = StringTrimming.EllipsisCharacter;
147 strfmt5.Alignment = StringAlignment.Far;
148 strfmt5.LineAlignment = StringAlignment.Near;
149 strfmt5.Trimming = StringTrimming.None;
150 strfmt5.FormatFlags = StringFormatFlags.DirectionVertical;
152 strfmt6.Alignment = StringAlignment.Far;
153 strfmt6.LineAlignment = StringAlignment.Far;
154 strfmt6.Trimming = StringTrimming.EllipsisCharacter;
155 strfmt6.FormatFlags = StringFormatFlags.NoWrap;
157 Rectangle rect1 = new Rectangle (10,50,100,150);
158 Rectangle rect2 = new Rectangle (10,300,150,150);
160 Rectangle rect3 = new Rectangle (200,50,175,175);
161 Rectangle rect4 = new Rectangle (200,300,150,150);
163 Rectangle rect5 = new Rectangle (400,50,175,175);
164 Rectangle rect6 = new Rectangle (400,300,150,150);
165 Rectangle rect7 = new Rectangle (550,300, 140,120);
167 gr.DrawRectangle( new Pen(Color.Yellow), rect3);
168 gr.DrawRectangle( new Pen(Color.Blue), rect4);
170 gr.DrawRectangle( new Pen(Color.Yellow), rect5);
171 gr.DrawRectangle( new Pen(Color.Blue), rect6);
173 Console.WriteLine ("1shit");
175 SolidBrush solid = new SolidBrush(Color.Blue);
177 Console.WriteLine ("shit2");
180 Console.WriteLine ("shit");
182 Console.WriteLine ("solid2");
185 gr.DrawString("Samples of text with different fonts and formatting",
186 new Font("Verdana",16), new SolidBrush(Color.White), new Rectangle (5,5,600,100), strfmttxt);
192 gr.FillEllipse(solid, rect1);
195 gr.DrawRectangle( new Pen(Color.Green), rect2);
196 gr.DrawRectangle( new Pen(Color.Green), rect7);
198 str = "Ara que tinc &vint anys, ara que encara tinc força,que no tinc l'ànima morta, i em sento bullir la sang. (" + f1.Name + ")";
199 gr.DrawString( str, f1, new SolidBrush(Color.White), rect1, strfmt1);
200 gr.DrawString(flagProcessing(strfmt1), fonttxt, brushtxt, calcRect(rect1), strfmttxt);
201 sz = gr.MeasureString (str, f1, new SizeF (rect1.Width, rect1.Height), strfmt1, out chars, out lines);
202 Console.WriteLine("MeasureString str1 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
204 str = "Ara que em sento capaç de cantar si un altre canta. Avui que encara tinc veu i encara puc creure en déus (" + f2.Name + ")";
205 gr.DrawString(str, f2, new SolidBrush(Color.Red),rect2, strfmt2);
206 gr.DrawString(flagProcessing(strfmt2), fonttxt, brushtxt, calcRect(rect2), strfmttxt);
207 sz = gr.MeasureString (str, f2, new SizeF (rect2.Width, rect2.Height), strfmt2, out chars, out lines);
208 Console.WriteLine("MeasureString str2 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
210 str = "Vull cantar a les pedres, la terra, l'aigua, al blat i al camí, que vaig trepitjant. (" + f3.Name + ")";
211 gr.DrawString(str,f3, new SolidBrush(Color.White), rect3, strfmt3);
212 gr.DrawString(flagProcessing(strfmt3), fonttxt, brushtxt, calcRect(rect3), strfmttxt);
213 sz = gr.MeasureString (str, f3, new SizeF (rect3.Width, rect3.Height), strfmt3, out chars, out lines);
214 Console.WriteLine("MeasureString str3 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
216 str = "A la nit, al cel i a aquet mar tan nostre i al vent que al matí ve a besar-me el rostre (" + f4.Name + ")";
217 gr.DrawString(str, f4, new SolidBrush(Color.Red),rect4, strfmt4);
218 gr.DrawString(flagProcessing(strfmt4), fonttxt, brushtxt, calcRect(rect4), strfmttxt);
219 sz = gr.MeasureString (str, f4, new SizeF (rect4.Width, rect4.Height), strfmt4, out chars, out lines);
220 Console.WriteLine("MeasureString str4 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
222 str = "Vull cantar a les pedres, la terra, l'aigua, al blat i al camí, que vaig trepitjant. (" + f5.Name + ")";
223 gr.DrawString(str, f5, new SolidBrush(Color.White), rect5, strfmt5);
224 gr.DrawString(flagProcessing(strfmt5), fonttxt, brushtxt, calcRect(rect5), strfmttxt);
225 sz = gr.MeasureString (str, f5, new SizeF (rect5.Width, rect5.Height), strfmt5, out chars, out lines);
226 Console.WriteLine("MeasureString str4 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
228 str = "Ve a besar-me el rostre (" + f6.Name + ")";
229 gr.DrawString(str, f6, new SolidBrush(Color.Red),rect6, strfmt6);
230 gr.DrawString(flagProcessing(strfmt6), fonttxt, brushtxt, calcRect(rect6), strfmttxt);
231 sz = gr.MeasureString (str, f6, new SizeF (rect6.Width, rect6.Height), strfmt6, out chars, out lines);
232 Console.WriteLine("MeasureString str6 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
234 str = "Vull plorar amb aquells que es troben tots sols, sense cap amor van passant pel món.. (" + f5.Name + ")";
235 gr.DrawString(str, f5, new SolidBrush(Color.White), rect7, strfmt4);
236 gr.DrawString(flagProcessing(strfmt4), fonttxt, brushtxt, calcRect(rect7), strfmttxt);
237 sz = gr.MeasureString (str, f5, new SizeF (rect7.Width, rect7.Height), strfmt5, out chars, out lines);
238 Console.WriteLine("MeasureString str7 [" + str + "] " + sz + ";chars:" + chars + " lines:" + lines);
242 Font f8 = new Font("Verdana", 10);
243 Font f9 = new Font("Verdana", 6);
244 Font f10 = new Font("Verdana", 12);
245 Font f11 = new Font("Verdana", 14);
247 Rectangle rect8 = new Rectangle (10, 550,100,100);
248 Rectangle rect9 = new Rectangle (150,550, 100,100);
249 Rectangle rect10 = new Rectangle (300,550, 100,100);
250 Rectangle rect11 = new Rectangle (420,550, 100,100);
251 Rectangle rect12 = new Rectangle (530,550, 200,100);
252 Rectangle rect13 = new Rectangle (530,600, 200,100);
253 Rectangle rect14 = new Rectangle (530,650, 200,100);
255 gr.DrawRectangle( new Pen(Color.Yellow), rect8);
256 gr.DrawRectangle( new Pen(Color.Yellow), rect9);
257 gr.DrawRectangle( new Pen(Color.Yellow), rect10);
259 StringFormat strfmt8 = new StringFormat();
260 strfmt8.Alignment = StringAlignment.Center;
261 strfmt8.LineAlignment = StringAlignment.Near;
262 strfmt8.Trimming = StringTrimming.EllipsisCharacter;
263 strfmt8.HotkeyPrefix = HotkeyPrefix.Show;
265 StringFormat strfmt9 = new StringFormat();
266 strfmt9.Alignment = StringAlignment.Center;
267 strfmt9.LineAlignment = StringAlignment.Center;
268 strfmt9.Trimming = StringTrimming.EllipsisCharacter;
269 strfmt9.HotkeyPrefix = HotkeyPrefix.None;
271 StringFormat strfmt10 = new StringFormat();
272 strfmt10.Alignment = StringAlignment.Center;
273 strfmt10.LineAlignment = StringAlignment.Near;
274 strfmt10.Trimming = StringTrimming.Word;
275 strfmt10.HotkeyPrefix = HotkeyPrefix.Show;
277 StringFormat strfmt11 = new StringFormat();
278 strfmt11.Alignment = StringAlignment.Center;
279 strfmt11.LineAlignment = StringAlignment.Near;
280 strfmt11.Trimming = StringTrimming.Word;
281 strfmt11.HotkeyPrefix = HotkeyPrefix.Show;
282 strfmt11.FormatFlags = StringFormatFlags.DirectionRightToLeft;
284 StringFormat strfmt12 = new StringFormat();
285 float[] tabs = {10, 20, 30};
286 strfmt12.Alignment = StringAlignment.Center;
287 strfmt12.LineAlignment = StringAlignment.Near;
288 strfmt12.Trimming = StringTrimming.Word;
289 strfmt12.HotkeyPrefix = HotkeyPrefix.Show;
290 strfmt12.SetTabStops(20, tabs);
292 StringFormat strfmt13 = new StringFormat();
293 float[] tabs2 = {5, 50, 60};
294 strfmt13.Alignment = StringAlignment.Center;
295 strfmt13.LineAlignment = StringAlignment.Near;
296 strfmt13.Trimming = StringTrimming.Word;
297 strfmt13.HotkeyPrefix = HotkeyPrefix.Show;
298 strfmt13.SetTabStops(0, tabs2);
300 StringFormat strfmt14 = new StringFormat();
301 strfmt14.Alignment = StringAlignment.Center;
302 strfmt14.LineAlignment = StringAlignment.Near;
303 strfmt14.Trimming = StringTrimming.Word;
304 strfmt14.HotkeyPrefix = HotkeyPrefix.Show;
305 strfmt14.FormatFlags = StringFormatFlags.DirectionRightToLeft;
307 str = "Vull alçar la veu,per cantar als homes que han nascut dempeus (" + f8.Name + ")";
308 gr.DrawString(str, f8, new SolidBrush(Color.White), rect8, strfmt8);
309 gr.DrawString(flagProcessing(strfmt8), fonttxt, brushtxt, calcRect(rect8), strfmttxt);
310 sz = gr.MeasureString (str, f8, new SizeF (rect8.Width, rect8.Height), strfmt8, out chars, out lines);
311 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect8.X, rect8.Y, (int)sz.Width, (int)sz.Height));
313 str = "I no tinc l'ànima morta i em sento bollir la sang (" + f9.Name + ")";
314 gr.DrawString(str, f9, new SolidBrush(Color.White), rect9, strfmt9);
315 gr.DrawString(flagProcessing(strfmt9), fonttxt, brushtxt, calcRect(rect9), strfmttxt);
316 sz = gr.MeasureString (str, f9, new SizeF (rect9.Width, rect9.Height), strfmt9, out chars, out lines);
317 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect9.X, rect9.Y, (int)sz.Width, (int)sz.Height));
319 str = "I no tinc l'ànima morta i em sento bollir la sang (" + f10.Name + ")";
320 gr.DrawString(str, f10, new SolidBrush(Color.White), rect10, strfmt10);
321 gr.DrawString(flagProcessing(strfmt10), fonttxt, brushtxt, calcRect(rect10), strfmttxt);
322 sz = gr.MeasureString (str, f10, new SizeF (rect10.Width, rect10.Height), strfmt10, out chars, out lines);
323 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect10.X, rect10.Y, (int)sz.Width, (int)sz.Height));
325 str = "I no tinc l'ànima morta i em sento bollir la sang (" + f11.Name + ")";
326 gr.DrawString(str, f11, new SolidBrush(Color.White), rect11, strfmt11);
327 gr.DrawString(flagProcessing(strfmt11), fonttxt, brushtxt, calcRect(rect11), strfmttxt);
328 sz = gr.MeasureString (str, f11, new SizeF (rect11.Width, rect11.Height), strfmt11, out chars, out lines);
329 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect11.X, rect11.Y, (int)sz.Width, (int)sz.Height));
331 str = "Tab1\tTab2\tTab3";
332 gr.DrawString(str, f8, new SolidBrush(Color.White), rect12, strfmt12);
333 sz = gr.MeasureString (str, f8, new SizeF (rect12.Width, rect12.Height), strfmt12, out chars, out lines);
334 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect12.X, rect12.Y, (int)sz.Width, (int)sz.Height));
336 str = "Nom\tCognom\tAdreça";
337 gr.DrawString(str, f8, new SolidBrush(Color.White), rect13, strfmt13);
338 sz = gr.MeasureString (str, f8, new SizeF (rect13.Width, rect13.Height), strfmt13, out chars, out lines);
339 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect13.X, rect13.Y, (int)sz.Width, (int)sz.Height));
341 str = "Nom Cognom Adreça";
342 gr.DrawString(str, f8, new SolidBrush(Color.White), rect14, strfmt14);
343 sz = gr.MeasureString (str, f8, new SizeF (rect14.Width, rect13.Height), strfmt14, out chars, out lines);
344 gr.DrawRectangle(new Pen(Color.Red), new Rectangle (rect14.X, rect14.Y, (int)sz.Width, (int)sz.Height));
346 bmp.Save("fontDrawingAdv.bmp", ImageFormat.Bmp);