ASP.NET
Never forget that ASP.NET and Windows Forms applications are all about .NET Framework. So theoretically we can do all the things in an ASP.NET application like we do in Windows Forms. The idea behind our solution will be to initialize a hidden WebBrowser component and load our web site inside it to be able to get screenshot.
Original article can be found on Codeproject.
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
oRectangle = 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
PHP
Original article can be found here on The Pimp. Alternatively you can use third party thumbnail generators API, but your app will be dependent which you will not want.
$im = imagegrabscreen();
imagepng($im, "myscreenshot.png");
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$im = imagegrabwindow($handle);
$browser->Quit();
imagepng($im, "iesnap.png");
$im = imagegrabscreen();
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://blog.thepimp.net");
/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->FullScreen = true;
$browser->Navigate("http://blog.thepimp.net");
/* Is it completely loaded? (be aware of frames!)*/
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>
It should work with any kind of window as long as you give the correct handle (usually $obj->HWND).
- php_gd2.dll for 5.2.x thread safe build
- php gd image documentation
- IE manual
11 comments:
Will this work with ASP.NET 1.1 or just 2.0 only?
I'm getting an error trying to use your namespace...
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
I'm trying to do the following
Dim oImg As System.Drawing.Bitmap = Nothing
Using oClass As New GetSiteThumbnail.GetImage("http://www.google.com", 1000, 800, 500, 400)
oImg = oClass.GetBitmap
End Using
Response.ContentType = "image/jpeg"
oImg.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)
Response.Cache.AppendCacheExtension("cache-control:no-cache")
Response.Cache.AppendCacheExtension("pragma:no-cache")
Response.Cache.AppendCacheExtension("expires:0")
Response.End()
I've added IDisposable to your GetImage class to keep it modular. I was curious on your work around with the Multi Thread WebBrowser error listed above.
Any help would be greatly appreciated.
Thanks
Hi Matt,
I have not been using .NET for about 1 year. an i was able to run this code. but now not remember what was the problem i encoutered when i try to run.
will it work on PHP+Firefox or any other browser ???
Wonderful. Works like a charm.
you are welcome guys.
I have used this code but i got only a black color screen as a screenshot.is there any other procedure to follow pls!
I managed to work this code, but now I am getting black rectangle. No screen shot.
Anybody, who have successfull worked on this code please submit the full working code or provide the solution for the black.
Thanks in advance.
also get only black image not a screeshot
For those getting black screens you need to change your apache settings to interact with a desktop. then restart apache and all is well.
The easy way to capture website screenshot I use... www.websitesScreenshot.com
It can generate webpage thumbnail in few lines of code.
Post a Comment