Easily implement killer-level application snap)

Source: Internet
Author: User

From: http://www.cnblogs.com/jessezhao/archive/2006/12/25/602656.html
It is easy to get a Windows application, but it is hard to understand how to get a webpage. In Windows application, we can use the. NET Framework 2.0 webbrowser component to obtain the website we need, and then use the. drawtobitmap function to obtain the bitmap of the current website. This component works well in Windows application. How does it work in the Web site? Can we implement the functions we want?

Do not forget that both Asp.net and Windows application run on. NET Framework. This allows us to use the implementation method of Windows Application on ASP.net application. My initial idea is to initialize a hidden webbrowser component and load our web site in it for reference. Of course, we will still face the Asp.net application and web forms application problems we mentioned earlier.

Use webbrowser Components

Dim mybrowser as new webbrowser
Mybrowser. scrollbarsenabled = false
Mybrowser. size = new size (1027,768)
Mybrowser. navigate (http://www.deveload.com ")
Dim mybitmap as new Bitmap (1024,768)
Dim drawrect as new rectangle (0, 0, 1024,768)
Mybrowser. drawtobitmap (mybitmap, drawrect)

After we define our own webbrowser variable, we will set some attributes for it, such as setting scrollbarsenabled to false. We call the navigate method to open my website "www.pinzui.cn ". Next we define the bitmap variable, the drawing area (drawrect), and call the drawtobitmap method to obtain our. It seems that everything is normal, but when you try to run the program, you will get too assumerrors. The first problem is that we cannot wait until the website we want is fully loaded by the webbrowser component. In fact, we only get an instance loaded from the website. In general, we need to wait until the website is fully loaded to obtain it.

Mybrowser. navigate (Me. url)
While mybrowser. readystate <> webbrowserreadystate. Complete
Application. doevents ()
End while

When our website starts loading, we just make a dumb loop to wait for the desired website to be fully loaded. Why should we try application. doevents ()? This is a trick. I will explain it to you later. The following is the next problem we encountered. The webbrowser space we use is actually a COM object. We need to use the webbrowser control freely in a separate thread. All our acquired programs should be controlled and run in an independent thread.

Dim newth as new threading. Thread (addressof doit)
Newth. setapartmentstate (threading. apartmentstate. Sta)
Newth. Start ()

Let's go back to the previous loop waiting for the webbrowser control. Rethink about it. We use a separate thread to wait for the website to be fully loaded without stopping the loop. Normally, this loop will cause a deadlock in our program. The control is re-delivered to webbrowser only after the web page is loaded. But why is our program not deadlocked? By the way, application. doevents () is the answer. This method will deliver control to our webbrowser control during the loop process.

Create webpage

Dim imgoutput as system. Drawing. Image = mybitmap
Dim othumbnail as system. Drawing. Image = new Bitmap (twidth, theight ,&_
Imgoutput. pixelformat)
Dim G as graphics = graphics. fromimage (othumbnail)
G. compositingquality = drawing2d. compositingquality. highspeed
G. smoothingmode = drawing2d. smoothingmode. highspeed
G. interpolationmode = drawing2d. interpolationmode. highqualitybilinear
Dim orectangle as rectangle = new rectangle (0, 0, twidth, theight)
G. drawimage (imgoutput, orectangle

In the above Code, we defined imgoutput and paid the original image named mybitmap to him. Twidth, theight is the variable indicating the target size. We create a grapics object through a dummy image with a target size. We set the properties for the graphics object and call the drawimage method to generate it.

Final class

Imports system
Imports system. Drawing
Imports system. Drawing. Imaging
Imports system. Windows. Forms
Imports system. Diagnostics
Namespace getsitethumbnail
Public class getimage
Private s_height as integer
Private s_width as integer
Private f_height as integer
Private f_width as integer
Private myurl as string
Property screenheight () as integer
Get
Return s_height
End get
Set (byval value as integer)
S_height = Value
End set
End Property
Property screenwidth () as integer
Get
Return s_width
End get
Set (byval value as integer)
S_width = Value
End set
End Property
Property imageheight () as integer
Get
Return f_height
End get
Set (byval value as integer)
F_height = Value
End set
End Property
Property imagewidth () as integer
Get
Return f_width
End get
Set (byval value as integer)
F_width = Value
End set
End Property
Property website () as string
Get
Return myurl
End get
Set (byval value as string)
Myurl = Value
End set
End Property
Sub new (byval website as string, byval screenwidth as integer ,&_
Byval screenheight as integer, byval imagewidth as integer ,&_
Byval imageheight as integer)
Me. Website = website
Me. screenwidth = screenwidth
Me. screenheight = screenheight
Me. imageheight = imageheight
Me. imagewidth = imagewidth
End sub
Function getbitmap () as bitmap
Dim shot as new webpagebitmap (Me. Website, me. screenwidth ,&_
Me. screenheight)
Shot. getit ()
Dim PIC as bitmap = shot. drawbitmap (Me. imageheight ,&_
Me. imagewidth)
Return PIC
End Function
End Class
Class webpagebitmap
Dim mybrowser as webbrowser
Dim URL as string
Dim height as integer
Dim width as integer
Sub new (byval URL as string, byval width as integer ,&_
Byval height as integer)
Me. Height = height
Me. width = width
Me. url = URL
Mybrowser = new webbrowser
Mybrowser. scrollbarsenabled = false
Mybrowser. size = new size (Me. Width, me. Height)
End sub
Sub getit ()
Mybrowser. navigate (Me. url)
While mybrowser. readystate <> webbrowserreadystate. Complete
Application. doevents ()
End while
End sub
Function drawbitmap (byval theight as integer ,&_
Byval twidth as integer) as bitmap
Dim mybitmap as new Bitmap (width, height)
Dim drawrect as new rectangle (0, 0, width, height)
Mybrowser. drawtobitmap (mybitmap, drawrect)
Dim imgoutput as system. Drawing. Image = mybitmap
Dim othumbnail as system. Drawing. Image = new Bitmap (twidth ,&_
Theight, imgoutput. pixelformat)
Dim G as graphics = graphics. fromimage (othumbnail)
G. compositingquality = drawing2d. compositingquality. highspeed
G. smoothingmode = drawing2d. smoothingmode. highspeed
G. interpolationmode = drawing2d. interpolationmode. highqualitybilinear
Dim orectangle as rectangle = new rectangle (0, 0, twidth, theight)
G. drawimage (imgoutput, orectangle)
Try
Return othumbnail
Catch ex as exception
Finally
Imgoutput. Dispose ()
Imgoutput = nothing
Mybrowser. Dispose ()
Mybrowser = nothing
End try
End Function
End Class
End namespace

How to use code?

Partial class _ default
Inherits system. Web. UI. Page
Protected sub button#click (byval sender as object ,&_
Byval e as system. eventargs) handles button1.click
Dim newth as new threading. Thread (addressof doit)
Newth. setapartmentstate (threading. apartmentstate. Sta)
Newth. Start ()
End sub
Sub doit ()
Try
Dim thumb as new getsitethumbnail. getimage ("
1024,768,320,240)
Dim X as system. Drawing. Bitmap = thumb. getbitmap ()
X. Save ("C: \ Inetpub \ wwwroot \ screeny \ deveload.jpg ")
Catch ex as exception
Dim y as system. Io. streamwriter = system. Io. file. createtext ("C: \ Inetpub \ wwwroot \ screeny \ error.txt ")
Y. writeline (ex. Message & vbcrlf & Ex. Source)
Y. Flush ()
Y. Close ()
End try
End sub
End Class

The code above creates an instance of the class we wrote and retrieves the webpage. If any error occurs, a text file is created and the error is written in. But never forget that the doit class will run in an independent thread.

Conclusion (conclusion)

This system can work perfectly on more than 90% of websites. However, websites that use Ajax or flash to call external data after webpage loading are not very useful.

PS: I changed it to C #, and debugging failed for a long time. The source code is not provided in the original text. In the web application, it seems that system. Windows. form cannot be referenced. I am confused for one night. Today I will release the translated articles. Let's see if you can provide a solution or source code.

# C # column

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.