简体   繁体   中英

C# accessing embedded image from within an embedded HTML resource

I want to have two embedded resources in a VS 2013 C# WinForms application.

One is a JPG file: embedding it is very straightforward.

The other is an HTML file (also easy to embed), but it is the contents of the HTML that are giving me fits. The HTML body needs to reference the embedded JPG file as a resource:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8" />
  <title></title>
</head>
<body>
  <image src=".... **reference that embedded JPG here**" />
</body>
</html>

The goal is to be able to display that HTML file, which consists solely of the image contents, in a WebBrowser control, without needing to store either file independently of the application resources.

I've seen some other answers here referencing WebForms, but none helpful for this task.

Any help is greatly appreciated.

Jeff W.

Reading, PA

You can base-64 encode your image and put it into HTML or CSS. It's not supported on old browsers (pre IE8 I think?) but otherwise you should be good to go. Here's how to do that: https://css-tricks.com/data-uris/

Otherwise, you'll just want to put a placeholder in your stored HTML and swap it out with the base-64 encoded image.

HTML

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8" />
  <title></title>
</head>
<body>
  <image src="data:image/gif;base64,{0}" /> <!-- Replace here -->
</body>
</html>

C#

var html = GetStringResource(); // However you do it.
var imageData = GetImageResource(); // Ditto
var encoded = GetBase64FromBinary(imageData); // TODO
var finalHtml = string.Format(html, encoded);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM