Reading binary data from SPROC..help please...

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 6
Reputation: sean_morris9 is an unknown quantity at this point 
Solved Threads: 0
sean_morris9 sean_morris9 is offline Offline
Newbie Poster

Reading binary data from SPROC..help please...

 
0
  #1
Feb 26th, 2008
Hi,

I'm trying to display an image that's stored in a SQL Server database. The ContentType and binary data are being returned but the image is coming up as a broken image. To test that the data is being retrieved I tried outputting the content type.

  1. Response.Write(adm["Image_MIME_type"].ToString());

It is getting the corect data i.e. "image/pjpeg"

Then I tried Response.write for the binary data returned

  1. Response.Write((byte[])adm["Image_image"]);

It is outputting printing "System.Byte[]"

Am I right in saying the data is being returned from the SPROC?

Here is the code...

  1.  
  2. if (adm.HasRows)
  3. {
  4.  
  5. while (adm.Read())
  6. {
  7. // Response.Write(adm["Image_MIME_type"].ToString());
  8.  
  9. Response.ContentType = adm["Image_MIME_type"].ToString();
  10. Response.BinaryWrite((byte[])adm["Image_image"]);
  11. }
  12. }
  13. else
  14. {
  15. Response.Write("Reaer has no rows");
  16.  
  17. }
  18. adm.Close();
  19. conn.Close();
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: sean_morris9 is an unknown quantity at this point 
Solved Threads: 0
sean_morris9 sean_morris9 is offline Offline
Newbie Poster

Re: Reading binary data from SPROC..help please...

 
0
  #2
Feb 26th, 2008
Any help would be great, thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Reading binary data from SPROC..help please...

 
0
  #3
Feb 26th, 2008
Load the image into a byte array.
Pass the byte array into a memory stream
Pass the memory stream into a new or existing bitmap.

  1.  
  2. byte[] b = (byte[])adm["Image_image"];
  3. System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
  4. stream.Write(b, 0, b.Length);
  5.  
  6. Bitmap bmp = new Bitmap(stream);

Error checking, null checking, etc needs to be added, left out for simplicity

Jerry
Last edited by JerryShaw; Feb 26th, 2008 at 10:28 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC