Reading Images From Sql Server 2005 and Displaying it on Web Page RRS feed

  • Question

  • help me out here guyz////
    the code looks fine and it runs too but no image appears..

    Code Snippet            try
                    SqlConnection con = new SqlConnection("server=asd;database=ashutosh;user id=sa;password=manshu");
                    SqlCommand cmd = new SqlCommand("select i from hello where full_name='ashutosh'",con);
                    byte[] barrImg=(byte[])cmd.ExecuteScalar();
                    string strfn=Convert.ToString(DateTime.Now.ToFileTime());
                    FileStream fs=new FileStream(strfn,
                    FileMode.CreateNew, FileAccess.Write);
                    Image1.ImageUrl = strfn;
                catch(Exception ea)
                    Response.Write("Read Failed : " + ea.ToString());
    Monday, July 2, 2007 11:02 AM

All replies

  • try this



    Code Snippet
        MemoryStream stream = new MemoryStream ();
        SqlConnection con = new SqlConnection("server=asd;database=ashutosh;user id=sa;password=manshu");
                    SqlCommand cmd = new SqlCommand("select i from hello where full_name='ashutosh'",con);
            byte[] image = (byte[]) command.ExecuteScalar ();  
            stream.Write (image, 0, image.Length);
            Bitmap bitmap = new Bitmap (stream);
            Response.ContentType = "image/gif";
            bitmap.Save (Response.OutputStream, ImageFormat.Gif);
            connection.Close ();
            stream.Close ();


    Monday, July 2, 2007 11:12 AM
  • Invalid parameter used.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: Invalid parameter used.

    Source Error:

    Line 62: 				byte[] image = (byte[]) cmd.ExecuteScalar ();  
    Line 63: stream.Write (image, 0, image.Length);
    Line 64: Bitmap bitmap = new Bitmap (stream);
    Line 65: Response.ContentType = "image/gif";
    Line 66: bitmap.Save (Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
    Monday, July 2, 2007 11:25 AM
  • guyz i need help its urgent..
    Tuesday, July 3, 2007 3:11 PM
  • can anyone post any code that is working... ?
    Sunday, July 8, 2007 2:38 PM
  • Are you trying to load an image from SQL into a UI Control or are you trying to make it a downloadable file for the user, so when you are writing the image to the output stream it would come up as a downloadable prompt?



    Sunday, July 8, 2007 10:12 PM
  • i am reading images from sql and i want to display it in a UI control ....
    Monday, July 9, 2007 1:28 AM
  • help me dude... u have done it before.
    Monday, July 9, 2007 1:31 PM
  • It looks like you have a web page and have placed an control on it.  Then in the code behind you are attempting to "load up" the image by setting the imageurl property to a byte array.


    I do not belive that you can do that.  You can do something similar on a windows form Picturebox, but I don't think you can do it with a web form.


    If you want to accomplish something like this via a web form you need two pieces.  A control/page/handler to "emit" an image and your web page that will reference the first thing as if it was a url.


    Let's start with a handler that will emit an image.



    Code Snippet

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


    using System;

    using System.Data;

    using System.Data.SqlClient;

    using System.Drawing;

    using System.Drawing.Imaging;

    using System.IO;

    using System.Web;


    public class ImageFetcher : IHttpHandler {


        private string myConnectString


            get{ return "server=asd;database=ashutosh;user id=sa;password=manshu"; }



        private string mySQLString


            get { return "select i from hello where full_name='ashutosh'"; }



        public void ProcessRequest(HttpContext context) {

            MemoryStream myByteStream = new MemoryStream();

            using (SqlConnection myConnection = new SqlConnection(myConnectString))


                using (SqlCommand myCommand = new SqlCommand())


                    myCommand.Connection = myConnection;

                    myCommand.CommandType = CommandType.Text;

                    myCommand.CommandText = mySQLString;


                    byte[] myImageBytes = (byte[])myCommand.ExecuteScalar();

                    myByteStream = new MemoryStream(myImageBytes);



            myByteStream.Position = 0;

            Bitmap myImageBitmap = new Bitmap(myByteStream);

            context.Response.ContentType = "image/gif";

            myImageBitmap.Save(context.Response.OutputStream, ImageFormat.Gif);



        public bool IsReusable {

            get { return false; }




    Now, you can reference this handler as if it was an image from your web page or code behind.


    Code Snippet

    protected void Page_Load(object sender, EventArgs e)


        Image1.ImageUrl = "ImageFetcher.ashx";




    Monday, July 9, 2007 3:40 PM
  • Try this code probably it should work


    Code Snippet

    Bitmap bitmap = (Bitmap)System.Drawing.Bitmap.FromStream(stream);
     Response.ContentType = "image/bmp";
     bitmap.Save (Response.OutputStream, System.Drawing.Imaging.ImageFormat.bmp);



    Tuesday, July 10, 2007 11:12 AM
  • jgalley your code does n't work..

    i have to idea what ur trying to do...
    Wednesday, July 11, 2007 12:50 PM
  • Line 65: byte[] image = (byte[]) cmd.ExecuteScalar (); Line 66: stream.Write (image, 0, image.Length); Line 67: Bitmap bitmap = (Bitmap)System.Drawing.Bitmap.FromStream(stream); Line 68: Response.ContentType = "image/jpeg"; Line 69: bitmap.Save (Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    Wednesday, July 11, 2007 12:55 PM
  • What error do you get using my code?
    Wednesday, July 11, 2007 2:49 PM
  • you are never using the process.... function u made.
    you are not passing any reference to HttpContext//

    does't make sense at all wht ur trying to do...
    Wednesday, July 11, 2007 5:36 PM
  • The first block of code is the code for an HttpHandler.


    In Visual Studio add a new file of type "Generic Handler".. Call it:  ImageFetcher.ashx

    Copy my code block in overtop of the default generated code.


    The second code block contains a snippet of how to use the handler in your code behind to set the url of your image to the handler and thus resulting in the image on your web page being rendered from an image stored in your database.


    An even simpler use example would be:


    Code Snippet



            <img src="ImageFetcher.ashx" />




    Wednesday, July 11, 2007 6:02 PM
  • Did that explaination help?

    Do you usderstand my example and what it does for you?

    Friday, July 13, 2007 7:04 PM
  • I have no idea what your saying jalley !!Can anyone help me as to how anyone can perform this ?
    Sunday, October 14, 2007 6:35 PM
  • What part of the example I sent are you having problems with?


    Do you believe the code I have given is in error?  If so, where and what error message are you getting.  Alternatively, if you don't understand the example, can you give me an idea of where you are having problems understanding it.


    Monday, October 15, 2007 3:23 PM