There is actualy an image on this particular page which i want to save on my disk.If not the disk then atleast load it on my picturebox1.
The image has no url since it's not saved on the server.
I simply want that whenever my browser navigates to this page,then this image should be copied to my hard disk or to my picturebox1 and it should always overwrite the previously saved image.
Meaning,suppose i navigate to this page once,the image1 get's saved.Now if i again navigate to this page,the new image that comes up now should get saved by overwriting the image1 that was saved previously.
Last edited by DragonBoy; Sep 13th, 2009 at 10:06 AM.
if you are using urldownloadtofile , which seems to be the method used in the thread you link to
as you specify the filepath for urldownloadtofile it is where ever you want
if you want to use the users temp folder use environ("temp") to get the full path
if you are using someother method to save the files you need to specify what method you are using
if you just want to save the source code of the webpage you can just write out the document element outerhtml to a text file
try like
vb Code:
open environ("temp") & "\tempfile.htm") for output as 1
print #1, wb.document.documentelement.outerhtml
close 1
either of these will overwrite existing without prompt
Last edited by westconn1; Sep 8th, 2009 at 04:55 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
I tried a method or two myself...but the problem is that they save the web page as a single file !!!
What i want is that images be saved separately so that i can use those images when i want to...
Is there anyway to same .bmp image from a web page ?
The url of the image changes every time & i cannot download it twice so i have to find a way to copy it straight forwardly from my web control...
I found this coding but it doesn't save any images for sites other then the three mentioned in it:
Code:
Option Explicit
Dim x As Integer
Dim b() As Byte
Dim sUrl As String
Dim sPicUrl As String
Dim sPicName As String
Dim iStart As Integer
Dim ctl As Control
Private Sub Form_Load()
ColorForm
List1.AddItem "www.Yahoo.com"
List1.AddItem "www.google.com"
List1.AddItem "www.visualbasicforum.com"
End Sub
Private Sub List1_Click()
'Set clicked url to a string
sUrl = List1.Text
'Navigate to that url string
WebBrowser1.Navigate sUrl
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
List2.AddItem "=========="
List2.AddItem sUrl
On Error Resume Next
MkDir App.Path & "\" & sUrl
'Loop through all images in html
For x = 0 To WebBrowser1.Document.Images.length - 1
'Set image url to a string
sPicUrl = WebBrowser1.Document.Images.Item(x).src
'Sometimes the webbrowser control returns a default
'error page on statup. If this is the case, do not
'download images in this page
If Mid$(sPicUrl, 1, 3) = "res" Then GoTo FirstLoad
'Get the picture name out of the url string
sPicName = Mid$(sPicUrl, InStrRev(sPicUrl, "/") + 1, Len(sPicUrl))
'Download the picture
DLpic
'Add picture url to listbox
List2.AddItem sPicUrl
Next x
List2.AddItem "----------"
FirstLoad:
End Sub
Private Sub DLpic()
'Open image url
b() = Inet1.OpenURL(sPicUrl, icByteArray)
'Open for a new file
Open App.Path & "\" & sUrl & "\" & sPicName For Binary As #1
Put #1, , b()
Close #1
Do
DoEvents
'Loop while still downloading
Loop While Inet1.StillExecuting
DoEvents
End Sub
Private Sub ColorForm()
'Loop through all controls in form and
'set the forcolor and backcolor for all listboxs
For Each ctl In Me
If TypeOf ctl Is ListBox Then
With ctl
.BackColor = RGB(95, 95, 143)
.ForeColor = vbWhite
End With
End If
Next
'Loop through all controls in form and
'set the forcolor and backcolor for all labels
For Each ctl In Me
If TypeOf ctl Is Label Then
With ctl
.BackColor = RGB(48, 111, 160)
.ForeColor = vbWhite
End With
End If
Next
'Set the forms backcolor
Me.BackColor = RGB(48, 111, 160)
End Sub
Last edited by DragonBoy; Sep 9th, 2009 at 12:57 AM.
i also don't know how many images you can download at the same time, but if you do not clear the cache or pass the constant for the latest version it should get the image from the ie cache, which would be the same as displayed in the webbrowser control
Last edited by westconn1; Sep 9th, 2009 at 04:53 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
westconn1:So,i should just put this code in a loop ?
I need to download only 1 image from that web page but that page originally contains 4 to 6 images i think.
If i put this coding in a loop,will it same all the images to my selected drive ?
Even if it does copies all the images,it's fine with me.Aslong as that 1 image i need is downloaded aswell...
westconn1:So,i should just put this code in a loop ?
that code is a loop
if you know the link to the image you do not need loop, just download the image you want
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Can you give me the coding declaring this function aswell ?
That would be great then
I've tried this :
Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
But it gives me an error on the same line saying:
Code:
Compile error:
Argument not optional
Last edited by DragonBoy; Sep 9th, 2009 at 07:54 AM.
you need to supply the last 2 arguments for the function, see the thread you linked to in the second post, for examples
URLDownloadToFile 0, sURL, sLocalFile, 0, 0
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
you need to supply the last 2 arguments for the function, see the thread you linked to in the second post, for examples
URLDownloadToFile 0, sURL, sLocalFile, 0, 0
Where should i exactly place this ?
The problem with this image is that it's url is not fixed and it can't be requested for twice from the server cause it's generated automatically every time,so the server doesn't hold a copy of this image...
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
i would assume that the image loaded in the webbrowser can be copied then saved to file, but i do not know how to do that, though the execwb method may be able to be used to copy an image on the webpage to clipboard, all examples i have seen try to copy the entire page
i believe that urldownloadtofile will get the image form cache if it exists there, but testing is a bit difficult, generally people always want to get the latest version and bypass the cache
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
EDIT:
Westconn1, I agree with your approach about Copying the image to the clipboard. I believe to Copy the image would be to have it as the selected image, then call ExecWB with OLECMDID_COPY. There's a selection property available, but before that:
DragonBoy, do you have more info on this "image"? Did you look at the page source? Any meaningful tags inside the IMG tag like Alt or Width and height? We need a viable method of locating the Image on the page. It would pointless to loop through the image elements when we dont know what to look for. Can you post the relevant source of the page? Can common sense overcome this, like it's a tiny tiny image? LOL I just need more info.
Originally Posted by westconn1
i believe that urldownloadtofile will get the image form cache if it exists there, but testing is a bit difficult, generally people always want to get the latest version and bypass the cache
I kinda doubt it. There a function for cache known as:
vb Code:
Private Declare Sub URLDownloadToCacheFile Lib "URLMON.dll" (ByVal lpunknown As Long, _
ByVal lpcstr As String, ByVal lptstr As String, ByVal dword As Long, _
ByVal dword As Long, ByRef TLPBINDSTATUSCALLBACK As LPBINDSTATUSCALLBACK)
Last edited by Xiphias3; Sep 10th, 2009 at 08:12 AM.
As for using URLDownloadToFile, sometimes, even if you specify it to get the latest copy, it will read from the cache, so what you would have to do is DeleteURLCacheEntry API...
westconn1:Thanks..i did try your coding on other site and it does saves the images but can't work on the web page i want it to cause it does it by downloading the image again from the server which i can't in this case
Have a look at the source code i'm pasting and tell me what i can do about it
vb5prgrmr:That's really helpfull..I didn't try it on the web page i'm trying to download image from but on yahoo...It's picture box shows me all the images one by one but how can i make it stop on the image i want to save ??
Xiphias3:Same thing..i can't download the image again from the server.
A simple right click on that image-->Save picture as...does save the image but i want it to be automatic so that the user can work while it runs on its own
And ofcourse..here is the source codeIt's a validation image in an online game.It's generaly easy but i need to save the image that comes up every time i go to that page)
Since the source is long..I'll use two posts to put it up..hope you can help me out.I've tried so many things but nothing seems to work here
Last edited by DragonBoy; Sep 13th, 2009 at 10:07 AM.
Reason: this much soruce code wasn't needed.
Here is the remaining half of the source code of that page :
(Really hope you can help me cause i've tried everything i know to download it but it's been useless..I want to make this automatic so that the user doesn't has to do anything with it and he can do whatever he want's to do while this browser saves the image automatically)
Last edited by DragonBoy; Sep 13th, 2009 at 10:07 AM.
As for using URLDownloadToFile, sometimes, even if you specify it to get the latest copy, it will read from the cache, so what you would have to do is DeleteURLCacheEntry API...
Xiphias3:Yah,i was thinking i have posted a bit long source code xD.
But anything that can help me out here would be a life saver cause i'm really tired of trying this myself even though you guys have helped me alot here at VBforums
Set oCR = oHTMLDoc.body.createControlRange()
Call Clipboard.Clear
Call oCR.Add(oImgEle)
If (oCR.execCommand("copy")) Then
that was what i couldn't figure out at all, very nice
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Okay, see post #3 of that same thread I pointed you to. I believe you said you know the url of the image you are wanting so you could use .href or perhaps .url in the do loop to test to see if the image is the one you are after.
Okay, see post #3 of that same thread I pointed you to. I believe you said you know the url of the image you are wanting so you could use .href or perhaps .url in the do loop to test to see if the image is the one you are after.
Good Luck
Originally Posted by DragonBoy
Xiphias3:Same thing..i can't download the image again from the server.
Well, it would not work as the image isnt stored on the server or something, which is why we resorted to copying the image to the Clipboard using the code you provided in the link. Still waiting on the results of Post #20 in this thread.
@vb5prgrmr
That's the problem,I don't know it's url since it's not stored on the server and hence doesn't has a fixed url.
@Xiphias3
I am already using a set of coding in which i have to integrate this coding,let me just post it here to give you an idea :
I tried to integrate your coding but it gave me an error :
Code:
Compile error:
User defined type not defined
On each of these types:
Code:
Dim oEle As IHTMLElement
Dim oImgEle As IHTMLImgElement
Dim oHTMLDoc As HTMLDocument
Dim oCR As IHTMLControlRange
Dim oNavNWait AscX3WebBrowserNavAndWait
Last edited by DragonBoy; Sep 13th, 2009 at 10:08 AM.
Does it have a tag of any sorts besides image (img) perhaps a name? If so you could use that to isolate it in the for each loop. Another way if it is in the same position all of the time then you might be able to use an integer to count the iterations to isolate the image.
vb5prgrmr:here is the whole image source code from the page source code for you :
And yes,it's position is always same on every occasion
How can i use that property of this image ??
I however did try and cut/copy that portion from the webpage but the coding only gets longer and longer that way and it still requires me to use my mouse then...
Even if i can't save this image..can i atleast load it on a picturebox in my form ???
If yes then how ??
Last edited by DragonBoy; Sep 13th, 2009 at 10:09 AM.
Dim oEle As IHTMLElement
Dim oImgEle As IHTMLImgElement
Dim oHTMLDoc As HTMLDocument
Dim oCR As IHTMLControlRange
Dim oNavNWait As cX3WebBrowserNavAndWait
OK, let's backup. Are you telling me you havent referenced to the HTML Obj API? How do you survive without intellisense from dot-notation? In any case, you should have some version of the HTML API on your system. Do the following:
1) Goto Project (in menu bar) -> References -> "Microsoft HTML Object library" (Syste32\MSHTML.TLB).
Type-Libraries are used to make the programmer's life easier and are not required at run-time. They're used to allow VB to access external elements such as Interfaces(IStream from Edanmo's OLE TLB) and DataTypes (IHTMLDocument from the MS HTML TLB).
2) Add the cX3WebBrowserNavAndWait to your project. it is attached.
Last edited by Xiphias3; Sep 11th, 2009 at 04:48 PM.
Reason: Uploaded wrong file
@vb5prgrmr
I'm gonna try it once again then i think..Thanks D:
@Xiphias3
LoL,yah,i'm a beginner in VB and all this is kinda new to me xD
Anyways..
Done the first step.
Copied the class module to my Project and copied some of it's coding into the coding of my Form1(the form with the browser)
And the format of the image is Bitmap...
I think you are just about there to solve my problem.Just help me out on this one
Last edited by DragonBoy; Sep 13th, 2009 at 10:09 AM.
hmmm..... I have little time now, so..... post of a pic of the Local variables.
When the error pops up, Break the code via Ctrl + Break on ur keyboard
Go to view and click Locals
Up a pic of the locals variables, as I cant really tell what's going on in your code.
Should i send a attachment of my coding to you so that you can simply see what's going wrong ?
I'm sure something only small is bothering me right now and you can easily fix it in a sec ?
I made a function for that. However, things go wrong.
Public Function imgToFile(img As HTMLImg, affiliateprogramid As Long) As String
Dim ocr As IHTMLControlRange
Dim theFile As String
theFile = trim(CStr((GetTickCount Mod 100000))) + ".jpg"
Set ocr = img.Document.body.createControlRange
Clipboard.Clear
ocr.Add img
ocr.execCommand "copy" '<- It doesn't get copied
Call SavePicture(Clipboard.GetData(ClipBoardConstants.vbCFBitmap), theFile)
imgToFile = theFile
End Function
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
did you try brackets around "copy" as per the original code posted?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
I think changing to execCommandShowHelp is the one that does the trick somehow. I figured this out through sheer luck. I don't even know what execCommandShowHelp does help?
execcommandshowhelp works most of the time. But after a while it simply fail to copy for no know reason. Only if I actually logged in it works. So if I switch to another user and let my program run it doesn't. Well, sometimes it does, sometimes it doesn't.
Using execcommand without help will make ie pops out a message saying whether I will allow website to copy clipboard.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete