Results 1 to 6 of 6

Thread: RESOLVED!!! Display Image from SQL Database

  1. #1

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    RESOLVED!!! Display Image from SQL Database

    I have an image file in my SQL database (image type) and would want to display it in an Image1 web control. I have been checking other post but couldn't figure out the "HOW TO". Any help or push to the right direction will be highly appreciated.

    Thanks
    Last edited by ARPRINCE; May 27th, 2004 at 03:43 PM.

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You could load it up into a memory stream and create your image object from the byte stream.

  3. #3

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Yup. Researching around on how to retrieve the image, I was able to display the image back on a picture box via WINDOWS APPLICATION. Now, I just need to figure out how to do the same thing on a WEB APPLICATION.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         Dim StrCN As String
    4.         StrCN = "XXXXXXXXXXX...."
    5.  
    6.         Dim cn As New SqlConnection(StrCN)
    7.         Dim cmd As New SqlCommand("SELECT ImageID,ImagePix FROM Image", cn)
    8.         Dim da As New SqlDataAdapter(cmd)
    9.         Dim ds As New DataSet
    10.         da.Fill(ds, "Image")
    11.         Dim c As Integer = ds.Tables("Image").Rows.Count
    12.         If c > 0 Then
    13.             Dim bytBLOBData() As Byte = _
    14.                 ds.Tables("Image").Rows(c - 1)("ImagePix")
    15.             Dim stmBLOBData As New MemoryStream(bytBLOBData)
    16.             'DISPLAY IMAGE IN PICTURE BOX
    17.             picBlob.Image = Image.FromStream(stmBLOBData)
    18.         End If
    19.     End Sub

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    this code turn a web form into a picture you just set an img src=DBPicture.aspx?pid=allthe-guid-fsdgdsfg-sdfgsdfgsdfg
    C#(sorry)
    Code:
    	public class DBPicture : System.Web.UI.Page
    	{
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			if(Request["pid"] != "" && Request["pid"] != null)
    			{
    				System.Data.SqlClient.SqlDataReader r = Data.Helpers.SqlProcedure.ToDataReader("get_pictureByGUID", new System.Data.SqlClient.SqlParameter("@PictureGUID", Request["pid"]), Data._.ConnectionString());
    				if(r.Read())
    				{
    					Response.ContentType = "Image/" + r["PictureFileExt"].ToString();
    					Response.BinaryWrite((byte[])r["PictureImage"]);
    					Response.Flush();
    					Response.End();
    				}
    				r.Close();
    			}
    			else
    			{
    				Response.ContentType = "Image/jpeg";
    				Response.Flush();
    				Response.End();
    			}
    		}
    
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
    		
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			this.Load += new System.EventHandler(this.Page_Load);
    		}
    		#endregion
    	}
    Magiaus

    If I helped give me some points.

  5. #5

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    I'm relatively new with ASP.NET and use VB.NET code behind but I will look into your code.

    Thanks for your post.

  6. #6

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    OK. Finally, I got it!!!!!

    My problem was I was placing everything on one aspx page. After a few more hours of research, here's the finish product for those who may be in need in the future.

    Here's my SQL table Structure:

    Code:
    TABLE NAME = IMAGE
    -----------+----------+-------------
    ImageID    |  Int     | 2
    ImagePix   |  Image   | 0xFFD8FFE...
    ImageType  |  VarChar | JPG
    -----------+----------+-------------
    (1) Using VS.NET, create a new web project.
    (2) I added a webform and named it as ShowEmployee.aspx as my start page.
    (3) I added an HTML Image (instead of Web Image1) and the HTML code below.

    Code:
    <IMG SRC="ShowEmployeePicture.aspx?ImageID=2">
    (4) Added another webform and named it ShowEmployeePicture.aspx
    (5) Added the VB code below:
    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class ShowEmployeePicture
    4.     Inherits System.Web.UI.Page
    5.  
    6.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim iImageID
    8.         iImageID = Request.QueryString("ImageID")
    9.  
    10.         Dim connectionString As String
    11.         connectionString = "XXXXXXXX"
    12.  
    13.         Dim myConnection As SqlConnection
    14.         myConnection = New SqlConnection(connectionString)
    15.  
    16.         Dim myCommand As New SqlCommand("SELECT ImageType,ImagePIX FROM Image WHERE ImageID = " & iImageID, myConnection)
    17.  
    18.         myConnection.Open()
    19.         Dim myDataReader As SqlDataReader
    20.         myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    21.  
    22.         Do While (myDataReader.Read())
    23.             Response.ContentType = myDataReader.Item("ImageType")
    24.             Response.BinaryWrite(myDataReader.Item("ImagePix"))
    25.             Response.Flush()
    26.             Response.End()
    27.         Loop
    28.  
    29.         myConnection.Close()
    30.     End Sub
    31.  
    32. End Class


    Thank you all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width