Displaying image on web page - c#

How to display an Image if I have the Physical Path of that image in webpage in a particular place?
I tried like below but not working.
img = rdr["ImagePath"].ToString();
img = Server.MapPath(img);
Image1.ImageUrl=img;

I believe it would be as below:
ImageInstanceName.ImageUrl = Server.MapPath(#"/Images/image.jpg");
Or if you were wanting to do it in markup then #Bibhu's solution should work.
Here is the link to MSDN library on Server.MapPath.

You can create an Handler which returns the binary output of that image. and in your image tag refer to this handler
eg. <img src="yourImagehandler.ashx"/>

In C# -
Image1.ImageUrl = Server.MapPath("c:\path\to\file.jpg");

<asp:Image id="Image1" runat="server"
AlternateText="Image text"
ImageAlign="left"
ImageUrl="images/image1.jpg"/>

Related

ImageUrl not finding images in project folder

I want to create a website which will store 2 images in the "home" page, which will lead to 2 different pages. In the case, I'm using the Image object from the toolbox. When I'm trying to assign the ImageUrl, it don't appear to find anything at all, regardless of the format. I've tried with multiple photos, different folders, there's the result:
I've also tried to change it programatically in C#, not working, as well:
Image1.ImageUrl = #"Images/left.jpg";
Should I use the usual <img src=""/> for finding images? Thank you very much!
Right click on that image shown and choose "include in project" and then try to give imageUrl
If you are sure that your path value contains the appropriate path from the ImageFiles directory, then you should be able to use the following :
<asp:Image ID="Image1" runat="server" ImageUrl='~/ImageFiles/<%# Eval("path")%>' />
or use the integrated string formatting of the Eval method as shown here :
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("path", "~/ImageFiles/{0}") %>' />
From the code behind you could do something like below including application base URL some thing like below-
Image1.ImageUrl = #"~/Images/left.jpg"
Please refer the below discussion for more information -
Image isn't showing in Image and ImageButton
ASP.NET will automatically replace the ~ with your application's base
URL, because Image1 is a server control

display image from Access database in .aspx

Could you help me - I want each day to display same text with image from Access database where I stored text and image path. Text is going on the page, but for image I got only placeholder. I tried also with Handlers. I search through theses questions and nothing is exact what I want.
Here is code in C# I tried:
//image
int IDImage = 0;
upit = new baza.Upit("SELECT * FROM (Images INNER JOIN JoinImageText ON Images.IDImage = JoinImageText.IDImage) WHERE JoinImageText.IDText = " + IDText.ToString() + " ORDER BY LastTimeDisplayed, RND()");
upit = new baza.Upit("SELECT * FROM (Images INNER JOIN JoinImageText ON Images.IDImage = JoinImageText.IDImage;
if (upit.Reader.Read())
{
TextImage.Src = upit.Reader["image"].ToString();
IDImage = Convert.ToInt32(upit.Reader["IDImage"]);
}
upit.Zatvori();
Thanks in advance.
You have to use Handlers in ASP.Net.
This post from CodeProject can help you in achieving your goal.
When you say you're getting a placeholder you mean when the page renders in the browser? If so, it means the path to the image is incorrect. What image information are storing in the Access DB? Image name only or image with path? Make sure the path to the image is correct.
Make sure your tag has the src attribute:
<img src="/images/someimage.jpg" alt="Image of something" height="64" width="64">
If you include the runat=server attribute, in code behind you could so something like:
<img src="<%= Url.Content("~/database_value_for_image_path") %>" runat=server/>.
The UrlHelper would resolve the relative path for you.
Or since you're using asp.net, consider the asp:Image tag as well - Image Class on MSDN

Html Image control not displaying image

Below is my html image control:
<img alt="" src="C:\Users\hkalidindi\Desktop\Feedback.jpg" style="height: 19px; width: 20px"/>
But after debugging it is not showing me any image:
I am new to dotnet..please help me to solve this issue...I know how to display image in image control but i am bit confused about the html image control...
Use image only from the content of your website. So copy the image in your website folder say Images and access your image as <img alt="" src="..\images\Feedback.jpg" style="height: 19px; width: 20px"/>. Or you could use Asp.NET Image control and set image path in Server code using
myImage.ImageUrl=Server.MapPath("Images\Feedback.jpg");
"C:\Users\hkalidindi\Desktop\"
Is only local, try uploading it to a website such as http://imageshack.us, http://tinypic.com, grab the direct link and put that in the source.
<img src="URL">
Here URL = The url of the image.
Possible values (for src):
An absolute URL - points to another web site (like src="http://www.example.com/image.gif")
A relative URL - points to a file within a web site (like src="image.gif")
Refer: HTML src Attribute
You are trying to call the image in your local folder. Add the image to the 'Images' folder in your solution and link to that.
Make sure the image is being referenced from a folder that is in your websites application/dir. Then you can reference it simply by using a tag like so:
<asp:Image id="imgFeedback" runat="server" ImageUrl="~/_common/img/feedback.jpg" />
Or set the image path on the server side:
this.imgFeedback.ImageUrl = Server.MapPath("\_common\img\feedback.jpg\");

Use of ImageButton without ImageURL to display the image

I need to use the asp control "ImageButton" in order to have access to the properties "OnClick" and "OnClientClick"
The problem is i cannot give any ImageURL since my Image is in Database.
I used this previously :
<telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" DataValue='<%#Eval("Image") %>'
AutoAdjustImageControlSize="false" Width="90px" Height="110px" Enabled="true" AlternateText="pas d'image"/>
But i don't have any Datavalue property in ImageButton control...
How can i manage to do this using ImageButton?
thanks in advance
You can use a Generic Handler file and call that as your ImageUrl like:
<asp:ImageButton ImageUrl='<%# String.Format("Image.ashx?id={0}", eval("ID")) %>' />
Read more on how to do this here:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129
The fact its an ImageButton makes no difference. It's the fact you want to render an image from image data type. I believe generating the image on the fly using a Generic Handler file is the most common way.
You can use handler for creating image from database or from binary format and this handler you can call from your imagebutton -> imageURL and it will show the image on the page.
You may provide a method that will return an image as stream. Your URL may be:
http://www.yourwebsite.com/Application/Images/GetImage?name=myImageName&format=png
Inside your site you'll provide a GetImage page to query the database and write in the output stream the image data (do not forget to set the mime type).

Loading image based on URL

In .NET(C#), I'm loading an image. The src of the image is stored in the database.
I currently retrieve text from my db using this:
TextBox4.Text = reader["descr"].ToString(); // snippet
However, I want to know, how would I display an image?
Image1.Text= reader["img1"].ToString();
and then in my WebForm:
<asp:Image ID="Image1" runat="server" />
Use the ImageUrl property instead.
Try
Image1.ImageUrl = reader["img1"].ToString();
Have you tried Image1.ImageUrl = reader["img1"].ToString();
This one has been answered many times on so:
Basically you need to create an image handler to load the binary stream of the image to the browser. Also if you are going this route, please remember to use the cache header for each image so you are not pulling data from your database on every request.
ASP.NET [Image Handler]
Enjoy!

Categories