4

Does anyone know in .Net 2.0 - .Net 3.5 how to load a jpeg into a System.Windows.Forms.WebControl as a byte-array and with the right mimetypes set so it will show?

Something like:

webBrowser1.DocumentStream = new MemoryStream(File.ReadAllBytes("mypic.jpg"));
webBrowser1.DocumentType = "application/jpeg";

The webBrowser1.DocumentType seems to be read only, so I do not know how to do this. In general I want to be able to load any kind of filesource with a mimetype defined into the browser to show it.

Solutions with writing temp files are not good ones. Currently I have solved it with having a little local webserver socket listener that delivers the jpeg I ask for with the right mimetype.

UPDATE: Since someone deleted a answer-my-own question where I had info that others could use, I will add it as an update instead. (to those who delete that way, please update the questions with the important info).

Sample solution in C# here that works perfectly: http://www.codeproject.com/KB/aspnet/AspxProtocol.aspx

Wolf5
  • 16,600
  • 12
  • 59
  • 58

6 Answers6

5

You have to implement an async pluggable protocol, e.g. IClassFactory, IInternetProtocol... Then you use CoInternetGetSession to register your protocol. When IE calls your implementation, you can serve your image data from memory/provide mime type.

It's a bit tedious, but doable. Look at IInternetProtocol and pluggable protocols documentation on MSDN.

liggett78
  • 11,260
  • 2
  • 29
  • 29
0

Try the res: protocol.

I haven't tried it with a .net dll but this post says it should work. Even if it does require a C++ dll it's much simpler to use as far as coding goes.

I've created a post that show you how here that shows you how to create the resource script and use the res: protocol correctly.

Community
  • 1
  • 1
Tim Ludwinski
  • 2,704
  • 30
  • 34
0

IE only support 32KB for inline images in base64 encoding, so not a good solution.

Seeker
  • 1
0

You cannot do it. You cannot stuff images into Microsoft's web-browser control.

The limitation comes from the IWebBrowser control itself, which .NET wraps up.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0

If you want a total hack, try having your stream be the HTML file that only shows your picture. You lose your image byte stream and will have to write the image to disk.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
0

I do not know whether the WebBrowser .NET control supports this, but RFC2397 defines how to use inline images. Using this and a XHTML snippet created on-the-fly, you could possibly assign the image without the need to write it to a file.

Image someImage = Image.FromFile("mypic.jpg");

// Firstly, get the image as a base64 encoded string
ImageConverter imageConverter = new ImageConverter();
byte[] buffer = (byte[])imageConverter.ConvertTo(someImage, typeof(byte[]));
string base64 = Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks);

// Then, dynamically create some XHTML for this (as this is just a sample, minimalistic XHTML :D)
string html = "<img src=\"data:image/" . someImage.RawFormat.ToString() . ";base64, " . $base64 . "\">";

// And put it into some stream
using (StreamWriter streamWriter = new StreamWriter(new MemoryStream()))
{
    streamWriter.Write(html);
    streamWriter.Flush();
    webBrowser.DocumentStream = streamWriter.BaseStream;
    webBrowser.DocumentType = "text/html";
}

No idea whether this solution is elegant, but I guess it is not. My excuse for not being sure is that it is late at night. :)

References:

hangy
  • 10,765
  • 6
  • 43
  • 63
  • I believe IE used to have no support for data:image stuff. – liggett78 Nov 15 '08 at 20:29
  • I ran a small test yesterday (http://browsershots.org/http://aktuell.de.selfhtml.org/artikel/grafik/inline-images/) which shows that no IE but IE8 does. :( However, I have no clue what kind of rendering engine that WebBrowser control uses. :) – hangy Nov 16 '08 at 00:59
  • The WebBrowser control uses whatever version of IE you have installed. This means of course that if you run your application on a machine with IE8 then the IE9 features you depend on will not be available. – fin Jan 15 '13 at 08:43
  • My question for Mr. hangy. How the hell you manage to add "text/html" to DocumentType since this property has only get method implemented...? – dovla110010101 Nov 11 '14 at 14:57