locked
Problem returning a image RRS feed

  • Question

  • User2034657745 posted

    How can I make the handler return a no picture.

    Question: If there is no picture and it is a Fullsize then return
    a image nopicture.

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

    using System;
    using System.Web;
    using System.Data.SqlClient;  //Add this namespace
    using System.Configuration; // Add this namespace
    using System.Data; //Add this namespace
    using System.IO; //Add this namespace

    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string MyImageSize;
         
            if (context.Request.QueryString["imagesize"] != null)
                MyImageSize = context.Request.QueryString["imagesize"];
            else
                throw new ArgumentException("No imagesize parameter specified");


            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["dbaspnetConnectionString"].ConnectionString;

            // Create SQL Command

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select img_title, img_stream, img_type from tbl_Images where img_id =@img_id";
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;

            SqlParameter ImageID = new SqlParameter("@img_id", System.Data.SqlDbType.Int);
            if (!string.IsNullOrEmpty(context.Request.QueryString["img_id"]))
            {
                ImageID.Value = context.Request.QueryString["img_id"];
                cmd.Parameters.Add(ImageID);
                con.Open();
                SqlDataReader dReader = cmd.ExecuteReader();
                dReader.Read();
               
                switch (MyImageSize)
                {
                    case "F":
                        // Full-size image
    //----------->this is whereI need help on no sure how this if statement should be?
    --->                    if ((byte[])dReader["img_stream"] == null)
                        {
                            byte[] image = Image.FromFile(context.Server.MapPath("~/Images/NoPhoto.jpg"));
                            image.Save(context.Response.OutputStream, dReader["img_type"].ToString());
                            return;
                        }
                       
                        context.Response.ContentType = dReader["img_type"].ToString();
                        context.Response.BinaryWrite((byte[])dReader["img_stream"]);
                        break;
                    case "T":
                        // Thumbnail-size image

                        context.Response.ContentType = dReader["img_type"].ToString();
                        context.Response.BinaryWrite(OmegaLove.Web.ImageResize.CreateThumbnail((byte[])dReader["img_stream"], 120));

                        break;

                    default:
                        throw new ArgumentException("Invalid imagesize parameter specified");

                }


                dReader.Close();
                con.Close();
            }
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
     
       

    }
    Monday, November 16, 2009 2:51 PM

Answers

  • User-821857111 posted

    Try changing to:

    if (dReader["img_stream"] == DBNull.Value)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 16, 2009 5:18 PM