Reputation: 8981
I have a byte array where the size of the array is 3104982 ~ 2.9 MB
. I want to create a bitmap of this image, but I ran into the parameter is not valid - ArgumentException
I've tried several options:
public static Bitmap ByteArrayToBitmap(byte[] blob)
{
using (var mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Position = 0;
var bm = new Bitmap(mStream);
return bm;
}
}
and
public static Bitmap ByteArrayToBitmap(byte[] blob)
{
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap) tc.ConvertFrom(blob);
return bitmap1;
}
but the exception still occurs. Any ideas?
Upvotes: 0
Views: 2770
Reputation: 54453
I don't think you can do it without knowing the Width
and Height
of th Bitmap.
A Length of 120000 pixels could come from a 300x400 bitmap or 400x300 or 200x600 or 600x100 or 12000x10..
How would the system be able do infer it from a raw ByteArray's Length??
Also the PixelFormat
(Color depth, number and meaning of bytes per pixel) must be known to create a bitmap.
How is your ByteArray created?
Upvotes: 1
Reputation: 453
Try this
public Bitmap changeByteToImage( Byte[] imagedata )
{
System.IO.MemoryStream ms=new System.IO.MemoryStream(imagedata);
Bitmap picdata=new Bitmap(Image.FromStream(ms));
return picdata;
}
Upvotes: 0
Reputation: 1482
I've tested it using a console program and it works ok for me.
class Program
{
static void Main(string[] args)
{
const string fileName = @"H:\Stackoverflow\C#\20140601 - Question 1\Image\DSC_0004b.jpg";
//const string fileName = @"H:\Stackoverflow\C#\20140601 - Question 1\Image\Test.txt";
byte[] blob;
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
blob = new byte[fs.Length];
fs.Read(blob, 0, (int)fs.Length);
}
Bitmap bitmap = ByteArrayToBitmap(blob);
Console.WriteLine("Height = {0}, Width = {1}", bitmap.Height, bitmap.Width);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
}
protected static Bitmap ByteArrayToBitmap(byte[] blob)
{
using (var mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Position = 0;
var bm = new Bitmap(mStream);
return bm;
}
}
}
The test results are displayed as:
Height = 3076, Width = 2104
Press any key to continue . . .
If I use a text file instead of an image file, then it throws an ArgumentException as you describe. It displays:
Parameter is not valid.
Press any key to continue . . .
When I code step it, the exact line that throws the exception is:
var bm = new Bitmap(mStream);
When you examine the ArgumentException object, there isn't any more useful information in it unfortunately. (eg Name of parameter would be helpful.)
So could it be that you are feeding invalid data in the blob parameter?
You might want to improve your exception handling so that it is more informative. For example:
protected static Bitmap ByteArrayToBitmap(byte[] blob)
{
using (var mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Position = 0;
try
{
var bm = new Bitmap(mStream);
return bm;
}
catch (Exception ex)
{
throw new ArgumentException("Not a valid bitmap.", "blob", ex);
}
}
}
Upvotes: 1
Reputation: 1064114
If the byte[]
is formatted data, then:
return Image.FromStream(mStream);
Which you might want to cast as Bitmap
if you are sure the data is Bitmap
.
return (Bitmap)Image.FromStream(mStream);
Upvotes: 1