locked
HttpHandler as image provider: Images are not shown! RRS feed

  • Question

  • User260076833 posted

    Hi,

    I have a HttpHandler that should serve images from a SQL database. It serves the images as binary streams, but the images are not shown. Only a placeholder is shown.

    Here is my handler:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace myProject.mix
    {
        /// <summary>
        /// Summary description for Provider1
        /// </summary>
        public class Provider1 : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext ctx)
            {
                Int32 raum, idx;
    
                if (ctx.Request.QueryString["raum"] != null)
                    raum = Convert.ToInt32(ctx.Request.QueryString["raum"]);
                else
                    throw new ArgumentException("No parameter specified");
    
                if (ctx.Request.QueryString["idx"] != null)
                    idx = Convert.ToInt32(ctx.Request.QueryString["idx"]);
                else
                    throw new ArgumentException("No parameter specified");
    
                ctx.Response.ContentType = "image/jpeg";
                Stream strm = showImage(raum, idx);
                byte[] buffer = new byte[4096];
                int byteSeq = strm.Read(buffer, 0, 4096);
    
                while (byteSeq > 0)
                {
                    ctx.Response.OutputStream.Write(buffer, 0, byteSeq);
                    byteSeq = strm.Read(buffer, 0, 4096);
                }
                //context.Response.BinaryWrite(buffer);
            }
    
            public Stream showImage(int raum, int idx)
            {
                string cns = ConfigurationManager.ConnectionStrings["StMGP"].ConnectionString;
                SqlConnection con = new SqlConnection(cns);
                string sql = "SELECT bild FROM rmv_raum_bild WHERE (raum=@raum AND idx=@idx);";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@raum", raum);
                cmd.Parameters.AddWithValue("@idx", idx);
                con.Open();
                object img = cmd.ExecuteScalar();
                try
                {
                    return new MemoryStream((byte[])img);
                }
                catch
                {
                    return null;
                }
                finally
                {
                    con.Close();
                }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    When I call him directly, no images are shown, only placeholder symbols:

    http://localhost:56820/mix/Provider.ashx?raum=352&idx=4

    What's wrong here?

    Is there another method for displaying images from a SQL server?

    Thanks
    Magnus

    Tuesday, August 4, 2015 12:15 PM

Answers

  • User260076833 posted

    I found it! The code was correct, but the image in the database was corrupt.

    I connected the table with ODBC to an Access form with an OLE control.

    Using this I can easily copy an image from somewhere (e. g. web browser) and paste image into the OLE control.

    This worked within Acces, i. e. I can store and also retrieve the images. But they were not shown by my code in ASP.NET.

    Could I modify my Access form somehow that it works?

    Thanks
    Magnus

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 6, 2015 5:43 AM