Feed Icon  


  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics


Open Source Projects



More Fun with Deserializing

Posted in Sql and Xml at Tuesday, November 18, 2003 5:50 AM Pacific Standard Time

Ok so I've gotten some feedback on my deserializing post, but I also received a suggestion. It was suggested that instead of making the separate SQL call to load the image I should just encode the image as Base64 data and return it with the XML (this can be done using "for xml auto, binary base64").

So it was back to my Sql2Class application to add in the photo field from the Northwind database. So I added a PictureBox to my form and a Photo property of type Byte[] to my Employee class.

When I tried loading the Byte[] into the PictureBox using Image.LoadFromStream it failed and gave me an error about invalid data. I found this kb article about the photo column in the employee database not being directly mappable to the Image type in .Net because of a header on the file. Ok, I've worked with headers before, so why not just remove it. By using a for loop I was able to figure out that if you skip the first 78 bytes of the data in the photo column then you will get a valid image.

Ok so now I have a valid image which I could load and display on my form. Next I added another button to load the image using Base64 encoding instead of my previous method. This was done by simply changing the “for xml” part of the query to “for xml auto, binary base64”. Next I had to modify my employee class to accept the encoded photo image which I did as follows:



/// Property EncodedPhoto (String)





public String EncodedPhoto



get {return Convert.ToBase64String(photo);}


set {photo = Convert.FromBase64String(value);}


This will allow the serializer to set the value of this field since I have aliased it in my for xml auto query as EncodedPhoto.

Now I'm ready to run my tests to see what kind of performance I'll be getting. Below is a screenshot of my test appliation.



Well as you can see, there is a pretty big performance hit when you encode the bits using Base64 in SQL Server. Granted, you have a cleaner solution (which can also be exported outside the bounds of your application).

Comments are closed.