Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET Silverlight
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 4:11am
Edited 4-Dec-12 4: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
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342

Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
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