By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();

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.sourceImage Box.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));
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);
// 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)
// 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);
this.destinationImageBox.Image = (Bitmap)image;
catch(Exception e)
MessageBox.Show(e.ToString(), "GetImageFromByteArray",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.