PDA

Click to See Complete Forum and Search --> : Retriving images from a database


mcp87
Jan 26th, 2005, 06:03 AM
Hey all I need some help with displaying some images that are in a database. Currently when I try to display them they come out in binary form and I am stumped on how to correct it.

Server info:

WWW Server:
Windows Server 2003
ASP .Net
Pages are .aspx written in VBScript

Database Info:
MS SQL Server 2000

Problem code:

<%
Dim rs
rs = Server.CreateObject("adodb.recordset")
rs.Open("Select * From avatar", Application("Conn"))
If not rs.eof then
Do Until rs.eof
Response.ContentType = "image/gif"
Response.binarywrite(rs.fields("a_avatar").value)
rs.movenext
Loop
rs.close
End If
%>

As stated above the result is display of the three images in the database in binary form.
http://test.adiktclan.com/test.aspx

Any help is greatly appreciated.

Thanks,

John K
MCSA, A+

Magiaus
Jan 26th, 2005, 10:12 AM
This is code from my dbimage.aspx page.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PKPromo.Web.Images
{
/// <summary>
/// Summary description for Image.
/// </summary>
public class Image : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if(Request["pid"] != "" && Request["pid"] != null)
{
Business.Images.Image img = new Business.Images.Image(new System.Guid(Request["pid"]));
Response.ContentType = "Image/jpeg";
Response.BinaryWrite(img.Raw);
Response.Flush();
Response.End();
}
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
}
}


C# but you should be able to get the idea. Basicly you just turn the page into an image. <img src="dbimage.aspx?pid=imageid">

The important parts are the content type and the binary write.