2

so i have this method:

    string sql = string.Format("EXECUTE getPlayerImage @playerID = '{0}'", bio.playerID);


    SqlCommand cm = baseballConnection.CreateCommand();

    baseballConnection.Open();
    cm.CommandText = sql;
    baseballConnection.Close();
    return cm.ExecuteScalar() as byte[];

and it works, at least i get no errors. but now all i have is a byte array, how can i display this on the screen? i have an image tool on the screen but the only way i know how to display an image is if its saved on the server and has a file path. i wouldnt know how to save the byte array to the server, but that would be cheesing it and there would be no point to my already saving the image in the db as a varbinary and now retrieving it.

how can i get this byte array to display?

2
  • I'd rather have the server transmit the file from the hard drive than have it fire up the bloated asp.net lifecycle, hit the database, pass the data through memory, then transmit the file. Commented Jun 5, 2013 at 18:33
  • i understand. i just wanted to see if i could do this. ive come so far, it is easier, i know, but nonetheless, id like to see this succeed Commented Jun 5, 2013 at 18:35

2 Answers 2

2

To render the image in asp.net, you have to create an asp.net page or generic handler to save the bytes in the output response stream.

You would then set the an HTML image or control to link to the page or generic handler.

For instance,

<img src="getimage.aspx?id=1" style="width:100px; height:100px" alt="" />

So in your case it would be something like this:

using System.Web;

namespace ImageUtil
{
    public class ImageProvider : IHttpHandler
    {
        publicvoid ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";

            string sql = string.Format("EXECUTE getPlayerImage @playerID = '{0}'", bio.playerID);

            SqlCommand cm = baseballConnection.CreateCommand();
            baseballConnection.Open();
            cm.CommandText = sql;
            byte[] img = (byte[])cm.ExecuteScalar();
            baseballConnection.Close();

            context.Response.BinaryWrite(img);
        }

        publicbool IsReusable
        {
            get
            {
                returnfalse;
            }
        }
    }
}

Here is a link: http://samiradel.wordpress.com/2011/08/03/how-to-display-image-byte-array-in-a-an-img-tag/

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried Response.BinaryWrite?

http://msdn.microsoft.com/en-us/library/ms524318%28v=VS.90%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.