Allen4Tech
Allen4Tech

Reputation: 2184

image in local html couldn't be loaded into webview in windows8

I want to load local html file which in the local folder to the webview, but WebView doesn't support 'ms-aspx:///' protocal, I found a solution to read the html file to stream, and then convert it to string, using NavigateToString method to load the html, it works well. But, If there's an image in the html file, the image couldn't display, anyone can help?

Upvotes: 2

Views: 1474

Answers (2)

Allen4Tech
Allen4Tech

Reputation: 2184

I have solved.

Solution:

Convert the image file to base64 string

StorageFolder appFolder = ApplicationData.Current.LocalFolder;
        StorageFile file = await appFolder.GetFileAsync("SplashScreen.png");
        using (var stream = await file.OpenAsync(FileAccessMode.Read))
        {
            var reader = new DataReader(stream.GetInputStreamAt(0));
            var bytes = new byte[stream.Size];
            await reader.LoadAsync((uint)stream.Size);
            reader.ReadBytes(bytes);
            base64 = Convert.ToBase64String(bytes);
        }

Use StringBuilder to create the html string

sb.Append("<html><head><title>Image test</title></head><body><p>This is a test app!</p><img src=\"data:image/png;base64,");
        sb.Append(base64);
        sb.Append("\" /></body></html>");
        TestWebView.NavigateToString(sb.ToString());     

Upvotes: 5

Jennifer Marsman - MSFT
Jennifer Marsman - MSFT

Reputation: 5225

Try using the ms-appx-web:// scheme instead of ms-aspx:// to load html from a WebView. If that doesn't work, you may need to use the ms-appdata:// scheme to access the image if it's in your application data folder.

Some further resources that might help:
How to load a local HTML-File into Webview
URI schemes
How to reference content

Upvotes: 0

Related Questions