Save webbrowser content as an image

Source: Internet
Author: User

Overview

The Flash Dynamic Chart fusioncharts free was recently used in winform development. It is an open-source flash component. The commercial version can use fusioncharts V3, and the free version is fusioncharts
Free v2.2. : Http://www.fusioncharts.com/free/download /. You can check out the results. The commercial version provides more charts, PDF, image, CSV, and other functions than the free version.

Generally, flash components are displayed on webpages in the form of flash. Therefore, if you want to use flash charts in winform, you may think there are two solutions. 1.
Use the webbrowser control to execute a webpage containing the flash URL. 2. Use the flash COM component Shockwave Flash. The first method is relatively simple, and the second one thinks that the best one is to use a flex component as a container, so that different charts can be displayed based on the passed parameters.

Because the free version does not provide functions such as exporting PDF, image, and CSV files, you have to write them on your own. In addition, it is loaded to the webbrowser control, so I have extended the webbrowser. Currently, commonly used flash charts can be displayed and exported in PDF, image, Excel, and other formats.

Preview Related Images

First, let's take a few pictures to give you an idea.

  

Figure 1. exported Image


Figure 2. exported PDF

Problems encountered

1.As the title saysWebbrowser.Drawtobitmap is a problem, because this control does not have this method, but you will not make an error when using it, but the problem arises. Sometimes you can export images and sometimes you cannot, the loaded webpage does not contain Ajax content. There are many solutions to this problem on the Internet, but what you see is basically wrong. A common practice is as follows:

Code

Publicvoid writebmp (string BMP path)
{
While (webbrowser. readystate
! = Webbrowserreadystate. Complete)
{
Application. doevents ();
}
System. Drawing. Rectangle r = new system. Drawing. rectangle (point. empty,
New size (Int. parse (webbrowser. Document. Body. getattribute ("scrollwidth ")),
Int. parse (webbrowser. Document. Body. getattribute ("scrollheight "))));
Bitmap BMP = new Bitmap (R. Width, R. Height );
Webbrowser. drawtobitmap (BMP, R );
BMP. Save (BMP path );
BMP. Dispose ();
}

As a result, the problem may occur from time to time. There are good solutions for this problem for overseas sites:
1. http://bytes.com/topic/c-sharp/answers/605264-taking-screenshot-webbrowser-tab.
2.

Http://efreedom.com/Question/1-2434156/WebBrowser-DrawToBitmap-Methods

Based on the above method, I listed the code for your reference. I have used the above two methods to achieve great results (the above method is generated as follows ):

Code

[Dllimport ("user32.dll")]
Privatestaticexternbool printwindow (intptr hwnd, intptr hdcblt,
Uint nflags );

Publicvoid writebmp (string BMP path)
{

Int screenwidth
= Webbrowser. Document. Body. scrollrectangle. width;
Int screenheight
= Webbrowser. Document. Body. scrollrectangle. height;

Intptr myintptr = webbrowser. Handle;
Int hwndint
= Myintptr. toint32 ();
Intptr hwnd = myintptr;

// Set HDC to the bitmap

Bitmap Bm = new Bitmap (screenwidth, screenheight );
Graphics G = graphics. fromimage (BM );
Intptr HDC = G. gethdc ();

// Snapshot The webbrowser

Bool result
= Printwindow (hwnd, HDC,
0 );
G. releasehdc (HDC );
G. Flush ();

// Save the bitmap, if successful

If (Result
= True)
BM. Save (BMP path );
}

2nd methods:

Code

Publicstaticclass apiconstants
{
Publicconstint srccopy
= 13369376;
}

Publicstaticclass utilities
{
Publicstatic image capturescreen ()
{
Return capturewindow (user32.getdesktopwindow ());
}

Publicstatic image capturewindow (intptr handle)
{

Intptr hdcsrc = user32.getwindowdc (handle );

Rect windowrect = new rect ();
User32.getwindowrect (handle, ref windowrect );

Int width
= Windowrect. Right
-Windowrect. Left;
Int height
= Windowrect. Bottom
-Windowrect. Top;

Intptr hdcdest = gdi32.createcompatibledc (hdcsrc );
Intptr hbitmap = gdi32.createcompatiblebitmap (hdcsrc, width, height );

Intptr hold = gdi32.selectobject (hdcdest, hbitmap );
Gdi32.bitblt (hdcdest, 0,
0, width, height, hdcsrc,
0, 0, apiconstants. srccopy );
Gdi32.selectobject (hdcdest, hold );
Gdi32.deletedc (hdcdest );
User32.releasedc (handle, hdcsrc );

Image image = image. fromhbitmap (hbitmap );
Gdi32.deleteobject (hbitmap );

Return image;
}
}

Publicstaticclass USER32
{
[Dllimport ("user32.dll")]
Publicstaticextern intptr getasktopwindow ();
[Dllimport ("user32.dll")]
Publicstaticextern intptr getwindowdc (intptr hwnd );
[Dllimport ("user32.dll")]
Publicstaticextern intptr getwindowrect (intptr hwnd,
Ref rect );
[Dllimport ("user32.dll")]
Publicstaticextern intptr releasedc (intptr hwnd, intptr HDC );
}

Publicclass GDI32
{
[Dllimport ("gdi32.dll")]
Publicstaticexternbool bitblt (intptr hobject,
Int nxdest,
Int nydest, int nwidth,
Int nheight, intptr hobjectsource,
Int nxsrc,
Int nysrc, int dwrop );
[Dllimport ("gdi32.dll")]
Publicstaticextern intptr createcompatiblebitmap (intptr HDC,
Int nwidth,
Int nheight );
[Dllimport ("gdi32.dll")]
Publicstaticextern intptr createcompatibledc (intptr HDC );
[Dllimport ("gdi32.dll")]
Publicstaticexternbool deletedc (intptr HDC );
[Dllimport ("gdi32.dll")]
Publicstaticexternbool deleteobject (intptr hobject );
[Dllimport ("gdi32.dll")]
Publicstaticextern intptr SelectObject (intptr HDC, intptr hobject );
}

[Structlayout (layoutkind. Sequential)]
Publicstruct rect
{
Publicint left;
Publicint top;
Publicint right;
Publicint bottom;
}

// If it is A. Net extension method, if your. Net Library version is relatively high, remove the comment
// Public static class controlextensions
//{
// Public static image drawtoimage (this control Control)
//{
// Return utilities. capturewindow (control. Handle );
//}
//}

2. For information on exporting PDF files, you can download the itextsharp open-source component. For more information, see.

1. http://itext0000.com/book/examples.php. Java version

2. http://www.cnblogs.com/islands/archive/2008/06/27/1231288.html

Summary

The above is my little experience in developing flash dynamic charts. I will write this article to share it with you. The purpose of this article is to provide a solution for a friend who has encountered similar problems. After you search Google, search for Baidu and find that it cannot solve your problem. Maybe this article can help you.

By jackhuclan
Source: http://jackhuclan.cnblogs.com/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.