Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
4.00/5 (1 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
Updated 11-Mar-11 18:21pm
v5
Comments
Slacker007 11-Mar-11 7:07am    
EDITED: readability, code blocks, spelling, and grammar.
Shahriar Iqbal Chowdhury/Galib 11-Mar-11 7:07am    
please share the error message
anilet v antony 12-Mar-11 0:18am    
Parameter is not valid.
at
Image.FromStream(ms)
Tarun.K.S 11-Mar-11 7:10am    
Provide the stack trace.

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!
 
Share this answer
 
Comments
Espen Harlinn 17-Mar-11 9:55am    
Nice link, 5ed!
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.
 
Share this answer
 
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.
 
Share this answer
 
You should keep image in the BLOB format. Probably, you do something wrong when you put image into database...
 
Share this answer
 
try this
C++
private Image image(){byte[] a = objstud.viewtudentphoto(txtadminno.Text);System.IO.MemoryStream ms = new System.IO.MemoryStream(a);return Image.FromStream(ms);}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900