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

C

CroDude

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.MimeType == mimeType)
{
Result = Encoders;
}
}
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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top