Explanation:
This Article is continuity of our Article
Loading Images into SQL Server with C#. Here We will discuss about
displaying a unique image for each user. This means every user will view an image
he is allowed to or he have uploaded in his own profile
Article Requierments:
To make sure we undertsand the Article better, before we move next we need to make sure that we know about the following:
- GridView/DataGrid Control
- ASP .NET 2.0 ObjectDataSource
- ASP .NET 2.0 Membership Objects
- ASP .NET Generic Handlers
Now, when we made sure that we have the basic knowledge of what we will use in our article, we can go a head. Our Gridview look like this;
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
AutoGenerateColumns=false >
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
/>
<asp:BoundField DataField="message" HeaderText="message"
SortExpression="message" />
<asp:TemplateField HeaderText="User Name">
<asp:TemplateField HeaderText="Picture">
<ItemTemplate>
<img src='PicHandler.ashx?UserName=<%# Eval("username")%>'
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Instead of using BoundField we are using ItemTemplate to cerate our
own customised Template. We are accessing our Images through a link which is pointing
to our Handler (.ashx). So how does actually the Handler access images and present
them to us? Handler process our Request and cerate a Proper Responce
to it. This Responce Handling is actually what a Handler all about.
Here we will use an ObjectDataSource Object to access the current logged
in user. Our ObjectDatasource Look like this:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="GetUsersData"
SelectMethod="GetUserName"
OldValuesParameterFormatString="original_{0}"/>
Here GetUsersData is our Class and GetUserName is a Static Method
which brings the Current Logged-in User. To detrermin the Current Logged-in User you
can use Membership and MembershipUser Objects.
We can create a Handler by right clicking on the Project > Add New Item > Select
Generic Handler > Add . You can edit this newly created Handler. Now see how does
our Handler look like;
<%@ WebHandler Language="C#" Class="PicHandler" %>
using System;
using System.Web;
using System.IO;
using System.Configuration;
public class PicHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Server);
context.Response.BufferOutput = false;
// Setup the PhotoID Parameter
Stream stream = null;
string userName = context.Request.QueryString["UserName"];
if (userName != null && userName != "")
{
stream = OurUsersData.GetUserPic(userName);
if (stream == null)
{
stream = OurUsersData.GetUserPic("defaultPicture");
}
}
// Write image stream to the response stream
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
public bool IsReusable
{
get{ return false;}
}
}
Where "ourUsersData" is Class defined in the "App-code"
folder and "GetUserPic" is a satic function. The bold
block is showing how to get an Image. If the user did not uploaded his picture the
default image will be displayed instead.
How are we accessing our Database? If you are saving images to the User Profile
in ASP .NET 2.0 then you need to create a method for accessing an image from the Profile
System otherwise, if you are saving Images in your own Database then you can access
the database by using ADO .NET 2.0. Make sure that the return type of your Method is a Stream.
|