We help IT Professionals succeed at work.

Asp.Net I need help on a HTTP Handler that will stream binary data in order to display an image.

westdh
westdh used Ask the Experts™
on
My SQL has already selected the image & returned the image in a value called: ImageData1
I need an HTTP Handler that will stream ImageData1's content and display it.
-----------------------------------------------------------------------------------------------------
I have an image control  a .dll that does this already and it works...But I need one so that I can see the source code.

this is the image control that works:
 <Rbm:RbmBinaryImage ID="Image10" runat="server" Height="100px" DisplayThumbnail="true"
                  ImageContent='<%#IIf(Typeof(Eval("ImageData1")) is DBNull, Nothing, Eval("ImageData1"))%>' />

this is what I need in a http handle to stream and Eval ImageDate1.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Information Security Specialist
Commented:
use this code to get the image

  Response.ContentType = "image/jpeg"
                        Response.Expires = 0
                        Response.Buffer = True
                        Response.Clear()
                        Response.BinaryWrite(CType(dt.Rows(0)("Picture"), Byte()))
                        HttpContext.Current.ApplicationInstance.CompleteRequest()

where dt.Rows(0)("Picture") is the binary image return from DB

Author

Commented:
Below is the script...you refered me to:
----------------------------------------------------------------------------------------------------------
using System;using System.Web;
using dsImagesTableAdapters;

public class DisplayImage: IHttpHandler
{
   public void ProcessRequest (HttpContext context)
   {
        int iImageId;if (context.Request.QueryString["id"] != null) {
              iImageId = Convert.ToInt32(context.Request.QueryString["id"]);
         }
         else {
          throw new ArgumentException("No parameter specified");
      }

      DataSet dsImages = GetDataSetImagesfromDatabase();
      DataTable dtImgTable = dsImages.Tables[0];
       
        if (dtImgTable .Rows.Count > 0) {
        if (dtImgTable [0]["Image"] != DBNull.Value)
        {
            context.Response.ContentType =
            dtImgTable [0]["ImageType"].ToString();
            context.Response.BinaryWrite((byte[])dtImgTable[0]["Image"]);
        }
      }
    }

    public bool IsReusable
    {
      get {
        return false;
        }
    }
}
---------------------------------------
what I'm saying is that i already have selected and return the image

what I need to need is how to modify the code in the above script.

at this point:  I need to replace this code and be able to pass in my ImageData
directly with out retruning to theDB.

      DataSet dsImages = GetDataSetImagesfromDatabase();
      DataTable dtImgTable = dsImages.Tables[0];
       
        if (dtImgTable .Rows.Count > 0) {
        if (dtImgTable [0]["Image"] != DBNull.Value)
        {
            context.Response.ContentType =
            dtImgTable [0]["ImageType"].ToString();
            context.Response.BinaryWrite((byte[])dtImgTable[0]["Image"]);
        }
      }

Author

Commented:
My opening statement in the post declared that I have already returned the image from the database
The Binary Content is the returned in ImageDate1,

What I need is to be able to stream it using a http handler
from that point.

My SQL has already selected the image & returned the image in a value called: ImageData1
I need an HTTP Handler that will stream ImageData1's content and display it.
   This is what I was asking.

everysample has it going thru some alogorithm accessing a DB,

Author

Commented:
     Can I replace a querystring passing "picture" instead of dt.Rows(0)("Picture"),

Response.ContentType = "image/jpeg"
                        Response.Expires = 0
                        Response.Buffer = True
                        Response.Clear()
                        Response.BinaryWrite(CType(dt.Rows(0)("Picture"), Byte()))
                        HttpContext.Current.ApplicationInstance.CompleteRequest()

Author

Commented:
I have more info.
I am passing a querystring to ShowImage.ashx of a Picture in ImageData1

<img alt="httpHandler" src="ShowImage.ashx?Picture='<%# Eval("ImageData1")%>'">

the problem is it ImageData1 is reconized as a 'System.Byte[]' meaning that it is still in a pack mode and needs to be streamed.

ShowImage.ashx?Picture='System.Byte[]'

------------------------------
  <img alt="httpHandler" src="ShowImage.ashx?Picture='<%# Eval("ImageData1")%>'">

ShowImages.ashx code is in the code block and it needs correcting in order to unpack ImageData Binary Image Data to a displayable Image. I am stuck I donot no how....See Image also.
using System;using System.Web;
using dsImagesTableAdapters;

public class DisplayImage: IHttpHandler
{
   public void ProcessRequest (HttpContext context)
   {

         Response.ContentType = "image/jpeg"
         Response.Expires = 0
         Response.Buffer = True
         Response.Clear()
         string v = Request.QueryString["Picture"];
         respone.write("test: " + v)
         Response.BinaryWrite(CType(Request.QueryString("Picture"), Byte())
         HttpContext.Current.ApplicationInstance.CompleteRequest()

    }

} 

Open in new window

sshot-9.png

Author

Commented:
Thanks I am trying now to pass it as a querystring.
and I have reposted my question