PDA

Click to See Complete Forum and Search --> : RESOLVED!!! Display Image from SQL Database


ARPRINCE
May 26th, 2004, 03:23 PM
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

Lethal
May 27th, 2004, 07:46 AM
You could load it up into a memory stream and create your image object from the byte stream.

ARPRINCE
May 27th, 2004, 07:58 AM
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.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim StrCN As String
StrCN = "XXXXXXXXXXX...."

Dim cn As New SqlConnection(StrCN)
Dim cmd As New SqlCommand("SELECT ImageID,ImagePix FROM Image", cn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds, "Image")
Dim c As Integer = ds.Tables("Image").Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables("Image").Rows(c - 1)("ImagePix")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
'DISPLAY IMAGE IN PICTURE BOX
picBlob.Image = Image.FromStream(stmBLOBData)
End If
End Sub

Magiaus
May 27th, 2004, 09:37 AM
this code turn a web form into a picture you just set an img src=DBPicture.aspx?pid=allthe-guid-fsdgdsfg-sdfgsdfgsdfg
C#(sorry)

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
}

ARPRINCE
May 27th, 2004, 10:12 AM
I'm relatively new with ASP.NET and use VB.NET code behind but I will look into your code.

Thanks for your post.

ARPRINCE
May 27th, 2004, 03:41 PM
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. :bigyello:

Here's my SQL table Structure:


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.


<IMG SRC="ShowEmployeePicture.aspx?ImageID=2">


(4) Added another webform and named it ShowEmployeePicture.aspx
(5) Added the VB code below:
Imports System.Data.SqlClient

Public Class ShowEmployeePicture
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim iImageID
iImageID = Request.QueryString("ImageID")

Dim connectionString As String
connectionString = "XXXXXXXX"

Dim myConnection As SqlConnection
myConnection = New SqlConnection(connectionString)

Dim myCommand As New SqlCommand("SELECT ImageType,ImagePIX FROM Image WHERE ImageID = " & iImageID, myConnection)

myConnection.Open()
Dim myDataReader As SqlDataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

Do While (myDataReader.Read())
Response.ContentType = myDataReader.Item("ImageType")
Response.BinaryWrite(myDataReader.Item("ImagePix"))
Response.Flush()
Response.End()
Loop

myConnection.Close()
End Sub

End Class



Thank you all!