Click here to Skip to main content
13,095,921 members (53,494 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm using WriteablleBitmap in Silverlight to insert my image into database.
Converting wrtieablebitmap to byte[]:
public byte[] ImageToByteArray(WriteableBitmap bitmap)
            int[] p = bitmap.Pixels;
            int len = p.Length << 2;
            byte[] result = new byte[len];
            Buffer.BlockCopy(p, 0, result, 0, len);
            return result;

Then, using Silverlight WCF Service, I'm inserting it to database:
        public void DoWork(byte[] img, string user, int id, double cen)
            DataClasses1DataContext db = new DataClasses1DataContext();
            produkty p = new produkty
                zdjecie = img,
                userName = user,
                id_kategorii = id,
                cena = cen,
                opis = "opis"
            try { db.SubmitChanges(); }
            catch (Exception e) { Console.WriteLine(e);  }
            return ;

It should be corect becaouse i can find inserted data in my database.
Next, I'm using generic handler to display image from database in default.aspx in control - Image, but it isn't working.

Handler source code:
string id = ctx.Request.QueryString["id"];
string conn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT zdjecie FROM produkty WHERE id_produktu=@id", con);
cmd.CommandType = CommandType.Text;
//cmd.Parameters.Add("@id", id);
cmd.Parameters.AddWithValue("@id", id);
byte[] pict = (byte[])cmd.ExecuteScalar();
ctx.Response.ContentType = "image/bmp";
ctx.Response.OutputStream.Write(pict, 78, pict.Length - 78);

Table: produkty
id_produktu -> int
userName-> nvarchar(256)
What am I doing wrong? Does it necessary any convertion or something?
Sorry for my English.
Posted 4-Dec-12 3:11am
Updated 4-Dec-12 3:16am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to change your byte array back into a Stream, if you dont want to save it to the hard disk then just use a memory stream.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100