I have this:
byte[] ar = new byte[ArrayAnsammlung[DurchLaeufer].Length];
ArrayAnsammlung[DurchLaeufer].CopyTo(ar, 0);
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
try
{
bild = (Bitmap)tc.ConvertFrom(ar);
}
catch (Exception ddd)
{
Console.WriteLine(ddd.ToString());
}
ar contains bitmap data, ie Blue-Green-Red-Alpha-Blue....
I am trying to convert it into the Bitmap bild. This is what the console shows:
A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
System.ArgumentException: Invalid Parameter.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(Object value)
at SK.MainWindow.thread() in MainWindow.xaml.cs:Zeile 523.
How do I solve this problem?
Try this :
public static Bitmap BytesToBitmap(byte[] byteArray)
{
using (MemoryStream ms = new MemoryStream(byteArray))
{
Bitmap img = (Bitmap)Image.FromStream(ms);
return img;
}
}
I'm not sure why the TypeConverter
isn't working, but you can achieve this just as easily with the Bitmap
class's Stream constructor:
var memoryStream = new MemoryStream(ar)
var bild = new Bitmap(memoryStream);
Note that as per MSDN , the memoryStream
has to stay alive for the lifetime of the object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.