1111// "The Art of War"
1212
1313using System ;
14+ using System . Drawing ;
15+ using System . Drawing . Imaging ;
1416using System . Drawing . Printing ;
15- using System . IO ;
16- using System . Reflection ;
17- using System . Text ;
18- using System . Text . RegularExpressions ;
19- using System . Threading ;
2017using System . Windows . Forms ;
2118
2219namespace HtmlRenderer . Demo
@@ -29,7 +26,9 @@ static class Program
2926 [ STAThread ]
3027 private static void Main ( )
3128 {
32- // TestPrint();
29+ // TestPrint();
30+ // TestImage();
31+ // return;
3332
3433 Application . EnableVisualStyles ( ) ;
3534 Application . SetCompatibleTextRenderingDefault ( false ) ;
@@ -39,97 +38,32 @@ private static void Main()
3938// Application.Run(new PerfForm());
4039
4140// PerfForm.Run();
42-
43- // ObfuscateHtml();
4441 }
4542
46- private static void TestPrint ( )
43+ private static void TestImage ( )
4744 {
48- // string html = File.ReadAllText(@"c:\source\html.txt");
49- string html = File . ReadAllText ( @"C:\Source\GitHub\HTML-Renderer\Source\Demo\Samples\02.text.htm" ) ;
50-
51- var doc = new PrintDocument ( ) ;
52- doc . PrintPage += ( sender , args ) =>
53- {
54- var fact = args . Graphics . DpiX / 96f ;
55-
56- // args.Graphics.PageScale = 1/fact;
57-
58- // var font = new Font("Arial", 10*fact);
59- // TextRenderer.DrawText(args.Graphics,"Hello World of PDF", font, new Point((int) (50*fact),(int) (50*fact)), Color.Red);
60-
61- // var font2 = new Font("Arial", 10);
62- // args.Graphics.DrawString("Hello World of PDF", font2, Brushes.DarkGreen, new PointF(50, 50));
63-
64- // Point location = new Point((int) (args.MarginBounds.Location.X*fact), (int) (args.MarginBounds.Location.Y*fact));
65- // Size maxSize = new Size((int)(args.MarginBounds.Size.Width * fact), (int) (args.MarginBounds.Size.Height*fact));
66- // HtmlRender.Render(args.Graphics, html, args.MarginBounds.Location, args.MarginBounds.Size);
67-
68- var image = HtmlRender . RenderToImage ( html , args . MarginBounds . Size ) ;
69-
70- args . Graphics . DrawImageUnscaled ( image , args . MarginBounds . Location ) ;
71- } ;
72- // doc.Print();
73-
74- // var bitmap = HtmlRender.RenderToImage(File.ReadAllText(@"c:\source\html.txt"), 350, 500);
75- // var bitmap = HtmlRender.RenderToImage(html, 600);
76- // bitmap.Save(@"c:\source\test.png", ImageFormat.Png);
45+ var html1 = "<p style='font-size:2em;color: gray'>This is an <b>HtmlLabel</b> on transparent background with <span style='color: red'>colors</span> and links: <a href='http://htmlrenderer.codeplex.com/'>HTML Renderer</a></p>" ;
46+ var img = HtmlRender . RenderToImageGdiPlus ( html1 , new Size ( 600 , 200 ) ) ;
47+ // var img = HtmlRender.RenderToImage(html, new Size(600, 200));
48+ img . Save ( @"d:\test1.png" , ImageFormat . Png ) ;
49+
50+ var image = Image . FromFile ( @"d:\b.png" ) ;
51+ HtmlRender . RenderToImage ( image , html1 , new Point ( 50 , 0 ) , new Size ( 620 , 0 ) ) ;
52+ image . Save ( @"d:\test2.png" ) ;
53+
54+ var html2 = "<html><body style=\" margin: 0px; height: 461px; background-image:url(d:/b.png);background-repeat:no-repeat;\" ><h1 style=\" margin-top:40px;margin-left:150px;\" >"
55+ + "<span style=\" font-family:Monotype Corsiva;\" ><em>Test HTML</em></span></h1><div style=\" width:560px;height:200px;margin-top:120px;margin-right:55px;margin-left:150px;\" ><em>"
56+ + "<span style=\" font-family:Monotype Corsiva;\" ><span style=\" font-size:22px;\" >Some Text Here</span></span></em></div></body></html>" ;
57+ var img2 = HtmlRender . RenderToImage ( html2 , new Size ( 755 , 461 ) ) ;
58+ img2 . Save ( @"d:\test3.png" , ImageFormat . Png ) ;
7759 }
7860
79- /// <summary>
80- /// Used so html samples can be added where the content of the html is confidential.
81- /// </summary>
82- public static void ObfuscateHtml ( )
83- {
84- var html = GetHtmlFromResource ( "HtmlRenderer.Demo.PerfSamples.2.Tables.htm" ) ;
85- var sb = new StringBuilder ( html ) ;
86-
87- var rand = new Random ( ) ;
88-
89- int idx = 0 ;
90- while ( idx < html . Length )
91- {
92- var tagIdx = html . IndexOf ( '<' , idx ) ;
93- if ( tagIdx < 0 )
94- tagIdx = html . Length ;
95-
96- for ( int i = idx ; i < tagIdx ; i ++ )
97- {
98- if ( char . IsLetter ( html , i ) )
99- {
100- sb [ i ] = Convert . ToChar ( 97 + rand . Next ( 18 ) ) ;
101- }
102- }
103-
104- if ( tagIdx < html . Length )
105- {
106- tagIdx = html . IndexOf ( '>' , tagIdx ) ;
107- if ( tagIdx < 0 )
108- tagIdx = html . Length ;
109- }
110-
111- idx = tagIdx ;
112- }
113-
114- var result = sb . ToString ( ) ;
115-
116- result = Regex . Replace ( result , "href\\ W*=\\ W*[\" '].*?[\" ']" , "href='http://www.google.com'" ) ;
117- }
118-
119- /// <summary>
120- /// Get html from resource by resource name.
121- /// </summary>
122- private static string GetHtmlFromResource ( string name )
61+ private static void TestPrint ( )
12362 {
124- var htmlStream = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( name ) ;
125- if ( htmlStream != null )
126- {
127- using ( StreamReader sreader = new StreamReader ( htmlStream , Encoding . Default ) )
128- {
129- return sreader . ReadToEnd ( ) ;
130- }
131- }
132- return null ;
63+ var html = "<p style='font-size:3em;color: gray'>This is an <b>HtmlLabel</b> on transparent background with <span style='color: red'>colors</span> and links: <a href='http://htmlrenderer.codeplex.com/'>HTML Renderer</a></p>" ;
64+ var doc = new PrintDocument ( ) ;
65+ doc . PrintPage += ( sender , args ) => HtmlRender . RenderGdiPlus ( args . Graphics , html , args . MarginBounds . Location , args . MarginBounds . Size ) ;
66+ doc . Print ( ) ;
13367 }
13468 }
13569}
0 commit comments