Error when read/write Bitmap to byte-array(System.ArgumentException: Invalid parameter used)

Error when read/write Bitmap to byte-array(System.ArgumentException: Invalid parameter used)

Post by CroDud » Tue, 25 Jan 2005 05:20:38


Hi all!
I have problems when writting bitmap to a byte array and after reading it
back form byte[] to Bitmap object.
What I do is this: First I throw Bitmap to a memory-stream and then I write
it into byte[] from a stream.
Exception (System.ArgumentException: Invalid parameter used) occurs when
reading from byte[] over a memory-stream back to the Bitmap object.
Please help, I'm really stuck here!

Here's the code I use (Sorry for a long post):

private void btn_Start_Click(object sender, System.EventArgs e)
{
byte[] imageData;
imageData = this.ImageToByteArray();
this.GetImageFromByteArray(imageData);
}

private byte[] ImageToByteArray()
{
// Store thumbnail image into the thumbnailImage row
long _quality = 50;
// Add thumbnail image as byte array (OLE object)
System.IO.Stream memoryStream =
this.BitmapToMemoryStream((Bitmap)this.sourceImageBox.Image,
ImageFormat.Jpeg, _quality);
//MemoryStream memoryStream = new MemoryStream();
//_thumbnail.ThumbnailImage.Save(memoryStream, ImageFormat.Jpeg);
byte[] rawData = new byte[memoryStream.Length];
memoryStream.Read(rawData, 0, System.Convert.ToInt32(memoryStream.Length));
memoryStream.Close();
return rawData;
}

private MemoryStream BitmapToMemoryStream(Bitmap _image, ImageFormat
_format, long _quality)
{
// Create MemoryStream object
MemoryStream memoryStream = new MemoryStream();
// Save the Bitmap to the Stream. If it's
// in JPEG format, save with the specified
// Quality level.
if (_format != ImageFormat.Jpeg)
{
// Save non-JPEG images withoug
// adjusting the Quality level
_image.Save(memoryStream, _format);
}
else
{
// Adjust quality level of JPEG images.
// Create an EncoderParameters object containing the Quality level as a
// parameter.
EncoderParameters Params = new EncoderParameters(1);
Params.Param[0] = new EncoderParameter(Encoder.Quality, _quality);
// Save the image using the JPEG encoder
// with the specified Quality level.
_image.Save(memoryStream, this.GetEncoderInfo("image/jpeg"), Params);
}
return memoryStream;
}

private ImageCodecInfo GetEncoderInfo(String mimeType)
// Return an encoder of the specified Mime type
// (e.g. "image/jpeg").
{
ImageCodecInfo Result = null;
ImageCodecInfo[] Encoders = ImageCodecInfo.GetImageEncoders();
for(int i = 0; Result == null && i < Encoders.Length; i++)
{
if (Encoders[i].MimeType == mimeType)
{
Result = Encoders[i];
}
}
return Result;
}
public void GetImageFromByteArray(byte[] _rawData)
{
try
{
// Read data to a stream
byte[] rawData = new byte[0];
rawData = (byte[])_rawData;
int len = new int();
len = rawData.GetUpperBound(0);
// Make a MemoryStream object to store bitmap from database
System.IO.Stream memStream = new MemoryStream(rawData);
// Make a bitmap object from a memory-stream
Bitmap image = new Bitmap(memStream);
memStream.Close();
this.destinationImageBox.Image = (Bitmap)image;
}
catch(Exception e)
{
MessageBox.Show(e.ToString(), "GetImageFromByteArray",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
 
 
 

1. System.ArgumentException:Invalid parameter used

2. Actual bit patterns used by write-byte and read-byte?

Does the language standard say anything about the actual bit patterns
that will be written to disk if I open a file stream with
:element-type '(unsigned-byte 8) and call write-byte? Similarly, what
are we guaranteed about how read-byte interprets the "bytes" it reads?
(I assume in reality that on all contemporary operating systems
streams with :element-type (unsigned-byte 8) operate on octets.) How
about for larger sizes of unsigned-byte, e.g. 16 or 32? Big endian?
Little endian? Up to the implementation?

-Peter


--
Peter Seibel XXXX@XXXXX.COM

Lisp is the red pill. -- John Fraser, comp.lang.lisp

3. "error CS1577: Assembly generation failed -- Unexpected exception processing attribute -- System.ArgumentException: Invalid directory on URL."

4. Writing array of bytes, socket write error

5. ArgumentException received when converting a byte array to bit

6. Convert Bitmap to Byte and Byte to Bitmap

7. reading/writing a byte array in java

8. writing image to byte-array

9. key to speed - use byte array instead of writing to locked image bytes

10. byte array size for read or write of filestream

11. Writing the file using Write to SGL and reading the data using Read from SGL

12. read/write data byte-per-byte to and from a socket

13. How can I write and read byte by byte at one choosen sector on my HDD

14. how to read/write a characters stream which is either of one byte/2 byte

15. System.ArgumentException: Invalid or unsupported code page type.