Parameter is not valid error

Getting parameter is not valid error while converting byte array to Im

Page 1 of 1

6 Replies - 54344 Views - Last Post: 02 April 2009 - 05:57 PM Rate Topic: -----

#1 vablatha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-February 09

Parameter is not valid error

Posted 01 April 2009 - 03:53 AM

Hello,

I am getting 'Parameter is not valid' error while converting from byte array to Image in C#. Below is my code.

public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
    {
        System.Drawing.Image returnImage = null;

        try
        {
            ms = new MemoryStream(byteArrayIn);
            returnImage = System.Drawing.Image.FromStream(ms); // Here I am getting Parameter is not valid' error.
        }
        catch (Exception e)
        {
            string str = e.Message;
        }
        return returnImage;
    }


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 01 April 2009 - 04:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Parameter is not valid error

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Parameter is not valid error

Posted 01 April 2009 - 04:29 PM

Try
MemoryStream ms = new MemoryStream(byteArrayIn);
Was This Post Helpful? 0
  • +
  • -

#3 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Parameter is not valid error

Posted 01 April 2009 - 06:19 PM

How are you creating the byte array?
Was This Post Helpful? 0
  • +
  • -

#4 vablatha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-February 09

Re: Parameter is not valid error

Posted 01 April 2009 - 09:35 PM

View PostJayman, on 1 Apr, 2009 - 05:19 PM, said:

How are you creating the byte array?


Hello Jayman,

Actually this code is a sub method in WebService method for which I am giving bytearray as input parameter from Client. The code is:
						// Get a reference to PostedFile object
						HttpPostedFile myFile = FileUpload1.PostedFile;
					 
						// Get size of uploaded file
						int nFileLen = myFile.ContentLength;

						// Allocate a buffer for reading of the file
						byte[] myData = new byte[nFileLen];

						// Read uploaded file from the Stream
						myFile.InputStream.Read(myData, 0, nFileLen);


Thanks&Regards,
Latha
Was This Post Helpful? 0
  • +
  • -

#5 vablatha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-February 09

Re: Parameter is not valid error

Posted 01 April 2009 - 10:14 PM

View Postn8wxs, on 1 Apr, 2009 - 03:29 PM, said:

Try
MemoryStream ms = new MemoryStream(byteArrayIn);


Hello n8wxs,

I created memorystream as you said but the problem still coming and same error message 'Parameter is not Valid'
Was This Post Helpful? 0
  • +
  • -

#6 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Parameter is not valid error

Posted 02 April 2009 - 12:48 PM

System.Drawing.Image.FromStream takes a Stream as a parameter, not a MemoryStream.
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Parameter is not valid error

Posted 02 April 2009 - 05:57 PM

The "Parameter is not Valid" is most probably because the data in the byte array is not an "image' format.

This works:
private void Form2_Load(object sender, EventArgs e)
{
	Image img;

	img = Image.FromFile(@"C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\cswf1\cswf1\35FLOPPY.BMP");

	byte[] bar = ImageToByteArray(img);

	img = null; // remove image

	img = byteArrayToImage(bar);

	pictureBox1.Image = (Image) img.Clone();
}

public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
	System.Drawing.Image returnImage = null;

	try
	{
		MemoryStream ms = new MemoryStream(byteArrayIn);

		returnImage = System.Drawing.Image.FromStream(ms); // Here I am getting Parameter is not valid' error.
	}
	catch (Exception e)
	{
		//string str = e.Message;
		MessageBox.Show(e.Message);
	}
	return returnImage;
}

public byte[] ImageToByteArray(Image img)
{
	using (MemoryStream ms = new MemoryStream()) {
		img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
		return ms.ToArray();
	}			
}


Was This Post Helpful? 1

Page 1 of 1