6

How can I show an image in webBrowser control directly from memory instead of hard disk? When I use RAM Disk software to create a virtual drive, it is possible to address an image source to load it like this: img src = "Z:/image.jpg" that Z is a RAM Disk drive. Is it possible to do that in .NET programmaticly? or use MemoryStream to do that?

I would really appreciate some suggestions about this.

svick
  • 236,525
  • 50
  • 385
  • 514
  • You could try this http://stackoverflow.com/questions/290035/how-do-i-get-a-c-sharp-webbrowser-control-to-show-jpeg-files-raw and if that doesn't work you could see this http://www.codeproject.com/KB/files/MhtBuilder.aspx and try to convert your image to .mhtml then show it. – user629926 Nov 21 '11 at 23:51
  • 1
    whats the goal, why do you need to load it from RAM? If its for performance you could just use a SSD. – Jeremy Thompson Nov 22 '11 at 01:38
  • Which WebBrowser control are you using? The WinForms one, the WPF one, or the Silverlight one? – Joe White Nov 22 '11 at 01:48
  • I use WinForms WebBrowser control. – Mohammad Afrashteh Nov 22 '11 at 18:13
  • It could be there's a much better way to accomplish your goal. Why do you need to do this? – djdanlib Dec 12 '11 at 18:31
  • Because I read an image from database and to show it, I should save it to disk then load from disk to show in browser (two accessible disk) – Mohammad Afrashteh Mar 11 '12 at 10:06

1 Answers1

2

You can encode the image in base64. For example

<img src="data:image/gif;base64,MyImageDataEncodedInBase64=" alt="My Image data in base 64" />

Here is a full example of how you can accomplish this:

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ImageEncodedInBase64InAWebBrowser
{
    [ComVisible(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string url = Directory.GetCurrentDirectory() + "\\page.html";
            webBrowser1.Url = new Uri(url);
            webBrowser1.ObjectForScripting = this;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string imageInBase64 = ReadImageInBase64();
            webBrowser1.Document.InvokeScript("setImageData", new[] { imageInBase64 });

        }

        private string ReadImageInBase64()
        {
            string imagePath = Directory.GetCurrentDirectory() + "\\opensource.png";
            using (var fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                return Convert.ToBase64String(buffer);
            }
        }
    }
}

And this Javascript code:

function setImageData(imageBase64) {
    var myImg = document.getElementById("myImg");
    myImg.src = "data:image/png;base64," + imageBase64;
}
Lars
  • 6,421
  • 1
  • 23
  • 24
rcarrillopadron
  • 459
  • 2
  • 6
  • 19