1

I am new to ASP & C# and have been unable to figure out how to do this.

I am loading BLOB's from a bd via an .ashx file like so <img src="getimage.ashx" /> and it works fine, but sometimes there is no BLOB or it is empty.

here is the basic code

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("@EmpID", id);

        con.Open();
        byte[] pict = (byte[])cmd.ExecuteScalar();
        con.Close();

        ctx.Response.ContentType = "image/bmp";
        ctx.Response.OutputStream.Write(pict, 0, pict.Length);

My thought is to check pict.Length right after con.Close() and if it fails, I want to display a default image, or even text.

Is that possible? How?

Chad
  • 1,139
  • 17
  • 41
  • I came accross this article, but I don't follow http://www.nullskull.com/a/263/aspnet-write-image-to-responseoutputstream.aspx – Chad Oct 18 '17 at 18:02
  • I am trying to implement this https://stackoverflow.com/a/2070493/3790921 but can not figure out how to "stream" it – Chad Oct 18 '17 at 19:01

2 Answers2

0

After a lot more searching and a lot of 'HttpCompileException(s)' I got this working.

Thanks to @Kazar for this answer here https://stackoverflow.com/a/2070493/3790921 and @Pranay Rana for this answer here https://stackoverflow.com/a/3801289/3790921

I put this together...

        con.Open();
        byte[] pict = (byte[])cmd.ExecuteScalar();
        con.Close();

        ctx.Response.ContentType = "image/bmp";
        if (pict.Length <= 1) {
            // the BLOB is not a picture
            byte[] txt = ImageToByteArray(DrawText("no image found"));
            ctx.Response.OutputStream.Write(txt, 0, txt.Length);
        } else {
            // stream the picture data from BLOB
            ctx.Response.OutputStream.Write(pict, 0, pict.Length);
        }

and it is working.

Chad
  • 1,139
  • 17
  • 41
  • The bonus of this approach is that you could output and error message, or change the text/pic on the fly. – Chad Oct 19 '17 at 13:08
0

If you want to load an image from the disk when none is found in the DB, use this snippet.

public void ProcessRequest(HttpContext context)
{
    //create a new byte array
    byte[] pict = new byte[0];

    //create a connection to the db and a command
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString))
    using (SqlCommand command = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", connection))
    {
        //set the proper command type
        command.CommandType = CommandType.Text;

        //replace the parameters
        command.Parameters.Add("@EmpID", SqlDbType.Int).Value = id;

        try
        {
            //open the db and execute the sql string
            connection.Open();
            pict = (byte[])command.ExecuteScalar();
        }
        catch (Exception ex)
        {
            //catch any errors like unable to open db or errors in command. view with ex.Message
        }
    }

    //if no image found in database load the default from disk
    if (pict == null || pict.Length == 0)
    {
        pict = File.ReadAllBytes(context.Server.MapPath("/noimage.bmp"));
    }

    //clear the buffer stream
    context.Response.ClearHeaders();
    context.Response.Clear();
    context.Response.Buffer = true;

    //set the correct ContentType
    context.Response.ContentType = "image/bmp";

    //set the filename for the image
    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"ImageName.bmp\"");

    //set the correct length of the string being send
    context.Response.AddHeader("content-Length", pict.Length.ToString());

    //send the byte array to the browser
    context.Response.OutputStream.Write(pict, 0, pict.Length);

    //cleanup
    context.Response.Flush();
    context.ApplicationInstance.CompleteRequest();
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Thank You! this line here `pict = File.ReadAllBytes(context.Server.MapPath("/noimage.bmp"));` was exactly what I needed. – Chad Oct 19 '17 at 13:06