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 ?

ismail baig
  • 861
  • 2
  • 11
  • 39
  • Maybe [this](http://stackoverflow.com/questions/676072/image-fromstream-method-returns-invalid-argument-exception) or [this](http://stackoverflow.com/questions/17667818/error-parameter-is-not-valid-while-converting-bytes-into-image) post can help you. – diiN__________ May 26 '16 at 06: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))` – SpeedOfSpin May 26 '16 at 06:28
  • @SpeedOfSpin: I tried your comment. But it still throws the same error. – ismail baig May 26 '16 at 07:14

1 Answers1

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.
vahid kargar
  • 800
  • 1
  • 9
  • 23
  • 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. – ismail baig May 26 '16 at 07:19
  • 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. – vahid kargar May 26 '16 at 07:26
  • Thanks Vahid.the blob bytes that i was getting from database were corrupted. Now its working fine. I have marked your answere as correct. – ismail baig May 26 '16 at 08:38