Error" Parameter is not valid " while converting Bytes into Image
65,507
Solution 1
try this
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(byteArrayIn);
return img;
}
Solution 2
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;
Author by
Umair Aslam
Updated on January 27, 2020Comments
-
Umair Aslam about 4 years
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
-
Umair Aslam almost 11 yearsNo , It doesn`t Work . my bytes are 1120135
-
sangram parmar almost 11 yearslike this byte[] array = { 68, 111, 116, 32, 78, 101, 116, 32, 80, 101, 114, 108, 115 };
-
Umair Aslam almost 11 yearsActually 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
-
Mohan Gopi almost 11 yearsHi 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..
-
Liron Harel about 10 yearshaving 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
-
pookie over 7 yearsOmg. This saved me. I had already spent a couple hours with the old
parameter is not valid
exception. I was using theImage.FromStream
method: stackoverflow.com/questions/457370/…