bitmap to byte[] (C#, Images, Streams, SQL Server)

Results 1 to 2 of 2

Thread: bitmap to byte[] (C#, Images, Streams, SQL Server)

  1. #1
    Join Date
    Dec 1969

    Default bitmap to byte[] (C#, Images, Streams, SQL Server)

    Hello,<BR>I need to get a byte[] out of an image (jpeg) file chosen by the client through &#060;input type=file&#062;. I then store this byte[] in SQL Server&#039;s image datafield for later retrieval. If I get the byte[] in the following way, it works fine:<BR><BR><BR>code:-----------------------------------------------------------<BR>//HIF is an instance of HtmlInputFile<BR>//blob is a byte[]<BR>System.IO.Stream stream = HIF.PostedFile.InputStream;<BR>blob = new byte[stream.Length];<BR>stream.Read( blob, 0, (int) stream.Length );<BR>stream.Close();<BR>----------------------------------------------------------------<BR><BR>However, I need to modify the image a little before putting it into the byte[]. For that I&#039;ve tried to create a System.Drawing.Image, modify it, and then put it into the byte[] for uploading to SQL Server. Here is the code:<BR><BR>code:-----------------------------------------------------------<BR>System.Drawing.Image bmp = System.Drawing.Image.FromStream( HIF.PostedFile.InputStream );<BR>//Modify bmp<BR>System.IO.MemoryStream stream = new System.IO.MemoryStream();<BR>bmp.Save( stream, System.Drawing.Imaging.ImageFormat.Jpeg );<BR>blob = new byte[ stream.Length ];<BR>stream.Read( blob, 0, (int)stream.Length );<BR>stream.Close();<BR>----------------------------------------------------------------<BR><BR>The problem is that when I do that (even without modifying the image), the image data gets somehow corrupted and does not show up in the browser when retrieved from SQL Server.<BR><BR>It would be great if someone could explain me why that is happening and how to fix that.<BR><BR>Thanks,<BR>Ilya K.

  2. #2
    Join Date
    Dec 1969

    Default Here

    I believe these contain the items you need:<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts