Trouble converting byte[] to bmp.....
-
public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); }
I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.I'm listening but I only speak GEEK.
-
public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); }
I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.I'm listening but I only speak GEEK.
I did something like this the other day, using the static Bitmap.FromStream() method. Here's my code - my bitmap was encoded in a string...
MemoryStream ms = new MemoryStream( Convert.FromBase64String( eImage.InnerText ) ); Bitmap bmp = Bitmap.FromStream( ms ) as Bitmap;
Why do you stream.Write()? Isn't the data already in the MemoryStream from the constructor? I also don't understand why you set bmp = null, or what the purpose of constructing a new Bitmap on return. -
public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); }
I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.I'm listening but I only speak GEEK.
Parts of your code are a mess. Also, you don't say where the errors occurred - this is important and I cannot guess where. Lets examine the code:
MemoryStream stream = new MemoryStream( b, true ); // MSDN MemoryStream Constructor[^]
stream.Write( b, 0, b.Length ); // MSDN MemoryStream.Write[^]I'm not sure about your thought processes here. You create a MemoryStream using the contents of the buffer, b, which contains the data. Then you write the contents of b into the stream. But the stream already contains the contents of b. I suggest that the Write operation is redundant.
Bitmap bmp = new Bitmap( stream ); // MSDN Bitmap Constructor[^]
stream.Close();This seems reasonable.
bmp = null;
return new Bitmap( bmp, imageSize );I am curious as to what you think this actually does. I don't mean to be unkind, but I really don't understand your thought process. The following would seem to be a good way forward:
using(MemoryStream ms = new MemoryStream(b))
{
Bitmap bmp = new Bitmap(ms);
return bmp;
}
Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website
-
Parts of your code are a mess. Also, you don't say where the errors occurred - this is important and I cannot guess where. Lets examine the code:
MemoryStream stream = new MemoryStream( b, true ); // MSDN MemoryStream Constructor[^]
stream.Write( b, 0, b.Length ); // MSDN MemoryStream.Write[^]I'm not sure about your thought processes here. You create a MemoryStream using the contents of the buffer, b, which contains the data. Then you write the contents of b into the stream. But the stream already contains the contents of b. I suggest that the Write operation is redundant.
Bitmap bmp = new Bitmap( stream ); // MSDN Bitmap Constructor[^]
stream.Close();This seems reasonable.
bmp = null;
return new Bitmap( bmp, imageSize );I am curious as to what you think this actually does. I don't mean to be unkind, but I really don't understand your thought process. The following would seem to be a good way forward:
using(MemoryStream ms = new MemoryStream(b))
{
Bitmap bmp = new Bitmap(ms);
return bmp;
}
Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website
Colin Angus Mackay wrote:
Also, you don't say where the errors occurred
Error Message[^] Understanding the logic behind the MemoryStream basiclly this is the first time I'd used it and based of examples I'd found, I thought I need to perform the write to prepare the stream. I'm still getting the same error message. I've added a link to a screen shot of the error message. Thanks for your help.
I'm listening but I only speak GEEK.
-
I did something like this the other day, using the static Bitmap.FromStream() method. Here's my code - my bitmap was encoded in a string...
MemoryStream ms = new MemoryStream( Convert.FromBase64String( eImage.InnerText ) ); Bitmap bmp = Bitmap.FromStream( ms ) as Bitmap;
Why do you stream.Write()? Isn't the data already in the MemoryStream from the constructor? I also don't understand why you set bmp = null, or what the purpose of constructing a new Bitmap on return.Nick Hodapp wrote:
Why do you stream.Write()?
Basiclly this is the first time I'd used it and based of examples I'd found, I thought I need to perform the write to prepare the stream.
Nick Hodapp wrote:
I also don't understand why you set bmp = null
I'll be calling this method hundreds of times with ASP.NET and will need to make sure that Garbage Collector knows I'm done with the variable. I'm still getting the same error message[^]. I've added a link to a screen shot of the error message. Thanks for your help.
I'm listening but I only speak GEEK.
-
public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); }
I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.I'm listening but I only speak GEEK.
Thank you every one for your help.
public static Bitmap BytesToBmp( byte[] b )
{
using ( MemoryStream ms = new MemoryStream(b , true) )
{
Bitmap bmp = Bitmap.FromStream( ms ) as Bitmap;
return bmp;
}
}Works great. This is was the byte[] that I was sending the method; it was not being recognized by the Bitmap Constructor. I am storing images within an Access 2003 db, (yes, not a good idea), and the way Access had been altering the byte [] so that the Bitmap Constructor couldn't recreate it. I'm not sure n exaclty what Access was doing with the Image but I as inserting a bitmap from file to it. ??? If anyone know the answer to this, please drop me a line. Otherwise, I'm now converting the image and inserting it into the database using a FileStream in and then a FileStream out. If anyone is interested I will post the project when I've completed. Thanks again!!
I'm listening but I only speak GEEK.