Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database image willnot show on web page 1

Status
Not open for further replies.

whill1

Technical User
Feb 14, 2004
46
US
Hi, I am trying to set up a web page so that I have a picture and a description. I have the info in a database and using a datagrid. when I open the page all info is there except in the image field I get "System.Byte[]" and no picture.
I do not have to use a datagrid and open to any ideas.
I assumed this would be very simple and.....
Can anyone shed some light on how to accomplish this?
I have windows XP, visual studio, web matrix.
Also use SQL Server 2000 or Access.. Tried both, Same result
Thanks
whill1
 
Your image seems to be stored as binary data in the database.

YOUR WEB FORM ASPX PAGE:
<asp:DataGrid Runat="server" ID="DataGrid1">
<Columns>
<asp:TemplateColumn >
<ItemTemplate>
<asp:image runat="server" ID="imgMyImage" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>




YOUR WEB FORM CODE BEHIND:

Private ImageIndex As Integer = 0
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim MyImage As System.Web.UI.WebControls.Image = e.Item.FindControl("imgMyImage")
MyImage.ImageUrl = "ShowImage.aspx?index=" & ImageIndex & "&ImageType=jpeg"
Dim bytes() As Byte = DataBinder.Eval(e.Item.DataItem, "MyDataBaseFieldNameForTheImage")
Session("ImageBytes" & ImageIndex) = bytes
ImageIndex = ImageIndex + 1

End If
End Sub



YOUR SHOWIMAGE.ASPX PAGE
'Nothing to add the ASPX portion of the page



YOUR SHOWIMAGE.ASPX CODE BEHIND
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim outPutFormat As System.Drawing.Imaging.ImageFormat
Dim contType As String
Dim Index As Integer = Request.QueryString("index")
Dim ImageType As String = Request.QueryString("imagetype")

Select Case ImageType
Case "png"
outPutFormat = System.Drawing.Imaging.ImageFormat.Png
contType = "image/png"
Case "jpg"
outPutFormat = System.Drawing.Imaging.ImageFormat.Jpeg
contType = "image/jpeg"
Case "gif"
outPutFormat = System.Drawing.Imaging.ImageFormat.Png
contType = "image/gif"

End Select
Dim bytes() As Byte = Session("ImageBytes" & Index)
Dim stmMemory As IO.MemoryStream = New IO.MemoryStream(bytes)

Response.Clear()
Response.ContentType = contType
stmMemory.WriteTo(Response.OutputStream)
Response.End()
End Sub


NOTES:
It is better on the ShowImage page to pass it some information to access the Bytes[] directly from the database. I used Session Variables to simplify the concept but that would not be the best use of memory to use Session Variables unless the images are just small thumbnails.
 
RTomes, Thank you for your reply. This seems to be alittle over my head as I didn't understand what to do with the info. For example Webform.aspx vs. showimage.aspx pages.

Here are other conciderations:

I am using only one page (Defaulf.aspx).
In Web Matrix I Drug a table onto the default.aspx page which created an MxDataGrid.

2 questions:

1. If I can figure out how to get the images into my database as anything other than binary would this solve my problem?

2. Do I have to put the images in the database in order that the images stay syncronized with info in a table.

Man this web stuff really adds frustration to what is normally straightforward.
I have been working with databases and windows forms for a few years now and getting fairl comfortable with it. Now I seem to be haveing a hard time with the asp.net/Internet learning curve.
Thanks,
whill1


 
Storing images in databases, as far as I know, means that they will always be in binary format so regardless of whether it is an ASP.NET app or a VB.NET app you would still have the same hurdle to get over.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks ca8msm,
Not sure about VB.NET.
I have an Access front end that gets it's data from SQL Server that i set up to see if the pictures are viewable and work fine (as expected). What is it that keeps the pictures from loading just because they are going to a webpage instead of a windows form?
Thanks again all,
whill1
 
HTTP Protocol. Big difference form a Web Forms and Windows Forms. The Web Form datagrid will write plain old HTML in the web page. An image appears like <img src=" />. In your case you need to kind of dynamically produce the URL will point to.
In this case it comes looking something like:
<img src=" />.
It's not going to embed the image in code of the web page so you have to pass it an address as to where to get the image. The MyImage.aspx returns the same bytes to the browser as if you took the bytes, created a hard file on your file system and used that for your URL.
 
Thanks again Rtomes,
It appears that this will not be as automated as I had hoped.
It sounds like I must put all my images in a folder and in my database put the name of the image, then use code that will set the image name from the table into the URL pointer.

Does this sound like a reasonable way to do this?
It will take me some time to figure out how to do this but I will give it a try.
Thanks,
whill1
 
The images come out of the database as a Byte[] Array.
All you need to do is grab the Bytes[] from above and:


' Create a file and write the byte data to a file.
Dim oFileStream As System.IO.FileStream
oFileStream = New System.IO.FileStream("c:\myImageName.jpg", System.IO.FileMode.Create)
oFileStream.Write(bytes, 0, bytes.Length - 1)
oFileStream.Close()
 
RTomes, Sorry I'm such a newbie at this that I do not understand how to do what you have explained. I do appreciate your (and others) help though!! I am learning but seem to be at the point where the more I try the more confused I get. ha,ha I am still trying to understand what should go on what page. Like Webform, codebehind, ascx, aspx, asmx, etc... I have been studying the microsoft walkthroughs -- Commerce, Portal and Community but they are so massive that I can't figure out whats doing what.

do you know where I can find a sample site that is small enough to understand?
Thanks,
whill1
 


Here is the part your interested in:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
MemoryStream stream = new MemoryStream ();
SqlConnection connection = new SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
try
{
connection.Open ();
SqlCommand command = new SqlCommand ("select Picture from Image", connection);
byte[] image = (byte[]) command.ExecuteScalar ();
stream.Write (image, 0, image.Length);
Bitmap bitmap = new Bitmap (stream);
Response.ContentType = "image/gif";
bitmap.Save (Response.OutputStream, ImageFormat.Gif);
}
finally
{
connection.Close ();
stream.Close ();
}


Goto this for full article:
 
Thanks again I will work on this tomorrow and let you know how things are going.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top