Click here to Skip to main content
15,881,684 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm using WriteablleBitmap in Silverlight to insert my image into database.
Converting wrtieablebitmap to byte[]:
C#
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:
C#
[OperationContract]
        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"
            };
            db.produkties.InsertOnSubmit(p);
            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:
C#
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);
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
ctx.Response.OutputStream.Write(pict, 78, pict.Length - 78);

Database:
Table: produkty
id_produktu -> int
zdjecie->varbinary(max)
userName-> nvarchar(256)
id_kategorii->int
cena->float
opis->nvarchar(150)
What am I doing wrong? Does it necessary any convertion or something?
Sorry for my English.
Posted
Updated 4-Dec-12 3:16am
v2

1 solution

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.
 
Share this answer
 

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