Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I need help locating the error in this code snippet:
byte[] a = objstud.viewtudentphoto(txtadminno.Text);
System.IO.MemoryStream ms = new System.IO.MemoryStream(a);
pictureBox2.Image = Image.FromStream(ms);
 
I am getting the error here:
pictureBox2.Image = Image.FromStream(ms);
 
here objstud.viewtudentphoto(txtadminno.Text) returns an image
from database
 
Any help is greatly appreciated.
 
Thanks.
Posted 11-Mar-11 1:43am
Edited 11-Mar-11 19:21pm
v5
Comments
Slacker007 at 11-Mar-11 7:07am
   
EDITED: readability, code blocks, spelling, and grammar.
Shahriar Iqbal Chowdhury at 11-Mar-11 7:07am
   
please share the error message
anilet v antony at 12-Mar-11 0:18am
   
Parameter is not valid.
at
Image.FromStream(ms)
Tarun.K.S at 11-Mar-11 7:10am
   
Provide the stack trace.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Please have a look at this excellent article. Taking it as a given that your method objstud.viewtudentphoto(String txtImg) does indeed do what you expect of it please take a look at this article on CP: C# Image to Byte Array and Byte Array to Image Converter Class[^].
 
Hope this helps some.
 
Cheers!
  Permalink  
Comments
Espen Harlinn at 17-Mar-11 9:55am
   
Nice link, 5ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Without knowing what objstud.viewtudentphoto does (I assume it loads an image from a file) I can only assume that it returns an image that is badly converted to a byte array.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As Original Griff said it is not sure what is there in the byte array. The byte array has to have all the bytes from the file as such if you haven't re dimension or any other header parameters changed by your code. Not just the pixel values.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You should keep image in the BLOB format. Probably, you do something wrong when you put image into database...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

try this
private Image image(){byte[] a = objstud.viewtudentphoto(txtadminno.Text);System.IO.MemoryStream ms = new System.IO.MemoryStream(a);return Image.FromStream(ms);}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 14 Mar 2011
Copyright © CodeProject, 1999-2014
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