如何让 C# WebBrowser 控件显示 jpeg 文件(原始)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/290035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:50:46  来源:igfitidea点击:

How do I get a C# WebBrowser control to show jpeg files (raw)?

c#.netwebbrowser-control

提问by Wolf5

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?

有谁知道在 .Net 2.0 - .Net 3.5 中如何将 jpeg 作为字节数组加载到 System.Windows.Forms.WebControl 中并设置正确的 mimetypes 以便显示?

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.

webBrowser1.DocumentType 似乎是只读的,所以我不知道该怎么做。一般来说,我希望能够加载任何类型的文件源,并在浏览器中定义 mimetype 以显示它。

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.

写入临时文件的解决方案不是很好的解决方案。目前我已经解决了这个问题,它有一个小的本地网络服务器套接字侦听器,可以提供我要求的带有正确 mimetype 的 jpeg。

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

C# 中的示例解决方案在这里完美运行:http: //www.codeproject.com/KB/aspnet/AspxProtocol.aspx

采纳答案by liggett78

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.

您必须实现一个异步可插拔协议,例如 IClassFactory、IInternetProtocol...然后您使用 CoInternetGetSession 来注册您的协议。当 IE 调用您的实现时,您可以从内存中提供图像数据/提供 mime 类型。

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

这有点乏味,但可行。查看MSDN上的 IInternetProtocol 和可插拔协议文档。

回答by Ian Boyd

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

你做不到。您不能将图像填充到 Microsoft 的 Web 浏览器控件中。

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

限制来自 IWebBrowser 控件本身,.NET 封装了它。

回答by Austin Salonen

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.

如果您想要彻底破解,请尝试将您的流设为仅显示您的图片的 HTML 文件。您丢失了图像字节流,并且必须将图像写入磁盘。

回答by hangy

I do not know whether the WebBrowser.NET control supports this, but RFC2397defines 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.

我不知道WebBrowser.NET 控件是否支持这一点,但RFC2397定义了如何使用内联图像。使用这个和动态创建的 XHTML 片段,您可以分配图像而无需将其写入文件。

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:

参考:

回答by Seeker

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

对于 base64 编码的内联图像,IE 仅支持 32KB,因此不是一个好的解决方案。

回答by Tim Ludwinski

Try the res:protocol.

尝试res:协议。

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

我还没有用 .net dll 尝试过,但这篇文章说它应该可以工作。即使它确实需要 C++ dll,就编码而言,它的使用要简单得多。

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

我创建了一个帖子,向您展示如何在此处向您展示如何创建资源脚本并正确使用res:协议。