Problem with image path of html files viewed by webbrowser control - c#

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.

Related

c# - Reference path of image in resource file

I have a resource file (Example.resx) in my solution and it contains an image (pic.png). I also have a razor view (.cshtml) file that I am building dynamically. Here I have to give the reference of the pic.png image path from the resource.
img src = 'path of the image from the resource'
Any way this can be done ?

Find the Directory of folder in root from a aspx page which is inside a folder in asp.net

I have a aspx page to upload image to a folder.The aspx page is inside a folder called admin.
If i keep the aspx page in the root directory i am able to upload file in the Image folder since both are in the root directory.
But when i do the same keeping the aspx page inside a folder,it shows Could not find a part of the path 'C:\Users\...
So how do i provide path such that i am able to upload image in a folder from a page which is also inside a folder.
Here goes my code:
if( fu_subcat.PostedFile.ContentLength>0 )
{
fn =Path.GetFileName(fu_subcat.PostedFile.FileName);
fu_subcat.SaveAs(Server.MapPath("imag/" + fn));
}
EDIT
It is mapping the path as:
Could not find a part of the path 'C:\Users\lenovo\Documents\Visual Studio 2010\WebSites\emarket\Admin\imag
But i have the folder inside emarket as emarket\imag not emarket\Admin\imag
Best to keep the image in a separate content folder, and then reference the image in a relative path, using the tilda e.g ~"content/image"
Using ~ will get you to the virtual root of your application, so Server.MapPath("~/imag/...") should do it.
This happens because when you call that code from
/Admin/page.aspx
Server.MapPath returns a path which is relative to /Admin/ - so the final path would be /Admin/imag/... To set path from the root you should add a / in front of the path.
Must be
fu_subcat.SaveAs(Server.MapPath("/imag/" + fn));
Notice / in front of the string.
Alternatively you can also add a ~ tilde
fu_subcat.SaveAs(Server.MapPath("~/imag/" + fn));
which points to a root of the current ASP.net application. This will be useful if you application is running in a virtual directory and as a part of global application.

HTML Reference to Image in RAM/Memory

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.

FileNotFoundException why trying to open an image

I have an image in one of my project folders:
Lets say its in:
~/App_Themes/Default/images/SomeImage.png
I want to load this image into a System.Drawing.Image, how do I do that?
If I try using the FromFile method of the Image class:
Image img = Image.FromFile("~/App_Themes/Default/images/SomeImage.png", true);
I get a FileNotFoundException.
I have read some suggesting to store the image into the Server but that's not an option. Is there any way to load this into the Image?
You seem to be using a relative path instead of a file path to locate the image. Try this:
var path = #"~/App_Themes/Default/images/SomeImage.png";
using (Image img = Image.FromFile(Server.MapPath(path)))
{
do some stuff
}
I had a similar problem. The problem for me was that I accidentally added Image folder inside of App_Code folder. I did not updated the code accordingly and therefore I was getting exception.
As soon I removed the Image folder out of App_Code folder, the problem was resolved.
Of course I could have updated also the path in the code.

Unable to load image in img control in asp.net

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...

Categories

Resources