none
How to Display Image From Byte

    Question

  • I have already received the byte [] of a jpeg image but it won't display.  I am using asp.net 2.0.  I was wondering what the correct way of displaying an image from a database is (I have seen the asp:DynamicImage but when I try to use it, on the build it says it is an unknown server control).  Here is my code:

    File: DisplayImage.aspx

    //Here is the code that I use to call from the control <asp:Image id="eventImage" runat="server"/>

    eventImage.ImageUrl = "~/User/Image.asp?ImagePath=" + evnt.ImagePath;

     

    File: Image.aspx

     

    <%@ Page Language="C#" %>

    <%@ Import Namespace="CollisonDatabase" %>

    <%

    Response.Expires = 0;

    Response.Buffer = true;

    Response.Clear();

    string imagePath = Request.QueryString.Get("ImagePath");

    CollisonDatabase.Database db = new CollisonDatabase.Database();

    byte[] bytes = db.GetImage(imagePath);

    Response.ContentType = "Image/JPEG";

    Response.BinaryWrite(bytes);

    Response.End();

    %>

    Tuesday, March 21, 2006 11:56 PM

Answers

All replies

  • well just one quick thing I noticed that could be causing you problems ..

     

    "~/User/Image.asp?ImagePath=" + evnt.ImagePath

     

    your image page is not image.asp but image.aspx (according to the above code sample).

    also what happens if you go the image link directly? (i.e. copy/paste picture link into the browser .. do you get an exception?)

    Cheers,

    Greg

    Wednesday, March 22, 2006 12:14 AM
  • No sorry it was just because I was trying other stuff.  But that wasn't my problem.  But thanks
    Wednesday, March 22, 2006 12:16 AM
  • and an answer to the question?

     

    Wednesday, March 22, 2006 12:17 AM
  • I get a null reference exception on bytes...  Thank You for the suggestion this will help alot.
    Wednesday, March 22, 2006 12:23 AM
  • Ok I changed it to this and only the one I tried to view gave that exception because, It really wasn't in the db,  but the others don't display the or give the null exception image.

     

    <%@ Page Language="C#" %>

    <%@ Import Namespace="CollisonDatabase" %>

    <%

    Response.Expires = 0;

    Response.Buffer = true;

    Response.Clear();

    string imagePath = Request.QueryString.Get("ImagePath");

    CollisonDatabase.Database db = new CollisonDatabase.Database();

    byte[] bytes = db.GetImage(imagePath);

    if (bytes != null)

    {

    Response.ContentType = "image/jpeg";

    Response.BinaryWrite(bytes);

    Response.End();

    }

    %>

    Wednesday, March 22, 2006 12:41 AM
  • ok ...

    what happens? a red X?

    How are you storing the data in the db? Are you doing anything odd like base63 encoding it that you may not be doing on the way out?

    Wednesday, March 22, 2006 1:06 AM
  •  

    File:AddImage.aspx

    //inside is this code

    //path = "The Path that I want to call it";

    addImage(path, FileUpload1.FileBytes );

     

     

    File: Database.cs

     

    addImage(string imagePath, byte[] imgBytes)

    {

    try

    {

    if (imagePath != "NONE")

    {

    SqlConnection conn = new SqlConnection();

    activateConnection(ref conn);

    SqlCommand cmd = new SqlCommand();

    cmd.CommandType = System.Data.CommandType.StoredProcedure;

    cmd.CommandText = "carterwjeff.addImage";

    cmd.Connection = conn;

    cmd.Parameters.AddWithValue("@imagepath", imagePath);

    cmd.Parameters.AddWithValue("@imgbyte", imgBytes);

    if (Convert.ToInt32(cmd.ExecuteNonQuery()) > 0)

    {

    _result += " Image Successfully Created.";

    return true;

    }

    else

    {

    _result += " Image was not successfully created.";

    return false;

    }

    Wednesday, March 22, 2006 1:12 AM
  • The Column is of SQL:   Image
    Wednesday, March 22, 2006 1:13 AM
  • have you verified sizes etc being correct between the data in the db and the actual files (i.e. that the db based data is actually valid (and they are actually in JPEG format?)

     

     

     

    Wednesday, March 22, 2006 1:21 AM
  • And it is displaying a red x?

    Have you verified that the data in the db is valid data in jpeg form? I have code almost identical to this up and running without issue.

     

    Wednesday, March 22, 2006 1:21 AM
  • Red X displays. what size should I use.  I use Image Size 16 (is this the size I should use,   so far I am the only one to post the jpegs so they are good files.   I put a break point and I notice that it is only saving 1000 bytes of info.  So what should the size be for my image size?  52000 is the size of the jpeg I am trying to save.
    Wednesday, March 22, 2006 1:36 AM
  • I would try the following just to test things out ... write a test function to insert a known good image (i.e. the google image etc) from a file ... then look at the data in the db to make sure it matches up ... if it does then try to display it.

    I am not following the fact that you use "Image Size 16" perhaps you could post the code in question.

    Greg

     

    Wednesday, March 22, 2006 1:46 AM
  • In SQL enterprise manager the settings in my image table at the column named img -->  are set to Image but says the Size of the colummn is 16  --> can this size be changed???
    Wednesday, March 22, 2006 1:49 AM
  • no that size is correct heh ... it still holds a variable amount of data.

    Greg

    Wednesday, March 22, 2006 1:51 AM
  • (I have seen the asp:DynamicImage but when I try to use it, on the build it says it is an unknown server control)

    -> So how far are we in seeing this in the future? This is going to be a great control, but is not included and there is no news of this feature on microsoft.com, I found a replacement at....
    http://msdn.microsoft.com/msdnmag/issues/04/04/CuttingEdge/default.aspx

    if anyone has any news, please let me know

     

    Tuesday, October 10, 2006 8:22 AM