[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net WebBrowser read from Stream

Posted on 2010-01-08
9
Medium Priority
?
6,747 Views
Last Modified: 2013-12-08
Good evening,
Ive got a problem where I need to show a PDF file, but I dont want to put the file on the hard drive then open it.

Ive looked into the posibility of using the Adobe Reader plugin, but cant find out how to get it to read from a stream like Ive got working for my JPG images. My first attempt was to convert all the PDF files to JPG images, and import them, then put the images into my viewer as streams, but I cannot find any free PDF to JPG converters that I can code in VB.net.

So my current idea is to use the webbrowser component, and change the header values and source to a PDF, only problem being, I have NO idea if I can get the webbrowser control to read from a file stream.

Ive attached my code so far, but when I get to the webbrowser.loadFile = stm, VB falls over :-(

The code for attaching works, as I can direct the stream to save the file to the hard drive, and can open the file fine.

Any ideas would be extreamly greatfully recived!

Thank you
Dim dbPath As String = Application.StartupPath & "\bob.db"
Dim con As New SQLiteConnection("Data Source=" & dbPath & ";UTF8Encoding=True;Version=3")
Dim da As New SQLiteDataAdapter("Select * From tbl ", con)
Dim MyCB As New SQLiteCommandBuilder(da)
Dim ds As New DataSet("MyImages")
da.Fill(ds, "MyImages")
con.Close()
Dim myRow As DataRow
myRow = ds.Tables("MyImages").Rows(ds.Tables("MyImages").Rows.Count - 1)
Dim stuff() As Byte = myRow("t")
Dim stm As New MemoryStream(stuff)
webbrowser1.loadfile = stm '<-FAILS HERE

Open in new window

0
Comment
Question by:tonelm54
  • 3
  • 3
  • 3
9 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 26211991
Just use a WebBrowser control.  You can pass the path of both to the Navigate() method:

        Dim browser As New WebBrowser()
        browser.Navigate("path")

0
 

Author Comment

by:tonelm54
ID: 26212812
But using the navigate method means I have to save the file first of all to the hard drive, which I dont want to do, i would like it to just read from the stream if possible.

Thank you
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 26213081
I don't know.  I'm sorry.

   Are they huge .pdf's?  If not, Why not just create a temporary file, show it, then delete it when it's closed?



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 26213158
Try looking at the WebBrowser.DocumentStream property.

WebBrowser.DocumentStream Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentstream.aspx

"Use this property to load a Web page into the WebBrowser control from a Stream object. You can use this property, for example, to load Web pages from a database or resource file. When you set this property, the WebBrowser control automatically navigates to the about:blank URL before loading the specified text. This means that the Navigating, Navigated, and DocumentCompleted events occur when you set this property, and the value of the Url property is no longer meaningful."
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 26213195
Hey Bob!

That looks like a better idea.  :)
0
 

Author Comment

by:tonelm54
ID: 26217414
Hi, using the "WebBrowser1.DocumentStream" method, however when using the DocumentStream method Im unable to add additional headers.

So if I can set the headers, my problem is completly solved :-)

Any ideas?

        Dim dbPath As String = Application.StartupPath & "\bob.db"
        Dim con As New SQLiteConnection("Data Source=" & dbPath & ";UTF8Encoding=True;Version=3")
        Dim da As New SQLiteDataAdapter("Select * From tbl ", con)
        Dim MyCB As New SQLiteCommandBuilder(da)

        Dim ds As New DataSet("MyImages")
        da.Fill(ds, "MyImages")
        con.Close()

        Dim myRow As DataRow

        myRow = ds.Tables("MyImages").Rows(ds.Tables("MyImages").Rows.Count - 1)
        Dim stuff() As Byte = myRow("t")
        Dim stm As New MemoryStream(stuff)

        'WebBrowser1.Document.Write("Content-type: application/pdf") <- Doesnt work :-(

        WebBrowser1.DocumentStream = stm
        Me.Text = myRow("filename")

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26232597
There are a few options, that I have been researching, but I haven't tried.

How do I get a C# WebBrowser control to show jpeg files (raw)?
http://stackoverflow.com/questions/290035/how-do-i-get-a-c-webbrowser-control-to-show-jpeg-files-raw

Asynchronous Pluggable Protocols
http://msdn.microsoft.com/en-us/library/aa767743(VS.85).aspx

A Simple protocol to view aspx pages without IIS implemented in C#
http://www.codeproject.com/KB/aspnet/AspxProtocol.aspx
0
 

Author Comment

by:tonelm54
ID: 26295711
According to several forums Ive read you cannot specify the document type when using the document.stream property.

Is it possible to kinda emulate a web-server so the stream starts off with the header values, and then continues with the document stream?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 26299808
When I figure that out, I will be a happy, happy man, since I have been researching this problem for a while (off and on).  I haven't had any time to see if I could make something of the CodeProject example, using the pluggable asynchronous protocol.  For this particular problem, now, I just write the .pdf stream to a temporary file, and then use the file association to open the file with the Acrobat Reader, but that is not the best solution.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
When you put your credit card number into a website for an online transaction, surely you know to look for signs of a secure website such as the padlock icon in the web browser or the green address bar.  This is one way to protect yourself from oth…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

872 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