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

7
  • I edited It. oOption.SelectedFile has a bytes Commented Jul 16, 2013 at 4: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). Commented Jul 16, 2013 at 4:10
  • Image returnImage = Image.FromStream(ms); this lines gives an error that paramter is not valid Commented Jul 16, 2013 at 4:11
  • The byte array is likely not a valid image (cannot be converted so the Image.FromStream is failing). Commented Jul 16, 2013 at 4:14
  • 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: stackoverflow.com/questions/6712557/… Commented Jul 16, 2013 at 4:19

6 Answers 6

13

try this

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

    return img;
}
Sign up to request clarification or add additional context in comments.

4 Comments

No , It doesn`t Work . my bytes are 1120135
like this byte[] array = { 68, 111, 116, 32, 78, 101, 116, 32, 80, 101, 114, 108, 115 };
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
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: stackoverflow.com/questions/457370/…
8

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;

Comments

-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"];
}

Comments

-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);
}

Comments

-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);

Comments

-2

try this,

public Image byteArrayToImage(byte[] byteArrayIn)
{
     Image returnImage = null;
     using (MemoryStream ms = new MemoryStream(byteArrayIn))    
     {   
         returnImage = Image.FromStream(ms);     
     }
     return returnImage;
}

2 Comments

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..
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

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.