16

I am converting bytes into an image but I get an error

Parameter is not valid

I am pasting my code. Kindly check the code and suggested that was I am doing right or wrong.

Image arr1 = byteArrayToImage(Bytess);

This is the function.

public static Image byteArrayToImage(byte[] byteArrayIn)
{
        if (null == byteArrayIn || byteArrayIn.Length == 0)
            return null;

        MemoryStream ms = new MemoryStream(byteArrayIn);
        try
          {
            Process currentProcess1 = Process.GetCurrentProcess();
            Image returnImage = Image.FromStream(ms);
            return returnImage;
          }
        catch (Exception ex)
          {
            MessageBox.Show(ex.Message);
          }
    }

I applied many techniques and solutions but it did not work for me

Your answer would be appreciated.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Umair Aslam
  • 207
  • 1
  • 6
  • 12
  • I edited It. oOption.SelectedFile has a bytes – Umair Aslam Jul 16 '13 at 04:09
  • 1
    I saw that... deleted my original comment... So what line throws the error exactly? Also, the currentProcess1 doesn't appear to be used for anything (as a side note). – Jason Down Jul 16 '13 at 04:10
  • Image returnImage = Image.FromStream(ms); this lines gives an error that paramter is not valid – Umair Aslam Jul 16 '13 at 04:11
  • The byte array is likely not a valid image (cannot be converted so the Image.FromStream is failing). – Jason Down Jul 16 '13 at 04:14
  • ohh Yes you are right . – Umair Aslam Jul 16 '13 at 04:15
  • this is not a valid image , because when I try to convert jpeg byte to image it successfully run but when I try to convert PDF bytes than it will give error of Parameter not Valid – Umair Aslam Jul 16 '13 at 04:16
  • 2
    Yes, it must be a recognized image format for Image.FromStream to work. If you are trying to convert a pdf you'll have to do it another way. Take a look at this question: http://stackoverflow.com/questions/6712557/image-fromstream-parameter-not-valid – Jason Down Jul 16 '13 at 04:19

6 Answers6

12

try this

public Image byteArrayToImage(byte[] byteArrayIn)
{
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
    Image img = (Image)converter.ConvertFrom(byteArrayIn);

    return img;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
sangram parmar
  • 8,462
  • 2
  • 23
  • 47
  • 2
    No , It doesn`t Work . my bytes are 1120135 – Umair Aslam Jul 16 '13 at 04:43
  • like this byte[] array = { 68, 111, 116, 32, 78, 101, 116, 32, 80, 101, 114, 108, 115 }; – sangram parmar Jul 16 '13 at 04:59
  • 1
    Actually my byte are generated from dialog Box , so how can i write here , first i select image from dialog box and than pass that image bytes into this method – Umair Aslam Jul 16 '13 at 05:03
  • 1
    Omg. This saved me. I had already spent a couple hours with the old `parameter is not valid` exception. I was using the `Image.FromStream` method: http://stackoverflow.com/questions/457370/c-how-to-convert-bitmap-byte-array-to-jpeg-format – pookie Jul 28 '16 at 17:46
7

After trying many things I found a way which has a little bit more control. In this example you can specify the pixel format and copy the bytes to a Bitmap.

byte[] buffer = GetImageBytes();
var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length);
bitmap.UnlockBits(bitmap_data);
var result = bitmap as Image;
Ertyui
  • 858
  • 9
  • 18
-1
cmd.CommandText="SELECT * FROM `form_backimg` WHERE ACTIVE=1";

MySqlDataReader reader6= cmd.ExecuteReader();

if(reader6.Read())
{
   code4 = (byte[])reader6["BACK_IMG"];   //BLOB FIELD NAME BACK_IMG
}
reader6.Close();

MemoryStream stream = new MemoryStream(code4);   //code4 is a public byte[] defined on top                             
pictureBox3.Image = Image.FromStream(stream);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
-1

The problem is because, you are bringing it incorrectly from database. Try changing your code like this:

while (registry.Read())
{
   byte[] image = (byte[])registry["Image"];
}
O. Jones
  • 103,626
  • 17
  • 118
  • 172
-1

In my case I got the error since my base64 string had wrong encoding before calling Image.FromStream. This worked for me in the end:

byte[] bytes = System.Convert.FromBase64String(base64ImageString);

using (MemoryStream ms = new MemoryStream(bytes))
{
    var image = Image.FromStream(ms);
    image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
-2

try this,

public Image byteArrayToImage(byte[] byteArrayIn)
{
     Image returnImage = null;
     using (MemoryStream ms = new MemoryStream(byteArrayIn))    
     {   
         returnImage = Image.FromStream(ms);     
     }
     return returnImage;
}
Community
  • 1
  • 1
Mohan Gopi
  • 237
  • 1
  • 4
  • 12
  • Hi Umair, I think your byte[] value is not in correct format. do one think, for checking take one image convert to byte[] after that pass this byte value to above coding. If it's converting correct image mean's the problem in your byte value. Let's know.. – Mohan Gopi Jul 17 '13 at 05:23
  • having the same issue when downloading an image from S3. However, when using the file before uploading (after submited using a form) it works without an issue. Still can't figure this out – Liron Harel Feb 26 '14 at 14:18