473,503 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.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));
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);
}
}
Nov 16 '05 #1
0 3563

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1798
by: Martyn Wynne | last post by:
Hi, Can anyone please tell me if there is any reason why when i am streaming from a webrequest (decompressing on route) to a file on the hard drive, i would be getting an exception of Filestream...
1
3866
by: Frank Jones | last post by:
When manually running resgen.exe from the command prompt (Visual Studio .NET 2003 command prompt) I get the following error: error: Invalid ResX input. error: Specific exception:...
0
1788
by: David Veeneman | last post by:
This post is for the Google archive and does not require a reply. I received an 'Invalid parameter used' error when trying to do double-buffering with the .Net SetStyles method. I used this code...
2
2352
by: Matthew Louden | last post by:
I want to read how many records in the table, and insert a record with id field which increment the counter by 1. However, I had the following runtime on Dim s As Integer = CInt(dr("t")). Since "t"...
5
1569
by: Patrick.O.Ige | last post by:
What could cause the error:- System.NullReferenceException: Object reference not set to an instance of an object. Any ideas?
2
10873
by: Simon Harris | last post by:
I have created a web service, which when I call in my browser presents the text form etc. When I click the button, I get this error: System.ArgumentException: Cannot convert to System.Int32....
1
13950
by: Harshil | last post by:
Hi All, I am working on Vb.net application with oracle as my database. I have developed reports using crystal report 9. Usually when you open reports directly in crystal report viewer control it...
9
24162
by: CQ | last post by:
Hi everyone, I get the following error when compiling my code: error: invalid initialization of non-const reference of type 'Vertex&' from a temporary of type 'int' The implementation of...
5
4229
by: Eric | last post by:
I run a program which read emails from a text file. There is some thing invalid in one or more text file. When program is busy doing parsing it shows that error and process stop. I dont know which...
7
14928
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.