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\100001203240.jpeg is not the correct value for Image.ImageUrl Property, the correct value is the relative path like ~/Imageslogo/100001203240.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.
Related
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.
I am upoloading image to below location:
Location:\MyPC-pc\u165121142\MyFolder\pid\11\Profile\FImage\
But I cannot show the image from above location.
below is my control:
<asp:ImageButton ID="ImgImage" runat="server" />
I am checking using below method for file exists or not(it works) but when I set image file location to ImageUrl property, I can not see image in browser.
if(System.IO.File.Exists("\\MyPC-pc\u165121142\MyFolder\pid\11\Profile\FImage\Koala6192013104451AM.jpg"))
{
ImgImage.ImageUrl="\\MyPC-pc\u165121142\MyFolder\pid\11\Profile\FImage\Koala6192013104451AM.jpg";
}
note: This folder (u165121142) is in network. I have given it full rights
The user's browser needs to be able to retrieve from the URL. It's retrieved at display time.
You could either put the file on a web server and use an http url, or you might be able to use a file: url. Either way, the browser won't retrieve from a disk file directly.
Is that your actual code? 'Cause you probably need to use double backslash, like:
if(System.IO.File.Exists("\\\\MyPC-pc\\u165121142\\MyFolder\\pid\\11\\Profile\\FImage\\Koala6192013104451AM.jpg"))
{
ImgImage.ImageUrl="\\\\MyPC-pc\\u165121142\\MyFolder\\pid\\11\\Profile\\FImage\\Koala6192013104451AM.jpg";
}
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
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 a folder C:\Images which has a some images. This folder is not inside my project and I would to know if there is a way to load an image from that folder on to an ASP.NET Image control.
<asp:Image ID="img" runat="server" />
One solution could be to make the Images folder a Virtual directory on the IIS but I would like to know if this can be done without creating a virtual directory for the Images folder.
Assuming proper access is granted to the Images folder you could do something like this:
Your main page:
protected void Page_Load(object sender, EventArgs e)
{
mainImage.ImageUrl = "ImageHandler.ashx?image=MyImage.jpg";
}
ImageHandler:
public void ProcessRequest(HttpContext context)
{
byte[] imageBytes = File.ReadAllBytes(#"C:\Images" + context.Request["image"]);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageBytes);
}
At the end of the day, the image needs to be sitting somewhere that the user's browser can see it.
That means your options are:
Move the image to a directory on the webserver
Set the image's directory up as a Virtual Directory
Copy the image at runtime to a directory on the webserver
The Best Practice answer is to put the images into your project if they're part of the site, or to map a Virtual Directory if the directory is a store of user-uploaded images.