Click here to Skip to main content
16,017,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a website and trying to retrieve an image from database stored in binary format but i found an error on the line
Bitmap b = (Bitmap)tc.ConvertFrom(bmp);
that parameter is not valid.
the code for ShowImage.ashx handler is below. Can anyone help me to solve this issue

Thanks in advance
C#
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Configuration;

namespace IDRC
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ShowImage : IHttpHandler
    {
        SqlConnection conn;
        byte [] pic = null;
        long Seq = 0;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string userid;
            if (context.Request.QueryString["userid"] != null)
            {
                userid = context.Request.QueryString["userid"];
            }
            else
            {
                throw new ArgumentException("No parameter specified");
            }

            // Convert Byte[] to Bitmap
            Bitmap newBmp = ConvertToBitmap(Show_Image(userid));
            if (newBmp != null)
            {
                newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                newBmp.Dispose();
            }
        }

        protected Bitmap ConvertToBitmap(byte []bmp)
        {
            if (bmp != null)
            {
                TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
                //ImageConverter img = new ImageConverter();
                Bitmap b = (Bitmap)tc.ConvertFrom(bmp);
                return b;
            }
            return null;
        }

        public byte[] Show_Image(string userid)//, string query)
        {
            conn = new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Telehealth_Data.MDF;Integrated Security=True;User Instance=True");
            string query = @"SELECT [staff_pic] FROM [staff_personal_info] WHERE [staff_member_id] = @userid ";
            SqlCommand cmd = new SqlCommand(query ,conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@userid", userid);
            try
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Seq = dr.GetBytes(0, 0, null, 0, int.MaxValue) -1;
                    pic = new byte[Seq + 1];
                    dr.GetBytes(0, 0, pic, 0, Convert.ToInt32(Seq));
                    conn.Close();
                }
                return pic;
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
Posted
Updated 20-Jul-10 19:40pm
v2
Comments
Sandeep Mewara 21-Jul-10 1:41am    
Use PRE tags to format code part. It makes the question readable.

1 solution

Goodness knows what this code is, but I always stuff the data into a memory stream and use that to initialise the bitmap, using Bitmap.FromStream.
 
Share this answer
 
Comments
saqib666 28-Jul-10 0:04am    
thanks for answering but i have also done this but again i get the same error when i pass memory streme as an argument
MemoryStreme ms = new MemoryStreme(bmp);
Bitmap b = new Bitmap(ms);
return b;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900