3 // Advanced text drawing and formatting sample
6 // Jordi Mas i Hernandez <jordi@ximian.com>
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Drawing
.Text
;
35 using System
.Drawing
.Imaging
;
37 namespace Font1Sample
{
38 public class FontDrawing
{
40 static string flagProcessing(StringFormat format
)
44 switch (format
.Alignment
) {
45 case StringAlignment
.Far
:
46 str
= "halign: Far - ";
48 case StringAlignment
.Near
:
49 str
= "halign: Near - ";
51 case StringAlignment
.Center
:
52 str
= "halign: Center - ";
58 switch (format
.LineAlignment
) {
59 case StringAlignment
.Far
:
60 str
+= "valign: Far - ";
62 case StringAlignment
.Near
:
63 str
+= "valign: Near - ";
65 case StringAlignment
.Center
:
66 str
+= "valign: Center - ";
72 switch (format
.Trimming
) {
73 case StringTrimming
.Character
:
74 str
+= "trimm: Char - ";
76 case StringTrimming
.EllipsisCharacter
:
77 str
+= "trimm: EllipsisChar - ";
79 case StringTrimming
.EllipsisPath
:
80 str
+= "trimm: EllipsisPath - ";
82 case StringTrimming
.EllipsisWord
:
83 str
+= "trimm: EllipsisWord - ";
85 case StringTrimming
.None
:
86 str
+= "trimm: None - ";
88 case StringTrimming
.Word
:
89 str
+= "trimm: Word - ";
95 switch (format
.FormatFlags
) {
96 case StringFormatFlags
.NoWrap
:
99 case StringFormatFlags
.DirectionVertical
:
102 case StringFormatFlags
.DirectionRightToLeft
:
113 static Rectangle
calcRect(Rectangle rect
)
115 return new Rectangle (rect
.X
, rect
.Y
+rect
.Height
+10, rect
.Width
,200);
118 public static void Main( )
120 float width
= 750.0F
;
121 float height
= 1000.0F
;
127 Font f1
= new Font("Arial",12);
128 Font f2
= new Font("Verdana", 8);
129 Font f3
= new Font("Courier New", 14);
130 Font f4
= new Font(FontFamily
.GenericSansSerif
, 14);
131 Font f5
= new Font(FontFamily
.GenericMonospace
, 14);
132 Font f6
= new Font(FontFamily
.GenericSerif
, 16);
133 Font fonttxt
= new Font("Verdana", 8);
134 SolidBrush brushtxt
= new SolidBrush(Color
.Pink
);
136 StringFormat strfmt1
= new StringFormat();
137 StringFormat strfmt2
= new StringFormat();
138 StringFormat strfmt3
= new StringFormat();
139 StringFormat strfmt4
= new StringFormat();
140 StringFormat strfmt5
= new StringFormat();
141 StringFormat strfmt6
= new StringFormat();
142 StringFormat strfmttxt
= new StringFormat();
144 Bitmap bmp
= new Bitmap((int)width
, (int)height
);
145 Graphics gr
= Graphics
.FromImage(bmp
);
146 SolidBrush br
= new SolidBrush(Color
.White
);
147 SolidBrush colorRed
= new SolidBrush(Color
.Red
);
149 strfmttxt
.Alignment
= StringAlignment
.Near
;
150 strfmttxt
.LineAlignment
= StringAlignment
.Near
;
151 strfmttxt
.Trimming
= StringTrimming
.Character
;
153 strfmt1
.Alignment
= StringAlignment
.Center
;
154 strfmt1
.LineAlignment
= StringAlignment
.Near
;
155 strfmt1
.Trimming
= StringTrimming
.Character
;
156 strfmt1
.HotkeyPrefix
= HotkeyPrefix
.Show
;
158 strfmt2
.Alignment
= StringAlignment
.Far
;
159 strfmt2
.LineAlignment
= StringAlignment
.Center
;
160 strfmt2
.Trimming
= StringTrimming
.Character
;
162 strfmt3
.Alignment
= StringAlignment
.Far
;
163 strfmt3
.LineAlignment
= StringAlignment
.Near
;
164 strfmt3
.Trimming
= StringTrimming
.None
;
166 strfmt4
.Alignment
= StringAlignment
.Far
;
167 strfmt4
.LineAlignment
= StringAlignment
.Far
;
168 strfmt4
.Trimming
= StringTrimming
.EllipsisCharacter
;
170 strfmt5
.Alignment
= StringAlignment
.Far
;
171 strfmt5
.LineAlignment
= StringAlignment
.Near
;
172 strfmt5
.Trimming
= StringTrimming
.None
;
173 strfmt5
.FormatFlags
= StringFormatFlags
.DirectionVertical
;
175 strfmt6
.Alignment
= StringAlignment
.Far
;
176 strfmt6
.LineAlignment
= StringAlignment
.Far
;
177 strfmt6
.Trimming
= StringTrimming
.EllipsisCharacter
;
178 strfmt6
.FormatFlags
= StringFormatFlags
.NoWrap
;
180 Rectangle rect1
= new Rectangle (10,50,100,150);
181 Rectangle rect2
= new Rectangle (10,300,150,150);
183 Rectangle rect3
= new Rectangle (200,50,175,175);
184 Rectangle rect4
= new Rectangle (200,300,150,150);
186 Rectangle rect5
= new Rectangle (400,50,175,175);
187 Rectangle rect6
= new Rectangle (400,300,150,150);
188 Rectangle rect7
= new Rectangle (550,300, 140,120);
190 gr
.DrawRectangle( new Pen(Color
.Yellow
), rect3
);
191 gr
.DrawRectangle( new Pen(Color
.Blue
), rect4
);
193 gr
.DrawRectangle( new Pen(Color
.Yellow
), rect5
);
194 gr
.DrawRectangle( new Pen(Color
.Blue
), rect6
);
196 SolidBrush solid
= new SolidBrush(Color
.Blue
);
198 gr
.DrawString("Samples of text with different fonts and formatting",
199 new Font("Verdana",16), new SolidBrush(Color
.White
), new Rectangle (5,5,600,100), strfmttxt
);
202 gr
.FillEllipse(solid
, rect1
);
204 gr
.DrawRectangle( new Pen(Color
.Green
), rect2
);
205 gr
.DrawRectangle( new Pen(Color
.Green
), rect7
);
207 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
+ ")";
208 gr
.DrawString( str
, f1
, new SolidBrush(Color
.White
), rect1
, strfmt1
);
209 gr
.DrawString(flagProcessing(strfmt1
), fonttxt
, brushtxt
, calcRect(rect1
), strfmttxt
);
210 sz
= gr
.MeasureString (str
, f1
, new SizeF (rect1
.Width
, rect1
.Height
), strfmt1
, out chars
, out lines
);
211 Console
.WriteLine("MeasureString str1 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
213 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
+ ")";
214 gr
.DrawString(str
, f2
, new SolidBrush(Color
.Red
),rect2
, strfmt2
);
215 gr
.DrawString(flagProcessing(strfmt2
), fonttxt
, brushtxt
, calcRect(rect2
), strfmttxt
);
216 sz
= gr
.MeasureString (str
, f2
, new SizeF (rect2
.Width
, rect2
.Height
), strfmt2
, out chars
, out lines
);
217 Console
.WriteLine("MeasureString str2 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
219 str
= "Vull cantar a les pedres, la terra, l'aigua, al blat i al camí, que vaig trepitjant. (" + f3
.Name
+ ")";
220 gr
.DrawString(str
,f3
, new SolidBrush(Color
.White
), rect3
, strfmt3
);
221 gr
.DrawString(flagProcessing(strfmt3
), fonttxt
, brushtxt
, calcRect(rect3
), strfmttxt
);
222 sz
= gr
.MeasureString (str
, f3
, new SizeF (rect3
.Width
, rect3
.Height
), strfmt3
, out chars
, out lines
);
223 Console
.WriteLine("MeasureString str3 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
225 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
+ ")";
226 gr
.DrawString(str
, f4
, new SolidBrush(Color
.Red
),rect4
, strfmt4
);
227 gr
.DrawString(flagProcessing(strfmt4
), fonttxt
, brushtxt
, calcRect(rect4
), strfmttxt
);
228 sz
= gr
.MeasureString (str
, f4
, new SizeF (rect4
.Width
, rect4
.Height
), strfmt4
, out chars
, out lines
);
229 Console
.WriteLine("MeasureString str4 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
231 str
= "Vull cantar a les pedres, la terra, l'aigua, al blat i al camí, que vaig trepitjant. (" + f5
.Name
+ ")";
232 gr
.DrawString(str
, f5
, new SolidBrush(Color
.White
), rect5
, strfmt5
);
233 gr
.DrawString(flagProcessing(strfmt5
), fonttxt
, brushtxt
, calcRect(rect5
), strfmttxt
);
234 sz
= gr
.MeasureString (str
, f5
, new SizeF (rect5
.Width
, rect5
.Height
), strfmt5
, out chars
, out lines
);
235 Console
.WriteLine("MeasureString str4 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
237 str
= "Ve a besar-me el rostre (" + f6
.Name
+ ")";
238 gr
.DrawString(str
, f6
, new SolidBrush(Color
.Red
),rect6
, strfmt6
);
239 gr
.DrawString(flagProcessing(strfmt6
), fonttxt
, brushtxt
, calcRect(rect6
), strfmttxt
);
240 sz
= gr
.MeasureString (str
, f6
, new SizeF (rect6
.Width
, rect6
.Height
), strfmt6
, out chars
, out lines
);
241 Console
.WriteLine("MeasureString str6 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
243 str
= "Vull plorar amb aquells que es troben tots sols, sense cap amor van passant pel món.. (" + f5
.Name
+ ")";
244 gr
.DrawString(str
, f5
, new SolidBrush(Color
.White
), rect7
, strfmt4
);
245 gr
.DrawString(flagProcessing(strfmt4
), fonttxt
, brushtxt
, calcRect(rect7
), strfmttxt
);
246 sz
= gr
.MeasureString (str
, f5
, new SizeF (rect7
.Width
, rect7
.Height
), strfmt5
, out chars
, out lines
);
247 Console
.WriteLine("MeasureString str7 [" + str
+ "] " + sz
+ ";chars:" + chars
+ " lines:" + lines
);
251 Font f8
= new Font("Verdana", 10);
252 Font f9
= new Font("Verdana", 6);
253 Font f10
= new Font("Verdana", 12);
254 Font f11
= new Font("Verdana", 14);
256 Rectangle rect8
= new Rectangle (10, 550,100,100);
257 Rectangle rect9
= new Rectangle (150,550, 100,100);
258 Rectangle rect10
= new Rectangle (300,550, 100,100);
259 Rectangle rect11
= new Rectangle (420,550, 100,100);
260 Rectangle rect12
= new Rectangle (530,550, 200,100);
261 Rectangle rect13
= new Rectangle (530,600, 200,100);
262 Rectangle rect14
= new Rectangle (530,650, 200,100);
264 gr
.DrawRectangle( new Pen(Color
.Yellow
), rect8
);
265 gr
.DrawRectangle( new Pen(Color
.Yellow
), rect9
);
266 gr
.DrawRectangle( new Pen(Color
.Yellow
), rect10
);
268 StringFormat strfmt8
= new StringFormat();
269 strfmt8
.Alignment
= StringAlignment
.Center
;
270 strfmt8
.LineAlignment
= StringAlignment
.Near
;
271 strfmt8
.Trimming
= StringTrimming
.EllipsisCharacter
;
272 strfmt8
.HotkeyPrefix
= HotkeyPrefix
.Show
;
274 StringFormat strfmt9
= new StringFormat();
275 strfmt9
.Alignment
= StringAlignment
.Center
;
276 strfmt9
.LineAlignment
= StringAlignment
.Center
;
277 strfmt9
.Trimming
= StringTrimming
.EllipsisCharacter
;
278 strfmt9
.HotkeyPrefix
= HotkeyPrefix
.None
;
280 StringFormat strfmt10
= new StringFormat();
281 strfmt10
.Alignment
= StringAlignment
.Center
;
282 strfmt10
.LineAlignment
= StringAlignment
.Near
;
283 strfmt10
.Trimming
= StringTrimming
.Word
;
284 strfmt10
.HotkeyPrefix
= HotkeyPrefix
.Show
;
286 StringFormat strfmt11
= new StringFormat();
287 strfmt11
.Alignment
= StringAlignment
.Center
;
288 strfmt11
.LineAlignment
= StringAlignment
.Near
;
289 strfmt11
.Trimming
= StringTrimming
.Word
;
290 strfmt11
.HotkeyPrefix
= HotkeyPrefix
.Show
;
291 strfmt11
.FormatFlags
= StringFormatFlags
.DirectionRightToLeft
;
293 StringFormat strfmt12
= new StringFormat();
294 float[] tabs
= {10, 20, 30}
;
295 strfmt12
.Alignment
= StringAlignment
.Center
;
296 strfmt12
.LineAlignment
= StringAlignment
.Near
;
297 strfmt12
.Trimming
= StringTrimming
.Word
;
298 strfmt12
.HotkeyPrefix
= HotkeyPrefix
.Show
;
299 strfmt12
.SetTabStops(20, tabs
);
301 StringFormat strfmt13
= new StringFormat();
302 float[] tabs2
= {5, 50, 60}
;
303 strfmt13
.Alignment
= StringAlignment
.Center
;
304 strfmt13
.LineAlignment
= StringAlignment
.Near
;
305 strfmt13
.Trimming
= StringTrimming
.Word
;
306 strfmt13
.HotkeyPrefix
= HotkeyPrefix
.Show
;
307 strfmt13
.SetTabStops(0, tabs2
);
309 StringFormat strfmt14
= new StringFormat();
310 strfmt14
.Alignment
= StringAlignment
.Center
;
311 strfmt14
.LineAlignment
= StringAlignment
.Near
;
312 strfmt14
.Trimming
= StringTrimming
.Word
;
313 strfmt14
.HotkeyPrefix
= HotkeyPrefix
.Show
;
314 strfmt14
.FormatFlags
= StringFormatFlags
.DirectionRightToLeft
;
316 str
= "Vull alçar la veu,per cantar als homes que han nascut dempeus (" + f8
.Name
+ ")";
317 gr
.DrawString(str
, f8
, new SolidBrush(Color
.White
), rect8
, strfmt8
);
318 gr
.DrawString(flagProcessing(strfmt8
), fonttxt
, brushtxt
, calcRect(rect8
), strfmttxt
);
319 sz
= gr
.MeasureString (str
, f8
, new SizeF (rect8
.Width
, rect8
.Height
), strfmt8
, out chars
, out lines
);
320 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect8
.X
, rect8
.Y
, (int)sz
.Width
, (int)sz
.Height
));
322 str
= "I no tinc l'ànima morta i em sento bollir la sang (" + f9
.Name
+ ")";
323 gr
.DrawString(str
, f9
, new SolidBrush(Color
.White
), rect9
, strfmt9
);
324 gr
.DrawString(flagProcessing(strfmt9
), fonttxt
, brushtxt
, calcRect(rect9
), strfmttxt
);
325 sz
= gr
.MeasureString (str
, f9
, new SizeF (rect9
.Width
, rect9
.Height
), strfmt9
, out chars
, out lines
);
326 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect9
.X
, rect9
.Y
, (int)sz
.Width
, (int)sz
.Height
));
328 str
= "I no tinc l'ànima morta i em sento bollir la sang (" + f10
.Name
+ ")";
329 gr
.DrawString(str
, f10
, new SolidBrush(Color
.White
), rect10
, strfmt10
);
330 gr
.DrawString(flagProcessing(strfmt10
), fonttxt
, brushtxt
, calcRect(rect10
), strfmttxt
);
331 sz
= gr
.MeasureString (str
, f10
, new SizeF (rect10
.Width
, rect10
.Height
), strfmt10
, out chars
, out lines
);
332 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect10
.X
, rect10
.Y
, (int)sz
.Width
, (int)sz
.Height
));
334 str
= "I no tinc l'ànima morta i em sento bollir la sang (" + f11
.Name
+ ")";
335 gr
.DrawString(str
, f11
, new SolidBrush(Color
.White
), rect11
, strfmt11
);
336 gr
.DrawString(flagProcessing(strfmt11
), fonttxt
, brushtxt
, calcRect(rect11
), strfmttxt
);
337 sz
= gr
.MeasureString (str
, f11
, new SizeF (rect11
.Width
, rect11
.Height
), strfmt11
, out chars
, out lines
);
338 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect11
.X
, rect11
.Y
, (int)sz
.Width
, (int)sz
.Height
));
340 str
= "Tab1\tTab2\tTab3";
341 gr
.DrawString(str
, f8
, new SolidBrush(Color
.White
), rect12
, strfmt12
);
342 sz
= gr
.MeasureString (str
, f8
, new SizeF (rect12
.Width
, rect12
.Height
), strfmt12
, out chars
, out lines
);
343 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect12
.X
, rect12
.Y
, (int)sz
.Width
, (int)sz
.Height
));
345 str
= "Nom\tCognom\tAdreça";
346 gr
.DrawString(str
, f8
, new SolidBrush(Color
.White
), rect13
, strfmt13
);
347 sz
= gr
.MeasureString (str
, f8
, new SizeF (rect13
.Width
, rect13
.Height
), strfmt13
, out chars
, out lines
);
348 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect13
.X
, rect13
.Y
, (int)sz
.Width
, (int)sz
.Height
));
350 str
= "Nom Cognom Adreça";
351 gr
.DrawString(str
, f8
, new SolidBrush(Color
.White
), rect14
, strfmt14
);
352 sz
= gr
.MeasureString (str
, f8
, new SizeF (rect14
.Width
, rect13
.Height
), strfmt14
, out chars
, out lines
);
353 gr
.DrawRectangle(new Pen(Color
.Red
), new Rectangle (rect14
.X
, rect14
.Y
, (int)sz
.Width
, (int)sz
.Height
));
355 bmp
.Save("fontDrawingAdv.bmp", ImageFormat
.Bmp
);