ImageButton image not displaying - c#

I am trying to programmatically set image of ImageButton.
My code is below:
String path="Images/1/";
path= path + string.Format("{0}", _ds.Tables[_bplp.SqlEntityX].DefaultView[0]["Photograph"]);
ImageButton1.ImageUrl = path;
I am saving images in my site folder Images/1 , and storing image name in database.
Problem lies when I am trying to display image in ImageButton.
Although after debugging it, path variable is taking correct path of image
as "Images/1/imagename.jpg", but still ImageButton doesnot show image.
Also, Images folder lies at root level of website.

try this
replace this
String path="Images/1/";
with
String path="~/Images/1/";

Also, you can try ResolveUrl();
ImageButton1.ImageUrl = ResolveUrl("~/" + path);

Problem was in my webconfig file, I had set incorrect site folder url in that, above code is working fine now
Thanks to all

Related

Show Images that has spaces in Image ASP.NET control

I am setting an absolute path to Image control. This path is having spaces. After assigning the path to ImageURL property of the Image ASP.NET control, it does not show the image. I don't have option to remove the spaces as it is the requirement. Also, this path is outside the root directory(There is basically a FileUpload control that takes the file and then I am assigning the path to Image control).
Firstly is it possible to do. If yes how? Below are the code blocks relevant to the question
Server Code
target.ImageUrl = strImagePath;
where target is the Image control id
File Path: C:\Users\WebMaster\Downloads\2 States Full Vedio Songs 720p Bluray Encodded By(Khanz)\Screenshoot\vlcsnap-2014-05-17-13h22m13s103.png
Rendered HTML
<img id="target" alt="[Jcrop Example]" src="C:\Users\WebMaster\Downloads\2%20States%20Full%20Vedio%20Songs%20720p%20Bluray%20Encodded%20By(Khanz)\Screenshoot\vlcsnap-2014-05-17-13h22m13s103.png" />
Thanks in advance for your help.
Thanks Manson. I was so wrong. The image has to be hosted on the web server to access. Actually, this is what I want to cut down. Uploading the image from local user path to server folder was taking time. But I forgot the basics. Re-writing your comment as answer.

Image is not showing in image control asp.net

Here is my image control in UI:
<asp:Image ID="Image1" ImageUrl='<%# Bind("strCLogo") %>' runat="server" width="200" Height="200"/>
And here is the C# code, I am grabbing the link of the image from database (customerstbl) but unable to bind it with image control, I can see the image link in the source but its not showing :
protected void Page_Load(object sender, EventArgs e)
{
int nCID = Convert.ToInt32( Session["nCID"].ToString());
DataClasses1DataContext _dc = new DataClasses1DataContext();
CustomersTbl _customer = new CustomersTbl();
_customer = _dc.CustomersTbls.Where(a => a.nCID == nCID).FirstOrDefault();
Image1.ImageUrl = _customer.strCLogo.ToString();
}
here is how I am saving the link of the image in database, It seems I have an issue when I save the image link in database (it saves the whole path not the local directory path and image name)
string s = Server.MapPath("~/Imageslogo/") + Path.GetFileName(LogoUpload.PostedFile.FileName);
LogoUpload.SaveAs(s);
Save the image path as a relative path, configure root path globally
You are storing the path to the image as an absolute path. This is bad for several reasons, the main ones being:
What happens if you deploy your app to a different server with a different file structure? your absolute path will be wrong
What happens if you decide to move all of your images (but keep the structure of the image folder)? Again, your absolute path will be wrong.
I would advise you to store the relative image path in the database. In this case /Imageslogo/100001203240.jpeg. Then store the image root path (the part before the relative path) in your Web.config file. E.g. in the appSettings:
<appSettings>
<add key="myApp.Imageroot" value="F:\projects\accounting\Accounting2014\Accounting2014" />
</appSettings>
You can get this appSetting value using the following code:
string myRoot = System.Configuration.ConfigurationManager.AppSettings.Get("myApp.Imageroot");
With these two parts of the full path, you can:
Use the relative path in your HTML output which will work fine on your web server
Construct the full path by combining the root from your appSettings with the relative path in the database. You might use this is you need to manipulate the image programmatically or otherwise access the actual file instead of just passing the path to the client browser.
Easily change the root by changing the appSetting
note
As an aside, if you are setting the ImageUrl property in code behind, then the ImageUrl attribute in your .aspx becomes pointless. So remove it:
<asp:Image ID="Image1" runat="server" width="200" Height="200"/>
F:\projects\accounting\Accounting2014\Accounting2014\Imageslogo\10000120324‌​0.jpeg is not the correct value for Image.ImageUrl Property, the correct value is the relative path like ~/Imageslogo/10000120324‌​0.jpeg.
You need to save the relative path to the database, use the code below to get the relative path
string imageLocation = string.Format("~/Imageslogo/{0}", Path.GetFileName(LogoUpload.PostedFile.FileName));
and save imageLocation to strCLogo column of CustomersTbl table.

Firefox: Image does not load on aspx code

I just use simple code to load image in asp.net image control, it works fine in IE but firefox does't show image. the line of code is:
string path = "F:\\Image\\";
string img = "header-firefox.PNG";
Image1.ImageUrl = path + img;
and the error is:
The address wasn't understood
Firefox doesn't know how to open this address, because the protocol (f) isn't associated with any program.
even file:///F:/Image/header-firefox.PNG path is not working in code even.
Do you mean that is the path you see in the HTML? I understand that the backslashes are escaped in C# but surely in the HTML the path should be "\\192.172.60.05\Users\4133.Png"?
Have you tried navigating to \192.172.60.05\Users\4133.Png directly in Firefox?

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