I'm showing an HTML file (Documentation.html)'s content inside a webBrowser control. The HTML file is located as resource.
string htmlFile = Properties.Resources.Documentation;
webBrowser1.DocumentText = htmlFile;
There is an image inside Documentation.html which will be shown properly in any browser:
<img src="Resources/Image.png">
Unfortunately all the text of the HTML-file appears normally but not the image.
Can I set the images path to the resources (like Properties.Resources.Image) or something else?
You can embed the image's data in documentation.html file like
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
ref:
Embedding Base64 Images
You can do it either direcly or even by code like
htmlFile .Replace("Resources/Image.png", string.concat("data:image/png;base64," + imageData))
Related
I am generating a report in PDF, I use an HTML template that I convert to PDF, I have problems when inserting an image that should go in the report, this image is passed as a parameter in the partial class of my .tt file of the form: System.Drawing.Image MyImgParam but failed to embed it in the HTML tag
I tried to convert it to base64 as I read in the documentation but it does not show me the image
`C#
<img src="<# =MakeImageSrcData(ImgParamBase64;) #>" />
`
I would like the image I pass as a parameter to be embedded in the src property of my tag
Change the way to pass the image, stop passing the Image object as a parameter and pass a string with the image converted to Base64 and then embed the code in this way:
<# string b64 = Base64String;
#>
<img src="data:image/png;base64,[<#= b64 #>]"/>
I've got a raw image : either a Bitmap object, or a byte[]. How may I include it in my website (in an .cshtml file), without saving it on my computer ?
I'm using c#, and the project follow the MVC structure.
You can put the image in your html as a base64 string. However, You do need to know the mime-type of the image as well because that has to be prefixed to the string.
For example:
<img src="data:#mime;base64,#(Convert.ToBase64String(bytes))" />
As a rendered example, this will show a dot:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
I want to render a html-page with the C#-Webbrowser Form.
Normally I receive the html file from another application. For simplicity I just read the html page from the hard drive into a stream and then I set the webBrowserControl to this content.
That works in general. But now I want the html file to reference to images in the imageList.
I don't want to save the images to the hard drive.
Is there any possibilty to reference images in RAM with HTML.
The common way like
<img src="C:\\pic.png"/>
is obviously not possible.
Explanation Code
Image image = Image.FromFile(src_pathfile); //normally from another application over interface
List<Image> imageList = new List<Image>();
imageList.Add(image);
Stream source = File.OpenRead("C:\\Webpage.html"); //from another application
webBrowser.DocumentStream = source;
Thank you for your help in advance.
magicbasti
You can encode the image as base-64 and store it in the <img> tag itself. There's some information about it here.
I have image control.I want to load image from my specific path.
i have a code in page behind
string imagePath ="E:/DotNetProjects/Templates/Default/icons/Computer.png";
imgEditor.ImageUrl = imagePath;
imgEditor.AlternateText = "Unable To Find Image";
path is exist and image is also available but always load alternate text.
imgEditor is my image control ID.
Plz help to catch my mistake.Thanks.
Just put your image in solution(any folder or even in root) and path image uri from that (with src in asp page) like :
src="Templates/Default/icons/Computer.png"
The imagePath is a filesystem path... you need a URL... (something like http://...). The URL must be accessible from the browser i.e. you need to setup your webserver (IIS) to serve the respective path... I would recommend putting the image into the solution/project so that the URL is relative...
I have an webbrowser control on my form. I am able display html files in that control. But my page contains some images if i give absolute path to it then images are displayed. But if i give relative path then images are not shown in the pages.
I have HtmlPages folder located at bin folder.
And i am assigning
FileStream source = new FileStream(#"..\HtmlPages\supportHtml.html", FileMode.Open, FileAccess.Read);
webBrowser.DocumentStream = source;
If i assign D:\myapp\bin\HtmlPages\file.png then there is no problem.
My images are stored in same folder. If i open html files with webbrowser then images are displayed.
What is the correct path to set ??
Relative paths are relative to WebBrowser.Url. Which, when you load the HTML directly, either through DocumentStream or DocumentText is about:blank. That's not going to help WB find the file, you must use an absolute path. Tinkering with the Url property is not an option.
Consider using Html Agility Pack to tinker with the file content before assigning it to the DocumentText property. Use Path.GetFullPath() to translate relative paths.