woklet Posted March 9, 2005 Posted March 9, 2005 I'm looking for source code on how to convert an entire HTML page into a BITMAP image. Not a screenshot; I'm able to get that. But my BITMAP needs to save what is not currently displayed as well(scroll). Any help would be greatly appreciated! Thanks in advance Ben Quote
woklet Posted March 11, 2005 Author Posted March 11, 2005 The following code will produce a �form� screen shot Private Sub btnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSave.Click ' Save the picture. Dim bm As Bitmap = GetFormImage() bm.Save("c:\newread\test.tif", System.Drawing.Imaging.ImageFormat.Tiff) 'Beep() End Sub Private Function GetFormImage() As Bitmap Dim me_gr As Graphics = Me.CreateGraphics Dim bm As New Bitmap(Me.ClientSize.Width, _ Me.ClientSize.Height, me_gr) Dim bm_gr As Graphics = me_gr.FromImage(bm) Dim bm_hdc As IntPtr = bm_gr.GetHdc Dim me_hdc As IntPtr = me_gr.GetHdc BitBlt(bm_hdc, 0, 0, Me.ClientSize.Width, _ Me.ClientSize.Height, _ me_hdc, 0, 0, SRCCOPY) me_gr.ReleaseHdc(me_hdc) bm_gr.ReleaseHdc(bm_hdc) Return bm End Function You�ll need to declare the BitBlt function and the variable SRCCOPY after your Public Class Form1 Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal _ hdcDest As IntPtr, ByVal nXDest As Integer, ByVal _ nYDest As Integer, ByVal nWidth As Integer, ByVal _ nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc _ As Integer, ByVal nYSrc As Integer, ByVal dwRop As _ System.Int32) As Boolean Private Const SRCCOPY As Integer = &HCC0020 Quote
woklet Posted March 11, 2005 Author Posted March 11, 2005 Now, as far as rendering the HTML page to a bitmap, I�ve come this far (doing my best to interpret Rob Manderson�s C++ article into VB.NET). I don�t get an error when I render my HTML page to the device context; at least that�s what I think I�m doing. I only now need to get my device context rendering into a bitmap, but I�m stuck. Do I need to use the BitBlt function? Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click Dim doc As mshtml.HTMLDocument = DirectCast(Me.AxWebBrowser1.Document, mshtml.HTMLDocument) Dim bodyElement As mshtml.IHTMLElement Dim render As mshtml.IHTMLElementRender Dim render2 As IHTMLElementRender Dim dd As mshtml.HTMLBody bodyElement = doc.body render2 = bodyElement Dim BitMap As New Bitmap(600, 400) Dim g As Graphics = Graphics.FromImage(BitMap) Dim memDC As IntPtr memDC = g.GetHdc() render2.DrawToDC(memDC) Try BitMap.FromHbitmap(memDC) Catch exc As Exception MsgBox(Err.Description) MsgBox(exc.ToString) End Try BitMap.Save("c:\newread\test.tif", System.Drawing.Imaging.ImageFormat.Tiff) MsgBox("Image created!") End Sub The following code is after your imports but before your Public Class Form1 <ComVisible(True), ComImport(), _ Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"), _ InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _ Interface IHTMLElementRender Sub DrawToDC(<[in]()> ByVal hDC As IntPtr) Sub SetDocumentPrinter(<[in](), MarshalAs(UnmanagedType.BStr)> ByVal _ bstrPrinterName As String, <[in]()> ByVal hDC As IntPtr) End Interface 'IHTMLElementRender It must be needed so DrawToDc accepts a normal pointer rather than a remotable handle Any help would be appreciated! Thank in advance Ben Quote
Tygur Posted March 12, 2005 Posted March 12, 2005 Here is a command line program that will save a page to an image file: http://iecapt.sourceforge.net/ The source code is available as well, but if you're gonna use it, it'd be easiest to just have your app call it with the relevant command line arguments. Quote
woklet Posted March 14, 2005 Author Posted March 14, 2005 Thanks much, Tygur I downloaded that program and it works really quite well. I'm set for now. In my spare time I can try to translate the C++ code into VB.NET code. (If it's possible...) Thanks again Quote
JDYoder Posted March 15, 2005 Posted March 15, 2005 FYI, there are various C++ to VB.Net converters. This one converts code snippets right on your webpage... http://www.developerfusion.com/utilities/convertcsharptovb.aspx Though if you ever want to get something that is really robust and will convert tons of code at once, there's this product, which has a demo... http://www.tangiblesoftwaresolutions.com/Product_Details/Instant_VB/Instant_VB.htm Quote
woklet Posted May 2, 2005 Author Posted May 2, 2005 Trouble in Rivercity... Hello once again. I ended up downloading the IECapt.exe program, which does a great job of capturing a url to an image. However, if the url is generic and not a direct reflection of the web page itself, it does me no good. Is there anyone who could help me save the contents of my axWebBrowser to an image??? I would be forever grateful. Thanks again in advance, Ben Quote
woklet Posted May 27, 2005 Author Posted May 27, 2005 Well I figured out how to capture the Web Browser, scroll down a page, capture again, and continue until I'm at the end. Then I just concatenate all the images together into one. It's "flashy" and a "tad too early 90s", but it works. Anyone have a method of capturing the Web Browser in one quick, swift stroke? VB.NET Thanks Ben Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.