locked
return an image from a web service RRS feed

  • Question

  • i have a sql 2005 table that has an image datatype as one of it's column. Can anyone give some pointers on retrieve the image from the database using a web service.
    So basically it will be 
    client consuming the web service --> Web Service --> Database call using stored procedure --> sql 2005 db

    Thanks in advance
    wantsrs
    Thursday, June 4, 2009 9:44 PM

Answers

  • This is roughly what you would need:

    private string SerializeImage(Bitmap bitmap)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Jpeg);
    
            return Convert.ToBase64String(stream.GetBuffer());
        }
    }
    
    private Bitmap DeserializeImage (string base64Data)
    {
        using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Data)))
        {
            Bitmap bitmap = new Bitmap(stream);
    
            return bitmap;
        }
    }

    Hope it helps!
    Keep it Sharp
    Friday, June 5, 2009 12:29 AM
  • Hi Kirang,

    I upload the sample works in my side to my sky drive. You can download it here,
    http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/ReturnImageFromWS.zip

    It includes the Webservice application and Windows Form application, as well as the test database I use in my side. Please let me know if this works for you. :-)


    Regards,
    Ji Zhou
    Wednesday, June 10, 2009 6:27 AM
    Moderator

All replies

  • From the top of my head, I would say that serializing to base64 is an option. Although there might be better solutions that I'm not aware of.
    Keep it Sharp
    Friday, June 5, 2009 12:16 AM
  • This is roughly what you would need:

    private string SerializeImage(Bitmap bitmap)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Jpeg);
    
            return Convert.ToBase64String(stream.GetBuffer());
        }
    }
    
    private Bitmap DeserializeImage (string base64Data)
    {
        using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Data)))
        {
            Bitmap bitmap = new Bitmap(stream);
    
            return bitmap;
        }
    }

    Hope it helps!
    Keep it Sharp
    Friday, June 5, 2009 12:29 AM
  • Hi Kirang,

    When we inquire a image coloumn from the SQL server, it returns a byte[] object. I think we can just return the byte[] object in our WebService, and in the client we produce the Image from the byte[] object. I test the following codes and they work fine for me.

    -------Web Service-------------------
            [WebMethod]
            public byte[] returnTestImage()
            {
                SqlConnection con = new SqlConnection(@"Data Source=.\;Initial Catalog=testdb;Integrated Security=True");
                con.Open();
                SqlCommand com = new SqlCommand("select ImageCol from Images where IDCol = 1",con);
                return (byte[])com.ExecuteScalar();
            }
    -------Windows Form client application---------
            private void Form1_Load(object sender, EventArgs e)
            {
                MemoryStream mem = new MemoryStream(new ServiceReference1.Service1SoapClient().returnTestImage());
                this.pictureBox1.Image = Image.FromStream(mem);
            }
    Please let us to know if this addresses your question and concern.


    Best regards,
    Ji Zhou
    Friday, June 5, 2009 10:18 AM
    Moderator
  • Hi Kirang,

    Does my code work for you? How is the status of the issue in your side?


    Regards,
    Ji Zhou
    Tuesday, June 9, 2009 3:51 AM
    Moderator
  • Hi Ji - Did not work. I was getting a null in the client application. I am still looking what might be the issue if it was working for you.
    wantsrs
    Wednesday, June 10, 2009 4:34 AM
  • Hi Kirang,

    I upload the sample works in my side to my sky drive. You can download it here,
    http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/ReturnImageFromWS.zip

    It includes the Webservice application and Windows Form application, as well as the test database I use in my side. Please let me know if this works for you. :-)


    Regards,
    Ji Zhou
    Wednesday, June 10, 2009 6:27 AM
    Moderator