Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have a small situation regarding images in asp.net.
I`ve inserted image in database using code below. Also, important thing is that I don`t use image table (with id, datatype etc), but image field is in my table Products.
public void setObject(Products ea)
{
.//storing other entities
.//
.//
   if(fu_image.PostedFile!=null && fu_image.PostedFile.ContentLength>0)
            {
                string ext = System.IO.Path.GetExtension(fu_image.PostedFile.FileName).ToUpper();
                if (ext == ".JPG" || ext == ".JPEG")
                {
                    byte[] content = new byte[fu_image.PostedFile.ContentLength + 1];
                    fu_image.PostedFile.InputStream.Read(content, 0, fu_image.PostedFile.ContentLength);
                    System.Data.Linq.Binary image = new System.Data.Linq.Binary(content);
                    ea.Image=image; //

                   
                }
           
            }
}


My problem is displaying image from database in my image control in aspx page.
I`ve read tons of tutorials, but everyone seems to be with separate Image table and than is easier to access it using f.e. http handler.

My product page shows (in listview) all the info about products, and I want to show also picture for each product.

Any tips?

Thank you.
Posted

 
Share this answer
 
Hi,
The simple way to read image from SQL Server is to use a handler (.ashx file)

write the below code in the ashx file as:

C#
public void ProcessRequest(HttpContext context)
    {
        Int32 empno;
        if (context.Request.QueryString["id"] != null)
            empno = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");
        
        AccountType tblAccount = new AccountType();
        tblAccount.LoadByPrimaryKey(empno);

        context.Response.ContentType = "image/jpeg";
        Stream strm = new MemoryStream((byte[])tblAccount.HeaderLogo);
        byte[] buffer = new byte[4096];
        int byteSeq = strm.Read(buffer, 0, 4096);

        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 4096);
        }        
    }


Here the AccountType is my Class and you should use your Product class Logics to pass the perameter.

and call the image as:
C#
ImgHeaderLogo.ImageUrl = "~/HeaderLogo.ashx?id="YOURID";
 
Share this answer
 
v2
Comments
ellisbay 7-Feb-12 7:26am    
Problem is that I don`t have a where clause in my query...
I`m showing all products in that listview.
No solution yet!!!
I`m following all the steps, but somewhere is a problem and I can`t figure it out :(
So, I`m going to specife my program structure in details, so maybe someone will now what is wrong with it.
I have a table PRODUCTS which has foreign key ImageFK on IMAGE table.
IMAGE table ID is nchar(10), also ImageFK in PRODUCTS is nchar(10).

In one aspx page I have a listview of my products with some records (f.e. Product Name, Product Price..) and I want also to show my product image.
So the query which is bind to this listview go like this:
var query=(from x in Products
select new 
{
x.Id, //Product ID
x.ProductName,
x.ProductPrice,
x.ImageFK //Foreign key for IMAGE table

}).ToList();
return query;


Everything is shown perfectly, beside Image :(

I`ve created ashx page "ImageShow.ashx", the code is shown below: //
public class ImageShow : IHttpHandler
   {

       public Stream ShowEmpImage(string id)
       {
           using (DCKlaseDataContext dc = new DCKlaseDataContext())
           {

               var queryImage = (from x in dc.Images    // find image where id`s are the same (id in Product table and Id in Image table)
                           where x.Id == id
                           select x).FirstOrDefault();


               return new MemoryStream(queryImage.Image.ToArray());


           }

       }
       public void ProcessRequest(HttpContext context)
       {
           string id = context.Request.QueryString["ImageFK"].ToString();


               context.Response.ContentType = "image/jpeg";

              Stream strm = ShowEmpImage(id);

              byte[] buffer = new byte[4096];
              int byteSeq = strm.Read(buffer, 0, 4096);

            while (byteSeq > 0)

            {

               context.Response.OutputStream.Write(buffer, 0, byteSeq);

                 byteSeq = strm.Read(buffer, 0, 4096);

             }

       }

And final step is on my image field in my aspx page, to add image url (which is this ashx page):
<asp:image id="img_1" runat="server" imageurl="ImageShow.ashx?ImageFK=<%# Eval("ImageFK") %>" xmlns:asp="#unknown" />


And when I build the program, it show error on
return new MemoryStream(queryImage.Image.ToArray());


with message: Object reference not set to an instance of an object.

Anyone?!

Thank you..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900