473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to display image from the database?

I would like to of display an the image at a proper position on a browser. I access the image from the database before doing this I stored the image in a database in a byte form. I also make a functionality of add image in a database.

When i retrieve the image from the database onliy image is displayed on a browser . The form from which I add the image is not displayed.

This is the aspx page coding
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ins_Image_in_DB.aspx.cs" Inherits="ImageDemo" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <h1>Insert Image In Database</h1>
  11.     <div id="dv_visible" runat="server">    
  12.     <form id="form1" runat="server" method="post">
  13.     <div>
  14.     <table border="3" cellspacing="3" width="300px">
  15.     <tr>
  16.     <td>
  17.     <b>Image_Name</b>
  18.     </td>
  19.     <td>
  20.     <asp:TextBox ID="txtImageName" runat="server" ></asp:TextBox>
  21.   </td>
  22.  </tr>
  23.      <tr><td>
  24.      <b>ImageUpload</b>
  25.      </td>
  26.      <td>
  27.     <asp:FileUpload ID="UploadImage" runat="server" />
  28.     </td>
  29.     </tr>
  30.     <tr>
  31.     <td colspan="2" align="right">
  32.     <asp:Button ID="AddImage" runat="server" Text="InsertImage" Font-Bold="true" 
  33.             onclick="AddImage_Click"/>
  34.     </td>
  35.      </tr>
  36.     </table>
  37.     <asp:Label ID="Message" runat="server"></asp:Label>
  38.     </div><h1>Select Image</h1>
  39.     <asp:DropDownList ID="ddlImageDisplay" runat="server" AutoPostBack="True"></asp:DropDownList>
  40.  
  41.  
  42.     </form>
  43.     </div>
  44.  
  45.     <div>
  46.     <asp:Image ID="DisplayImage" runat="server" ImageUrl="Ins_Image_in_DB.aspx" />
  47.    <%--  <img src="Ins_Image_in_DB.aspx" alt="image not dispalyed"/>--%>
  48.     </div>
  49.  
  50.  
  51.  
  52.     </body>
  53. </html>
  54.  
  55.  
  56.  
That is the .cs page coding
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14. using System.Collections.Generic;
  15. using BllDummy;
  16.  
  17. public partial class ImageDemo : System.Web.UI.Page
  18. {
  19.     DummyBLL dummy = new DummyBLL();
  20.     protected void Page_Load(object sender, EventArgs e)
  21.     {
  22.         dv_visible.Visible = true;
  23.  
  24.         if (!Page.IsPostBack)
  25.         {
  26.             dropdownImagelist();
  27.         }
  28.        selectedImagedisplay();
  29.  
  30.     }
  31.     protected void AddImage_Click(object sender, EventArgs e)
  32.     {
  33.          Stream imgStream = UploadImage.PostedFile.InputStream;
  34.          int ImgLength= UploadImage.PostedFile.ContentLength;
  35.          dummy.ImageName = txtImageName.Text;
  36.          dummy.ImageContentType = UploadImage.PostedFile.ContentType;
  37.          byte[] imageType = new byte[ImgLength];
  38.          dummy.ImageType = imageType;
  39.          int n = imgStream.Read(imageType, 0, ImgLength);
  40.          int roweffected= dummy.addImage();
  41.          if (n >= 0)
  42.              Message.Text = "Image Is Saved In a database";
  43.          else
  44.              Message.Text = "error Occur";
  45.  
  46.     }
  47.     private void dropdownImagelist()
  48.    {
  49.         List<DummyBLL> ListImages = dummy.DisplayImage();
  50.         ddlImageDisplay.Items.Add(new ListItem("--SelectImage---", "0"));
  51.         foreach (DummyBLL ImageObject in ListImages)
  52.         {
  53.             ddlImageDisplay.Items.Add(new ListItem(ImageObject.ImageName.ToString(), ImageObject.ImageId.ToString()));
  54.  
  55.  
  56.         }
  57.     }
  58.         private void selectedImagedisplay()
  59.         {
  60.             List<DummyBLL> ListImages = dummy.DisplayImage();
  61.             foreach (DummyBLL ImageObject in ListImages)
  62.             {
  63.                 if (ImageObject.ImageId.ToString() == ddlImageDisplay.SelectedValue)
  64.                 {
  65.                     dummy.ImageId = ImageObject.ImageId;
  66.                     dummy = dummy.displayImage();
  67.                     Response.ContentType = dummy.ImageContentType;
  68.                     Response.BinaryWrite(dummy.ImageType);
  69.  
  70.                     break;
  71.                 }
  72.             }
  73.  
  74.  
  75.     }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
May 29 '10 #1
8 8596
Frinavale
9,735 Expert Mod 8TB
The reason only the image is showing up is because you are writing it to the Response output stream.

You have the right idea here....but you want to move this code into an ASPX page of its own. This page will retrieve the image from the database and write it to the response output stream. (You'll have to change the header content type to be "image")

So you have a ASPX page that will return binary-image content instead of HTML (which is what's normally returned).

To display this in a particular place you will use an Image control. Set the Image control's source so that it calls the ASPX page that returns the image.

The thing is that you will need to specify the ID of the image using the URL :)

So you have something like this....


An ASPX page, called "Thumbnail", that will serve the image to the browser (the actual asp code for this doesn't matter because it's going to be over written when you write the image to the Response output stream...this is the C# code responsible for that):
Expand|Select|Wrap|Line Numbers
  1. public partial class Thumbnail : System.Web.UI.Page
  2. {
  3.         protected void Page_Load(object sender, EventArgs e)
  4.         {
  5.             Response.ContentType = "image/jpg";
  6.             string id = Request.QueryString("ID");
  7.             selectedImageDisplay(id);
  8.         }
  9.  
  10.         private void selectedImageDisplay(string id)
  11.         {
  12.             List<DummyBLL> ListImages = dummy.DisplayImage();
  13.             foreach (DummyBLL ImageObject in ListImages)
  14.             {
  15.                 if (ImageObject.ImageId.ToString() == id)
  16.                 {
  17.                     dummy.ImageId = ImageObject.ImageId;
  18.                     dummy = dummy.displayImage();
  19.                     Response.ContentType = dummy.ImageContentType;
  20.                     Response.BinaryWrite(dummy.ImageType);
  21.  
  22.                     break;
  23.                 }
  24.             }
  25.         }
  26. }
Now, back in your ImageDemo ASPX page you would have something like the following (notice at the bottom how I added an Image control):
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ins_Image_in_DB.aspx.cs" Inherits="ImageDemo" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <h1>Insert Image In Database</h1>
  11.     <div id="dv_visible" runat="server">    
  12.     <form id="form1" runat="server" method="post">
  13.     <div>
  14.     <table border="3" cellspacing="3" width="300px">
  15.     <tr>
  16.     <td>
  17.     <b>Image_Name</b>
  18.     </td>
  19.     <td>
  20.     <asp:TextBox ID="txtImageName" runat="server" ></asp:TextBox>
  21.   </td>
  22.  </tr>
  23.      <tr><td>
  24.      <b>ImageUpload</b>
  25.      </td>
  26.      <td>
  27.     <asp:FileUpload ID="UploadImage" runat="server" />
  28.     </td>
  29.     </tr>
  30.     <tr>
  31.     <td colspan="2" align="right">
  32.     <asp:Button ID="AddImage" runat="server" Text="InsertImage" Font-Bold="true" 
  33.             onclick="AddImage_Click"/>
  34.     </td>
  35.      </tr>
  36.     </table>
  37.     <asp:Label ID="Message" runat="server"></asp:Label>
  38.     </div><h1>Select Image</h1>
  39.     <asp:DropDownList ID="ddlImageDisplay" runat="server" AutoPostBack="True"></asp:DropDownList>
  40.  
  41.  
  42. <asp:Image ID="DummyBLLImage" runat="server" />
  43.  
  44.     </form>
  45.     </div>
And in your ImageDemo's C# code...change the selectedImagedisplay method to provide the ImageUrl source for the new Image control...having it point to the Thumbnail.aspx page that provides the binary-image to the Image control:
Expand|Select|Wrap|Line Numbers
  1. private void selectedImagedisplay()
  2. {
  3.   string id =  ddlImageDisplay.SelectedValue;
  4.   DummyBLLImage.ImageUrl = "~/Thumbnail.aspx?id="+id;
  5. }
-Frinny
May 31 '10 #2
@Frinavale
Actually i have little problem to understand this
by the in a Selected Display method you Pass a url in imageurl property
But in a ImageUrl field we pass the path of image at which the image is saved so that it display the image at image control
But you Pass that url on which image is not saved.
I confused at this thing.

what i got from ur code i try to describe.
By the way you forget to call the seletedimage display method ,
first we have to call this method , after that you access the id from the query string....
& pass this id to another selectedImagedisplay(id)
So the image is retrieved from that method....

little confused here
Jun 1 '10 #3
Frinavale
9,735 Expert Mod 8TB
Hmm...I'm confused about what you're confused about.
I don't know the details of your BLLDummy class so I'm not sure how it works. I based the example code off of your posted code since I have no database with pictures in it or class that retrieves them to work with.

Here on the basic details on what I was trying to get at.
  • You need to retrieve the image from the database when an <img> HTML tag requests it (ASP.NET Image controls are rendered as <img> HTML). The only way that the <img> tag can get the image is for it to request the image.
  • Create a "Thumbnail.aspx" page that the <img> tag can call to retrieve the picture.
  • In order to retrieve the correct image we must tell the Thumbnail.aspx page which image to retrieve. To do that we pass the "id" of where to locate the "image" using the URL. This way you can set the <img src="Thumbnail.aspx?id=theIdOfTheImage" />. You can dynamically set this by setting the Image.ImageUrl = "~/Thumbnail.aspx?id=theIdOfTheImage".
  • The Thumbnail.aspx page contains all the code necessary to retrieve the image....that's all it does.

-Frinny
Jun 1 '10 #4
@Frinavale
Sorry for late reply i was too much busy these days..........
let me renind you that i have a problem to display the image in asp image control...

what actually happiened
Expand|Select|Wrap|Line Numbers
  1.  Response.ContentType = dummy.ImageContentType;
  2.  Response.BinaryWrite(dummy.ImageType);
  3.  
the above just display the image according to its actual size.
the code
Expand|Select|Wrap|Line Numbers
  1. DisplayImage.ImageUrl = "~/Ins_Image_in_DB.aspx?Imageid=" + identity;
  2.  
so that still i m not able to put image at image control.
even i use this statement.after bineray.write statement.
i send my code please check it.......
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14. using System.Collections.Generic;
  15. using BllDummy;
  16. using System.Drawing;
  17.  
  18. public partial class ImageDemo : System.Web.UI.Page
  19. {
  20.     DummyBLL dummy = new DummyBLL();
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.         dv_visible.Visible = true;
  24.  
  25.         if (!Page.IsPostBack)
  26.         {
  27.             dropdownImagelist();
  28.  
  29.         }
  30.         selectedImagedisplay();
  31.  
  32.  
  33.  
  34.  
  35.     }
  36.     protected void AddImage_Click(object sender, EventArgs e)
  37.     {
  38.         Stream imgStream = UploadImage.PostedFile.InputStream;
  39.         int ImgLength = UploadImage.PostedFile.ContentLength;
  40.         dummy.ImageName = txtImageName.Text;
  41.         dummy.ImageContentType = UploadImage.PostedFile.ContentType;
  42.         byte[] imageType = new byte[ImgLength];
  43.         dummy.ImageType = imageType;
  44.         int n = imgStream.Read(imageType, 0, ImgLength);
  45.         int roweffected = dummy.addImage();
  46.         if (n >= 0)
  47.             Message.Text = "Image Is Saved In a database";
  48.         else
  49.             Message.Text = "error Occur";
  50.         selectedImagedisplay();
  51.  
  52.  
  53.     }
  54.     private void dropdownImagelist()
  55.     {
  56.         List<DummyBLL> ListImages = dummy.DisplayImage();
  57.         ddlImageDisplay.Items.Add(new ListItem("--SelectImage---", "0"));
  58.         foreach (DummyBLL ImageObject in ListImages)
  59.         {
  60.             ddlImageDisplay.Items.Add(new ListItem(ImageObject.ImageName.ToString(), ImageObject.ImageId.ToString()));
  61.  
  62.  
  63.         }
  64.     }
  65.     private void selectedImagedisplay()
  66.     {
  67.         List<DummyBLL> ListImages = dummy.DisplayImage();
  68.         Table table = new Table();
  69.         TableRow tablerow = new TableRow();
  70.         foreach (DummyBLL ImageObject in ListImages)
  71.         {
  72.             /*if (ImageObject.ImageId.ToString() == ddlImageDisplay.SelectedValue)
  73.              {
  74.                  dummy.ImageId = ImageObject.ImageId;
  75.                  dummy = dummy.displayImage();
  76.                  Response.ContentType = dummy.ImageContentType;
  77.                  Response.BinaryWrite(dummy.mageType);
  78.  
  79.                  break;
  80.              }*/
  81.  
  82.  
  83.  
  84.  
  85.             TableCell cell = new TableCell();
  86.             cell.ForeColor = System.Drawing.Color.Chocolate;
  87.             cell.BackColor = System.Drawing.Color.LemonChiffon;
  88.  
  89.  
  90.             LinkButton ImageLink = new LinkButton();
  91.             ImageLink.ID = ImageObject.ImageId.ToString();
  92.             ImageLink.Text = ImageObject.ImageName;
  93.             ImageLink.CommandArgument = ImageObject.ImageId.ToString();
  94.             ImageLink.CommandName = ImageObject.ImageId.ToString();
  95.             ImageLink.Command += new CommandEventHandler(btnImage_Click);
  96.             cell.Controls.Add(ImageLink);
  97.  
  98.             tablerow.Cells.Add(cell);
  99.  
  100.  
  101.  
  102.  
  103.         }
  104.         table.Rows.Add(tablerow);
  105.         panelImage.Controls.Add(table);
  106.     }
  107.     protected void btnImage_Click(object sender, CommandEventArgs e)
  108.     {
  109.  
  110.         List<DummyBLL> ListImages = dummy.DisplayImage();
  111.         foreach (DummyBLL ImageObject in ListImages)
  112.         {
  113.             if (e.CommandName == ImageObject.ImageId.ToString())
  114.             {
  115.                 int id = Convert.ToInt32(e.CommandArgument.ToString());
  116.                 dummy.ImageId = id;
  117.                 string identity=id.ToString();
  118.                 dummy = dummy.displayImage();
  119.                 DisplayImage.ImageUrl = "~/Ins_Image_in_DB.aspx?Imageid=" + identity;
  120.                 Response.ContentType = dummy.ImageContentType;
  121.                 Response.BinaryWrite(dummy.ImageType);
  122.  
  123.                 lblmess.Text = identity;
  124.                 break;
  125.             }
  126.  
  127.  
  128.         }
  129.     }
  130.  
  131. }
  132.  
  133.  
The image is displayed with its original size.
while i set the width & height of image in image control
but this property is not applied.
Thank You.......
Jul 3 '10 #5
Frinavale
9,735 Expert Mod 8TB
You have to move the code that writes the image to the Response into another aspx page...It looks like you are going to call that page "Ins_Image_in_DB"....I would have called it "Thumbnail".

-Frinny
Jul 6 '10 #6
@Frinavale
I will exaplain what i do......
I crette a "Ins_Image_in_DB".aspx page ok.
after that i add a image control to this aspx page.
when i retrieve the image from the database . i doesnot use the image control just retrieve the image from the database & display the image by using binary.write .
in its orginal size.
why we can't do here in this page.
Jul 6 '10 #7
Hi AnagJohari,

You keep saying the same thing over and over - try to listen to what Frinny is saying. He is describing a method that I have used many times so I can attest that it is the proper way to do it.

The aspx page that is responsible for rendering the image is to have NO MARKUP AT ALL except for the page directive. DO NOT add an image control to this page! Its code-behind will retrieve the image from the database based on an image ID that you can retrieve from the querystring and write it to the response stream.

Add the image control to the page that is to display the image and set its image URL to the page that we were talking about in the previous paragraph. The url should also have a querystring variable with the ID of the image that you want to show.
Jul 7 '10 #8
Tnx frinny you are great man. now i get my image at a right place..
tnx critopher nitro your comments also helps me to comeout from my situation......

Ok i done this
but try to understand this concept
i m very new .net i just finish my study so sometime what did u say not able to get easily.
so sorry for this
i want to clear this concept
when i call image control on image_insert_in Db not get
when i transfer my code in thumpsnail its work plz
little explain this....
Jul 7 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tan | last post by:
Hi I am desperate for any help with display image in Gridview I have a gridview contain surname , forename ..... and image. I could not display image as my database store the column image as...
3
by: harish | last post by:
Hi friends I am facing problem as follow I can't display image from SQL Database to Picture box Control. Here are the codes that I am writing Dim arrPicture() As Byte = _
2
by: RedSouljaz | last post by:
Hi, How to display image that was saved in database ms sql server 2000 into picture box. The field type that I use in database is Images I can save to database but cannot show from database. ...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
7
by: eholz1 | last post by:
Hello Group, Perhaps you can help me. I have a mysql db, that holds images. Images are encoded using base64_decode/encode, etc. Image data seems fine. I have a view.php page that is supposed...
7
by: alexseow | last post by:
Query.asp <%@ LANGUAGE="VBSCRIPT" %> <!-- #include file="../../includes/dbconn.asp"--> <% dim MyRs, sqlstr, MyConn Response.Expires = 0 Response.Buffer = TRUE Response.Clear
1
by: shalini328 | last post by:
I am using ASP.net 1.1 C# and database is MS access.Can you tell me how can i display image from database
1
by: saadkhan | last post by:
I want to display image in <img> tag using database. I just need to know that how could i be able to define path or whatever to <img> tag. I have no probem in making queries to database.....plz help
5
by: SafaaDalloul | last post by:
Please I want know How I can Display Image from database randomly when the web page refresh in Asp.net by C# code
1
by: Laxmikant | last post by:
I am using VB6 as front end, Access 2007 as backend, crystal report 8 for reporting. I want to display image on my crystal report output, based on path saved in a table in Access Database. Is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.