我怎么可以显示直接从内存的WebBrowser控件的形象呢? [英] How can I show an image in webBrowser control directly from memory?

查看:128
本文介绍了我怎么可以显示直接从内存的WebBrowser控件的形象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能直接从内存而不是硬盘显示WebBrowser控件中的图像?
当我使用RAM磁盘软件创建一个虚拟驱动器,可以解决图像源加载它就像这样:
IMG SRC =Z:/image.jpg即Z为RAM磁盘驱动器。是否有可能做,在.NET programmaticly?或者使用的MemoryStream这样做呢?

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?

我真的AP preciate这个建议。

I would really appreciate some suggestions about this.

推荐答案

您可以连接code以base64形象。例如:

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);
            }
        }
    }
}

和此Javascript code:

And this Javascript code:

function setImageData(imageBase64) {
    var myImg = document.getElementById("myImg");
    myImg.src = "data:image/png;base64," + imageBase64;
}

这篇关于我怎么可以显示直接从内存的WebBrowser控件的形象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆