Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

WebBrowser capture image to a picturebox

Posted on 2007-07-30
16
Medium Priority
?
4,379 Views
Last Modified: 2010-07-27
I have a Project with WebBrowser control.
After navigating a site I would like to capture an image that is on the page and display that image into a picturebox.
I would like to accomplish that without creating additional request to the server. I.E. once the page is loaded and all images displayed on the webbrowser control capture a specific image and display it in a picturebox.

(for example navigate google.com and then display the google logo in a picturebox / without creating additional server request)

Thanks in advance,
0
Comment
Question by:Teknosoft
  • 6
  • 5
  • 2
  • +3
16 Comments
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 19596803
you could use bitblt to paint the image in a picturebox, or maybe its possible to find the temp path the pictures are stored in and extract them somehow
0
 

Author Comment

by:Teknosoft
ID: 19597175
Mark,
Thank you for the reply, I would love to get the temp path to display the image. any idea on how to get that done?
I am not sure if what is being rendered to the webbrowser control can be accessable via document elements.
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 2000 total points
ID: 19599341

here you go, an example of retrieving file paths from an image name.
i hardcoded the path of the site and image, but im sure you can find a way to extract them from the webbrowser

the function does accept wildcards.

create a new project,
add one command button to your form, one webbrowser and one picturebox
make sure your form is big enough, so you can see the webbrowser and the picturebox clearly


add this code to your form, run your project and click on the button.









Option Explicit

Private Const ERROR_CACHE_FIND_FAIL As Long = 0
Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122

Private Const LMEM_FIXED As Long = &H0

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Type INTERNET_CACHE_ENTRY_INFO
   dwStructSize As Long
   lpszSourceUrlName As Long
   lpszLocalFileName As Long
   CacheEntryType  As Long
   dwUseCount As Long
   dwHitRate As Long
   dwSizeLow As Long
   dwSizeHigh As Long
   LastModifiedTime As FILETIME
   ExpireTime As FILETIME
   LastAccessTime As FILETIME
   LastSyncTime As FILETIME
   lpHeaderInfo As Long
   dwHeaderInfoSize As Long
   lpszFileExtension As Long
   dwExemptDelta  As Long
End Type

Private Declare Function FindFirstUrlCacheEntry Lib "wininet" Alias "FindFirstUrlCacheEntryA" (ByVal lpszUrlSearchPattern As String, lpFirstCacheEntryInfo As Any, lpdwFirstCacheEntryInfoBufferSize As Long) As Long
Private Declare Function FindNextUrlCacheEntry Lib "wininet" Alias "FindNextUrlCacheEntryA" (ByVal hEnumHandle As Long, lpNextCacheEntryInfo As Any, lpdwNextCacheEntryInfoBufferSize As Long) As Long
Private Declare Function FindCloseUrlCache Lib "wininet" (ByVal hEnumHandle As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function lstrcpyA Lib "kernel32" (ByVal RetVal As String, ByVal Ptr As Long) As Long
Private Declare Function lstrlenA Lib "kernel32" (ByVal Ptr As Any) As Long
Private Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal uBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long



Private Sub Command1_Click()
    WebBrowser1.Navigate2 "www.isoftware.nl"
    While WebBrowser1.Busy
        DoEvents
    Wend
    Picture1.Picture = LoadPicture(GetCacheURL("http://www.isoftware.nl/images/iSoftware.gif"))
End Sub


Private Function GetCacheURL(strIn As String) As String
Dim ICEI As INTERNET_CACHE_ENTRY_INFO
Dim hFile As Long
Dim cachefile As String
Dim dwBuffer As Long
Dim pntrICE As Long
   
    dwBuffer = 0
    hFile = FindFirstUrlCacheEntry(vbNullString, ByVal 0, dwBuffer)
    If (hFile = ERROR_CACHE_FIND_FAIL) And (Err.LastDllError = ERROR_INSUFFICIENT_BUFFER) Then
        pntrICE = LocalAlloc(LMEM_FIXED, dwBuffer)
        If pntrICE Then
            CopyMemory ByVal pntrICE, dwBuffer, 4
            hFile = FindFirstUrlCacheEntry(vbNullString, ByVal pntrICE, dwBuffer)
            If hFile <> ERROR_CACHE_FIND_FAIL Then
                Do
                    CopyMemory ICEI, ByVal pntrICE, Len(ICEI)
                    If (ICEI.CacheEntryType And &H40) Then
                        cachefile = GetStrFromPtrA(ICEI.lpszSourceUrlName)
                        If cachefile Like strIn Then
                            GetCacheURL = GetStrFromPtrA(ICEI.lpszLocalFileName)
                            GoTo endMeNow
                        End If
                    End If
                    Call LocalFree(pntrICE)
                    dwBuffer = 0
                    Call FindNextUrlCacheEntry(hFile, ByVal 0, dwBuffer)
                    pntrICE = LocalAlloc(LMEM_FIXED, dwBuffer)
                    CopyMemory ByVal pntrICE, dwBuffer, 4
                Loop While FindNextUrlCacheEntry(hFile, ByVal pntrICE, dwBuffer)
            End If
        End If
    End If
endMeNow:
    Call LocalFree(pntrICE)
    Call FindCloseUrlCache(hFile)
End Function


Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
    GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
    Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Teknosoft
ID: 19608747
Mark,
Thank you for the provided solution. You have provided a good (complete) example on how to get this done.
I was able to extract all the image links within the document source. I am able to display most images but an image that is generated by the server could not be located in the cache. any other idea on how to get that done. mybe you can help me with the bitblt if that would work as I will be working with different sites that display the image in different positions!
The URL for the site I need to capture the image is "http://www.indexgiant.com/suggest-link.php" the image is captcha image. if I retrive the image from the server again it will generate different result!
I have increased the points to 500.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 19609739
Hi,

the link to the page with the captcha image is a dead link, do you have a more recent one.

thx

bcs
0
 

Author Comment

by:Teknosoft
ID: 19610195
The site should be up now I was restarting the server (installed updates to control panel), sorry
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 19610598

somehow the captcha is indeed not in the cache, probably because it is set to expire immediately
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 19611054
Hi,

still cant access that url, just me? lol

thx
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 19611129
yeah, thats probably you
i can access it just fine
0
 

Author Comment

by:Teknosoft
ID: 19611651
Babycorn-Starfish / Maybe the browser cache needs to be cleared!
Other than that is there a way to capture the image. Maybe using the BitBit method? This is becoming more challenging!!
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 19612135
yeah, bitblt it is then

but there isnt an one fits all for that, it needs to be made specific for this site,
or youll have to create image recognition or a html parser
0
 

Author Comment

by:Teknosoft
ID: 19612508
Can I use "Save Picture As" which is accessable through right click on the control over the image. and then be able to display it? just a thought.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 19612610

that would be possible i think
0
 

Expert Comment

by:Rohini_Chavakula
ID: 22077853
Hi Teknosoft,

     I too need to capture captcha image.Have You got any solutions to that problem. If so, please let me know so that it will be very much helpful for me.......

Thanks in advance.
0
 
LVL 5

Expert Comment

by:Tom Knowlton
ID: 22370954
Did this ever get solved...facing same problem.

Tom
0
 

Expert Comment

by:RoBarbieri
ID: 25945465
Please help,

I have the same problem. I need to capture a CAPTCHA image from a WebSite. Did anyone solve it?
I'm using mshtml (VB.NET). I'm able to save all the images from the site, but not the captcha image. There's a way to capture this image(in the cache, maybe) or should I use print screens or something like this?


Thanks,
Rodrigo Barbieri
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question