c# - Reference path of image in resource file - c#

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 ?

Related

In Jquery Img Tag in not populating image from DB because Src appending View path location in source that i cant remove ?? MVC

I have MVC application view page is under Home folder.
my image control is like,
<img id="user_img" height="100" width="90" style="border:solid" />
i am saving image in folder Img on the same level like App_data , App_Start etc.
i populating this img from response i am getting in Ajax.
$("#user_img").attr("src", response.EmpPic);
value i am getting from database is ~/Img/imageName.jpg
now according to jquery it set its source value but because my view is in Home folder it does not get image and show source i checked in browser developer tool is ,
"http://localhost:53798/Home/~/Img/imageName.jpg"
It will work if it should be like,
http://localhost:53798/Img/WelcomeScan124117716.jpg
I might change the source of image but how to edit above address it is getting due to view folder "/Home/"
Hopes for your suggestion thanks
Use it without the "~". You only need this in a few cases in Razor. It should work without it.
Try generating your path on the Controller side like this:
response.EmpPic=Path.Combine(Directory.GetCurrentDirectory(), #"/Img/", fileName)
This would give you the path of the current directory where the Img folder resides.

Save current page to image

I'm working on a project which requires to save the current page in an image. I found some examples in Javascript to create a blob of page, but what I would like to do is save the page in a file.
My question is, is it possible to save the content of a page in an image file?
Does a plugin exist to do it directly ?
If not, is it possible to save the blob and render the blob in C# to create an image?
To save a page as an image, you can use http://html2canvas.hertzen.com/
Example:
var canvas = document.getElementById("mycanvas");
var img = canvas.toDataURL("image/png");
document.write('<img src="'+img+'"/>');

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

Problem with image path of html files viewed by webbrowser control

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.

Categories

Resources