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!
Related
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
so started using a new method for dynamicly displaying images inserted into my database. This is the asp.net code
<asp:ImageButton ID="ImageButton1" ImageUrl='<%#Eval ("ImageUrl", "~/img/{0}") %>' Width="200px" PostBackUrl='<%#Eval ("ProductID", "Product.aspx?ProductID={0}") %>' runat="server" />
now, as you can see what it does is getting the "ImageUrl" from my database depending on what the ProductID is, and then shows that as the image url. Example in my folder /img/ i have a picture called "pic.jpg" then this code would make the image url "~/img/Pic.jpg" now how would i add a new image into the table from the backend? i would have to make a flileupload control upload the file to the "img" folder and then insert only the file name as the "ImageUrl" in the database table. How is this done? do you have any examples or can point me in the right direction?
thanks!
all answers appreciated!
ImageButton is actually a html input type image. So you should not use it for just display purposes. You can use Image control or even use html img tag.
Check this link for FileUpload control
http://asp.net-tutorials.com/controls/file-upload-control/
Check this link for Image control
http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_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).
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"/>
How to add System.Drawing.Image object to Asp:Image Control but I dint have imageId in my table.Image field is in another table..i retrieve image from database in Image object.i want to display it in asp:Image control or grid view.how to do it?? pls send reply... C#
The Image class and <asp:image> control are completely different thing.
You need to have a page or http handler to write the Image object as an image file such as .jpg / .png / .gif. And then use the <asp:image> control to access the URL of your page or http handler to get the image file you generated and display it. <asp:image> basically only render a HTML <img> tag with the URL of the image.
You can use this ASP.NET module and the SqlReader plugin to serve images directly from the database.
You can then reference the images like so:
<asp:Image runat="server" ImageUrl="/sqlimages/[imageidhere]" />