Answered by:
Image to Byte[] --> sql --> Byte[] to Image

Question
-
User705659023 posted
Hi,
I am trying to create a simple Add control where the user can select an image to upload. When the use clicks 'Submit' the image is to be converted to Byte[] and stored in SQL database column with type 'image'. I believe that this is being processed correctly, however when I try to retrieve the image back from the database I am unable to convert the data to the image.
The code that I am using to convert the image to byte[] is
Stream inStream = fileUpload.PostedFile.InputStream; System.Drawing.Image img = System.Drawing.Image.FromStream(inStream); MemoryStream ms = new MemoryStream(); Byte[] imageBytes = null; EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, 85L); ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders()[1]; EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; resizedImg.Save(ms, jpegCodec, encoderParams); imageBytes = ms.ToArray();
When retrieving the image I use
MemoryStream msRead = new MemoryStream((byte[])myReader["imgData"]); System.Drawing.Image imgRead = System.Drawing.Image.FromStream(msRead);
But when I run this code I get the following exceptionSystem.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) at Add.Page_Load(Object sender, EventArgs e) in ...\Add.aspx.cs:line 43
Am I doing something wrong?
Monday, October 5, 2009 1:56 PM
Answers
-
User-1171043462 posted
try this
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 6, 2009 1:23 PM
All replies
-
User-999007726 posted
Why don't you try instead....
MemoryStream msRead = new MemoryStream; msRead.Write((byte[])myReader["imgData"],0,(byte[])myReader["imgData"].Length ); System.Drawing.Image imgRead = System.Drawing.Image.FromStream(msRead);
This should actually work...Cheers,
AJMonday, October 5, 2009 2:08 PM -
User1992938117 posted
hi,
DamounH
but where is Line 43 ?
try to do like this..
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129&AspxAutoDetectCookieSupport=1
http://www.beansoftware.com/ASP.NET-Tutorials/Save-Read-Image-Database.aspx
Thanks..
Monday, October 5, 2009 2:10 PM -
User705659023 posted
OOO good point
line 43 is the 3rd line in the second code block, where I try to read image from stream.
sorry about that
Monday, October 5, 2009 4:32 PM -
User705659023 posted
I will try both of these solutions, but I feel like I tried the ariejones suggested before but had the same issue.
Ill try it again as soon as I get home, thank you for your suggestions
Monday, October 5, 2009 4:33 PM -
User705659023 posted
BEAUTIFUL!
I tried the sample code in the links and it worked, I think I was making a big mistake when converting to image to byte[], I had to change a few things. Thank you very much for your help
Tuesday, October 6, 2009 10:21 AM -
User705659023 posted
hmm, what if I want to get the image from the database and bind it to a gridview?
the main purpose of doing this is to create a cardview and show the entries along with the picture
Tuesday, October 6, 2009 1:13 PM -
User-1171043462 posted
try this
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 6, 2009 1:23 PM