WebView webView = new WebView
{
Source = new HtmlWebViewSource
{
Html = @"<!DOCTYPE html><HTML>
<BODY>
<H1>.NET MAUI</H1>
<P>Welcome to WebView.</P>
<img src=""myimg.jpg"" alt=""My Image"" width=""500"" height=""600"">
</BODY>
</HTML>",
},
};
I wanted to write something like this. Having HTML
-string in memory to be displayed.
Images, Audios and potentially Vidoes should also be loaded from memory.
What must I do to add an Handler
to webView
or webView.Source
that is called, when the image should be loaded so that I can return an byte[]
with the data should be interpreted as image, sound or video?
I have search in the web and found some ideas
webView.Source.Load(new MauiWKWebView());
It works in this [example][1] if images are added as static resource. But my media data is stored in a database.
CodePudding user response:
While using WebView
, the only way to render image I'm avare of is to set it's content as base64
string.
Doing so, your example would look something like this:
var htmlString = @"<!DOCTYPE html>
<HTML>
<BODY>
<H1>.NET MAUI</H1>
<P>Welcome to WebView.</P>
<img src=""data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="" alt=""Red dot"">
</BODY>
</HTML>";
var webView = new WebView
{
Source = new HtmlWebViewSource
{
Html = htmlString
}
};
Assuming you have image data in byte[]
form, you can use this to convert it to base64 string
:
var imageBytes = ...;
var base64ImageString = System.Convert.ToBase64String(imageBytes);