simple example
1. My example Database stucture is as follows:
[FileId] UNIQUEIDENTIFIE R PRIMARY CLUSTERED KEY
[Data] IMAGE NOT NULL
2. ViewImage.aspx. cs code
-- BEGIN CODE --
using System;
using System.Data;
using System.Data.Sql Client;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
public partial class ViewImage : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Guid id;
try
{
id = new Guid(Request.Qu eryString["id"]);
}
catch
{
// display a default image or message
// saying id is invalid
return;
}
this.AttachImag eContent(id);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
private void AttachImageCont ent(Guid id)
{
byte[] content = this.GetImageCo ntent(id);
Response.ClearC ontent();
Response.Conten tType = "image/jpg"; // get it from database
Response.Output Stream.Write(co ntent, 0, content.Length) ;
Response.End();
}
private const string ConnectionStrin g =
"server=(local) ;uid=;password= ;" +
"database=BlobD atabase;pooling =true;max pool size=1;min pool size=1;";
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private byte[] GetImageContent (Guid id)
{
// should be a stored procedure
string query = "SELECT [Data] FROM [File] WHERE [FileId] = @FileId";
SqlConnection connection = new SqlConnection(C onnectionString );
SqlCommand command = new SqlCommand(quer y, connection);
SqlParameter parameter = new SqlParameter("@ FileId",
SqlDbType.Uniqu eIdentifier);
parameter.Direc tion = ParameterDirect ion.Input;
parameter.Value = id;
command.Command Timeout = 120;
command.Command Type = CommandType.Tex t;
command.Paramet ers.Add(paramet er);
try
{
connection.Open ();
return (byte[]) command.Execute Scalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection.Sta te != ConnectionState .Closed)
connection.Clos e();
}
}
}
-- END CODE --
3. Aspx Page html code
-- BEGIN CODE --
<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs"
Inherits="_Defa ult" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
<style type="text/css">
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns=False>
<Columns>
<asp:TemplateFi eld>
<ItemTemplate >
<img
src='ViewImage. aspx?id=<%#Data Binder.Eval(Con tainer.DataItem , "FileId")%> '/>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:BoundFie ld DataField="Firs tName"/>
</Columns>
</asp:GridView>
</form>
</body>
</html>
-- END CODE --
4. Apsx behind c# code
-- BEGIN CODE --
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
GridView1.DataS ource = GetDataSource() ;
GridView1.DataB ind();
}
private DataTable GetDataSource()
{
DataTable table = new DataTable();
table.Columns.A dd("FileId", typeof(string)) ;
table.Columns.A dd("FirstName" , typeof(string)) ;
DataRow row = table.NewRow();
row[0] = "e8302aa9-e82c-4d20-b3ae-79f8c5009dfb";
row[1] = "George W Bush";
table.Rows.Add( row);
return table;
}
}
-- END CODE --
Hope this helps
--
Milosz Skalecki
MCP, MCAD
"CLEAR-RCIC" wrote:
I have several images i want to display in an ASP.Net application. The
images are being passed to me in binary format from another application. Is
there a good way to write them directly to an HMTL page without having to
save them to the server and create a URL to the virtual directory?
FYI: I currently am doing this with just single images. I do a
Response.Binary Write(byte) to display the one image. The advantage is that I
never have to worry about deleting the images at some later time.
Unfortunatly, this method does not lend itself to multiple images.