0

i could see this question is asked many many times but none seems to be straight forward answer. so am posting this question.

I am reading BLOB from oracle database into photoByteArray[] array. Now i just want to save this byte[] into file systems as anyFileName.jpeg (or any format), but i get the "Parameter not vaid" exception.

What i tried is

using (var ms = new System.IO.MemoryStream(photoByteArray))
{
  using (var img = Image.FromStream(ms)) // error thrown here as 'parameter is not valid'
    {
      img.Save("D:\\anyFileName.jpg", ImageFormat.Jpeg); 
     }
 }

My bytes

Byte image

Few are suggesting that some header gets added in the byte array, but how and howmuch to remove that kind of header is not straight forward way.

What am i doing wrong ?

3
  • Maybe this or this post can help you. Commented May 26, 2016 at 6:22
  • 1
    The error just means that the byte array you are passing into it is possibly not valid .Try turning off validation and see if that works. IE using (var img = Image.FromStream(ms, false, true)) Commented May 26, 2016 at 6:28
  • @SpeedOfSpin: I tried your comment. But it still throws the same error. Commented May 26, 2016 at 7:14

1 Answer 1

4

When I use something like this:

Image img = Image.FromFile(@"C:\a.png");
            byte[] arr;
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                arr = ms.ToArray();
            }
            using (var ms = new System.IO.MemoryStream(arr))
            {
                using (var img1 = Image.FromStream(ms,false,true)) // error thrown here as 'parameter is not valid'
                {
                    img1.Save("D:\\anyFileName.png", ImageFormat.Png);
                }
            }

which convert an image file to byte and with your method convert that byte array to image it works properly.

but when I change some byte of that byteArray to 0 (zero)(for example first 15 bytes) I get your error.

so It is concluded that your byte array really isn't in correct format, some probabilities are:

  1. you are saving your byte array to your database with mistakes(for
    example datatype of your column is not appropriate).
  2. while getting byte array from DB you make some mistakes.
  3. some other reasons which I don't know.
Sign up to request clarification or add additional context in comments.

3 Comments

Actually few suggest some header gets added while getting BLOB from DB, i just wanted to know how to get rid of these headers (if that is the case). Anyhow thanks for esponse.
dear @ismailbaig, as you said, some headers maybe added unexpectedly, you should find it, with these information which you get to us, we can help you this much.
Thanks Vahid.the blob bytes that i was getting from database were corrupted. Now its working fine. I have marked your answere as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.