Click here to Skip to main content
12,508,185 members (55,474 online)
Rate this:
 
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:
[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:
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 4-Dec-12 3:11am
Updated 4-Dec-12 3:16am
v2

1 solution

Rate this: bad
 
good
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.
  Permalink  

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 | Mobile
Web02 | 2.8.160927.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2016
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