Reputation: 2439
I have one Application were we write output to Html file. For writing the text we use
Console.write("Added to Report");
Now I want to add a image so I use something like this.
string imageTag = "<img src=\"Winter.jpeg\" />";
Console.WriteLine(WebUtility.HtmlDecode(imageTag));
But in my html report it shows something like this :
<img src="winter.jpeg" />
So the image is not added to the report and we just see the string.
Is there any way that I can add this in the report
string imagePath = string.Format(@"{0}\Resources\DashboardScreenshot\{1}.png", AppDomain.CurrentDomain.BaseDirectory, imageName);
bmpCrop.Save(imagePath, ImageFormat.Png);
string imageTag = String.Format("<img src=\"{0}\" alt=\"{1}\" />",imagePath, imageName);
Console.Write(imageTag);
Upvotes: 1
Views: 150
Reputation: 12439
You don't need to use HtmlDecode
here. Because your tag has nothing to be decoded.
Use it when you are writing something which is an escape sequence
in C#, or anything which will not be considered as text in C# only then you should use it.
Here is my testing:
string imageName = "img.jpg";
string imagePath = @"C:\xxxx\xxxxxxxx\Downloads\img.jpg";
string imageTag = String.Format("<img src=\"{0}\" alt=\"{1}\" />", imagePath, imageName);
Console.Write(imageTag);
File.WriteAllText("file.html", imageTag);
and its fine, I have the image on the browser.
Upvotes: 1
Reputation: 172270
Apparently, you are HTML-encoding your content somewhere¹ outside the blocks of code you have shown us before it is written to the HTML file.
That won't work. If you want to add literal HTML tags to your HTML file, you must not HtmlEncode them.² Instead, only encode the "text parts", i.e., the parts which do not contain HTML tags:
Console.WriteLine(WebUtility.HtmlEncode("Added to Report. Note that 3 < 5."));
Console.WriteLine("<img src=\"Winter.jpeg\" />");
¹ My guess would be that the same block of code that insert the <pre>
(cf. your comment to Shaharyar's answer) also does the (unintended) HTML encoding. If you are still unclear on how to solve this, show us this part of your code.
² ...and no, Encode(Decode(someText)) != someText
(in general), so the workaround of decoding it first -- as you have tried -- won't work.
Upvotes: 0