Copying pictures from inside an iFrame in webbrowser control - vb.net

I used the code below to successfully get a copy of each picture inside a page loaded using webbrowser control.
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)
Dim imgRange As IHTMLControlRange = DirectCast(DirectCast(doc.body, HTMLBody).createControlRange(), IHTMLControlRange)
For Each img As IHTMLImgElement In doc.images
imgRange.add(DirectCast(img, IHTMLControlElement))
imgRange.execCommand("Copy", False, Nothing)
Using bmp As Bitmap = DirectCast( _
Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
bmp.Save(img.nameProp)
End Using
Next
I got the code from here: Copy an image from cache of web browser control present in VB.NET
However, the picture I am interested in is inside an iFrame.
I tried changing:
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)
to
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.Window.Frames(iFrameID).Document.DomDocument, IHTMLDocument2)
but I am getting an "Access Denied" Error. I guess (not sure) its because the iframe's src is on a different domain.
Is there a way around this problem?
Thanks!

If the iFrame has content from a domain that is different from the the parent then your out of luck. You could attempt a different solution and retrieve the iFrame's page via Ajax and parse out the image src. Another alternative is to do it server side with a program like PhantomJs (or VB, PHP etc) to retrieve the page and parse it for images to retrieve.

Related

MS Access VBA Dynamically Show Pics From URL

I found this code from here:
Show Pictures Directly From URLs in Access Forms and Reports
I have this code on my form:
Public Function PeopleImage()
'FUNCTION TO SET THE IMGPEOPLEPIC WITH THE CURRENT PERSON'S PHOTO, IF THE PHOTO CAN BE FOUND. OTHERWISE A STOCK IMAGE SAYING NO PIC FOUND WILL SHOW
Dim PeoplePic As String
Dim NoPeoplePic As String
Dim ImgDwnld As String
PeoplePic = "https://TheWebAddress.com/People%20Photos/" & Nz(Me.PersonID, 0) & ".jpg"
ImgDwnld = "\\AppServer\AppDir\Storage\Images\" & Nz(Me.PersonID, 0) & ".jpg"
DownloadFile PeoplePic, ImgDwnld, False
NoPeoplePic = "\\AppServer\AppDir\Storage\NoPeoplePic.jpg"
If FileExists(ImgDwnld) Then
Me.ImgPeoplePic.Picture = ImgDwnld
Else
Me.ImgPeoplePic.Picture = NoPeoplePic
End If
Me.ImgPeoplePic.Requery
End Function
When I added the image control ImgPeoplePic I set it to the NoPeoplePic, so that a picture would be set. Now, when I load the form and navigate, it looks like the photo is reloading (it blinks), but it just keeps showing the stock NoPeoplePic.
Anyone know what I'm missing? I feel like I'm so close. I just don't understand what it's not putting in the image that it downloads. I verified that the image downloads to the directory, and that it is there. I debug the path and image and it pulls up with no problem.
Any help is appreciated!

Programmatically download file (PDF) in GeckoFX VB.NET console application

I use GeckoFX to navigate to a certain website which requires me to login. So far the navigating and logging in looks something like this:
Public Flag_Completed As Boolean = False
....
Navigate("http://www.website.com/loginpage/")
While Not Flag_Completed
Application.DoEvents()
End While
Dim User As GeckoInputElement = GeckoWebBrowser1.Document.GetHtmlElementById("edit-name")
Dim Pass As GeckoInputElement = GeckoWebBrowser1.Document.GetHtmlElementById("edit-pass")
User.Value = "myUsername"
Pass.Value = "myPassword"
Dim Form = CType(GeckoWebBrowser1.Document.Forms(1), GeckoFormElement)
Form.submit()
Flag_Completed = false
While Not Flag_Completed
Application.DoEvents()
End While
...
Public Sub Navigate(ByVal URL As String, Optional ByVal LimitTimeinMinutes As Integer = 1)
Flag_Completed = False
GeckoWebBrowser1.Width = 1920
Application.DoEvents()
Try
GeckoWebBrowser1.Navigate(URL)
Catch ex As Exception
End Try
End Sub
Everything seems to work so far but I can't find a way to download the file properly. I tried using WebClient in combination with the DownloadFile() Method like so:
Dim myWebClient As New WebClient()
myWebClient.DownloadFile("http://www.website.com/path/file.pdf", "C:\Path\to\local\file.pdf")
The problem is that the WebClient is not logged in (in contrary to my GeckoFX browser (GeckoWebBrowser1). What happens is that I end up with a file that has a .pdf file extension but opening it in a text editor makes clear that the file is actually the HTML webpage that would show up on the screen when you enter the link without being logged-in. (Makes sense)
Unfortunately I have searched for more than a day now and can't find an answer to my particular problem. There doesn't seem to be a method within the GeckoFX library what could take the DownloadFile()-method's place. I found the following question on here: How to handle downloading in GeckoFX 29 which seems to be similar to my problem. Sadly for me, the solution is aimed at a Windows.Forms application in C# and I can't seem to make use of that in my own VB.NET Console-Application. Would this be the right way to approach this? If yes, any ideas? If not, how exactly?
UPDATE:
For completeness sake I will mention that I solved my particular problem (downloading a PDF behind a login) but I didn't use GeckoFX but instead used a WebClient and HttpRequests to download the file. I highly recommend the following Tutorial that explains just that: http://odetocode.com/Articles/162.aspx

SvgDocument.Draw() Object reference not set to an instance of an Object

I have looked around Stackoverflow and the internet in generel, but haven't found a post that could help me solve my problem.
My problem is that in the following code snippet at line
Dim bm As Bitmap = SvgDoc.Draw()
I get an Object reference not set to an instance of an object.
Protected Function SvgToPng(ByVal svg As String) As Byte()
svg = svg.Replace("url(""#lineArea"")", "url('#lineArea')")
Dim byteArray = Encoding.UTF8.GetBytes(svg)
Dim str As New MemoryStream(byteArray)
Dim svgDoc = SvgDocument.Open(str)
scaleSvgDoc(svgDoc, 7)
Dim bm As Bitmap = svgDoc.Draw()
Dim out As New MemoryStream
bm.Save(out, ImageFormat.Png)
Return out.ToArray
End Function
I have multiple buttons, under different menus that access this method. My problem is that for a single of these menus, I get the problem as described above, but I don't get it for the rest.
I have checked that both the SvgDoc, str and byteArray all are set, and the only difference between the working one, and one that doesn't work, is the SvgString (in this case svg).
Anyone that can help me here?
EDIT: It's the SVG Rendering Engine library that I use.
I don't know much about this library, I assume you are using SVG Rendering Engine? But I noticed that there is a method SvgDocument.OpenAsBitmap. Why not just open as a bitmap and then change to whatever image format you want?

Itextsharp multiple actions work in Chrome, but not in IE

I looked at this Stackoverflow question, iTextSharp multiple actions for pushbuttonfield, but I still cannot get this working correctly. This is a proof of concept before we buy the iTextSharp license.
So here is the background. I am working on a web forms web application written in vb.net. I open a new browser window to display the PDF in an iframe. The PDF contains form fields, which the user can fill out. At the bottom of the form, there will be a submit button to process the form and a save progress button to save their current changes. After the form is processed/saved, it's supposed to redirect to an HTML page that says "Form Submitted Successfully". I have this all working in Google Chrome, but it's not doing the redirect to the HTML page in IE.
We will be getting LOTS of PDFs from our customers. When I create the PDF forms in Acrobat, I add the 2 buttons to the bottom of the PDF. This ensures the correct placement of the buttons, based on where it fits for that PDF. So the buttons will always have the same names, "btnSaveProgress" and "btnProcessForm".
In the codebehind for the source of the iframe, I have this iTextSharp code:
Using myPdfReader As New PdfReader(strFullFilePathName)
Context.Response.Clear()
Context.Response.ContentType = "application/pdf"
Using stamper As PdfStamper = New PdfStamper(myPdfReader, Response.OutputStream)
stamper.ViewerPreferences = PdfWriter.HideToolbar
Dim hostUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
Dim btnArray() As String = {"btnSaveProgress", "btnProcessForm"}
Dim intCounter = 0
For Each btnName As String In btnArray
Dim saveButtonField As PushbuttonField = stamper.AcroFields.GetNewPushbuttonFromField(btnName)
Dim pff As PdfFormField = saveButtonField.Field
Dim myAction As PdfAction = PdfAction.CreateSubmitForm("../../Common/Services/StaticFormSubmissionHandler.ashx?submissionType=" & intCounter & "&formID=" & intFormID & "&resultID=" & intResultID & "&templateName=" & Server.UrlEncode(sShortName), Nothing, PdfAction.SUBMIT_EXCL_F_KEY)
myAction.Next(New PdfAction(hostUrl & "/Main/Pages/FormSub.htm"))
'pff.SetAdditionalActions(PdfName.U, myAction)
pff.Action = myAction
stamper.AcroFields.ReplacePushbuttonField(btnName, pff)
intCounter += 1
Next
End Using
End Using
The save/process submission works, but the redirect only works in Chrome. In IE, it makes the iframe blank. I'm not seeing any errors or anything. Also, in IE, the PDF has the menu bars displayed. I understand if this is strictly related to the Reader in the IE browser, and I have no control over it. But it would be nice if the stamper.ViewerPreferences = PdfWriter.HideToolbar would work.
Thank you so much in advance for any advice/input.

Set lists of images using bitmap class on Windows Phone

i recently start developing windows phone apps ... well i tended to try using an image control to display a picture on it by setting the code below :
Dim myimages As New BitmapImage(New Uri("/Add Radcontrols test;component/Images/screen.PNG", UriKind.Relative))
Image1.Source = myimages
til now everything is perfectly working but .. i wondered if i could add more than one image and navigate between them by hitting a particular button existing in the UI of my windows phone emulator .
You can do it by creating a List of image uri string. And upon a button clicked, create new BitmapImage using next image uri in the List, then set image1's Source to that newly created BitmapImage. That will be less memory-consuming then if you load all images at once and navigate between them. If those images are stored locally (not downloaded from internet) as what you seems to be doing currently, you'll see the effect as if all images loaded at once. No noticeable delay when loading next image, that what I see so far in emulator.
Dim imageUris As New List(Of String)
Dim nextImageIndex As Integer
...
...
'Upon button click
Dim myImage As New BitmapImage(New Uri(imageUris(nextImageIndex), UriKind.Relative))
Image1.Source = myimage
nextImageIndex += 1
'if index > last uri index, reset index
If nextImageIndex = imageUris.Count Then nextImageIndex = 0 End