ArgumentException: Invalid parameter used at Bitmap.SetPixel

  • Thread starter Thread starter halise
  • Start date Start date
H

halise

Hi,

i am in trouble with an exception in .net platform with c#.

The exception thrown is exactly as follows:

System.ArgumentException: Invalid parameter used.
at System.Drawing.Bitmap.SetPixel(Int32 x, Int32 y, Color color)
at binaryData2Image.Form1.button_Click(Object sender, EventArgs e)
in c:\documents and settings\..\my documents\visual studio
projects\binarydata2image\form1.cs:line 237

my code is something like that

// Create a bitmap to set the binary data as pixel data
// Bit depth is 16bits per pixel
Bitmap bmp = new Bitmap (256, 256,
System.Drawing.Imaging.PixelFormat.Format16bppRgb565 );

// Created for reading binary data and extracting color values
BinaryReader br_img = new BinaryReader(File.OpenRead("C:/Documents and
Settings/../SE1/IM1bdata2im"));


try
{

for(int y=1; y<256; ++y)
{

for(int x=1; x<256; ++x)
{

ushort pix = br_img.ReadUInt16();

//* For PixelFormat.Format16bppRgb565
int red = (int)( pix & 0xF800 ) >> 11 ; //takes the
first 5 most significant bits
int green = (int) ( pix & 0x07E0 ) >> 5
;//takes the bits from 6th to 12th
int blue = (int)( pix & 0x001F ) ;//takes the last 5 least
significant bits

bmp.SetPixel(x, y, Color.FromArgb(red, green, blue ) ) ;

}

}
}
catch( Exception exc )
{
textbox0.Text = exc.ToString();
}

br_img.Close();

picturebox.BackgroundImage = bmp;


I cant find why this exception is thrown, because everyhing seems very
reasonable to me.

Besides, when i used PixelFormat.FormatGrayScale, I got the same
problem with SetPixel method too.

please, can anyone help me?

thanks, in advance!

halise
 
Hi Halise,

I see nothing wrong with this code. Could you post the actual code you
use?

Do you get the same problem using other PixelFormats?

Btw, it appears you are creating a bitmap from raw data which will benefit
hugely from using LockBits in unsafe mode (~90% time cut compared to
SetPixel)
 
hi, i still cant decide whether i can use LockBits or not in my case,
that is creating a bitmap from my binary data rather than processing
the pixel data of the bitmap.

what dou you think about that? can you clearify more?

thanks,

halise.
 
Add using System.Text;
then error
An unhandled exception of type 'System.IO.DirectoryNotFoundException'
occurred in mscorlib.dll

Additional information: DirectoryNotFoundException
halise said:
Hi,

i am in trouble with an exception in .net platform with c#.

The exception thrown is exactly as follows:

System.ArgumentException: Invalid parameter used.
at System.Drawing.Bitmap.SetPixel(Int32 x, Int32 y, Color color)
at binaryData2Image.Form1.button_Click(Object sender, EventArgs e)
in c:\documents and settings\..\my documents\visual studio
projects\binarydata2image\form1.cs:line 237

my code is something like that

// Create a bitmap to set the binary data as pixel data
// Bit depth is 16bits per pixel
Bitmap bmp = new Bitmap (256, 256,
System.Drawing.Imaging.PixelFormat.Format16bppRgb565 );

// Created for reading binary data and extracting color values
BinaryReader br_img = new BinaryReader(File.OpenRead("C:/Documents and
Settings/../SE1/IM1bdata2im"));


try
{

for(int y=1; y<256; ++y)
{

for(int x=1; x<256; ++x)
{

ushort pix = br_img.ReadUInt16();

//* For PixelFormat.Format16bppRgb565
int red = (int)( pix & 0xF800 ) >> 11 ; //takes the
first 5 most significant bits
int green = (int) ( pix & 0x07E0 ) >> 5
;//takes the bits from 6th to 12th
int blue = (int)( pix & 0x001F ) ;//takes the last 5 least
significant bits

bmp.SetPixel(x, y, Color.FromArgb(red, green, blue ) ) ;

}

}
}
catch( Exception exc )
{
textbox0.Text = exc.ToString();
}

br_img.Close();

picturebox.BackgroundImage = bmp;


I cant find why this exception is thrown, because everyhing seems very
reasonable to me.

Besides, when i used PixelFormat.FormatGrayScale, I got the same
problem with SetPixel method too.

please, can anyone help me?

thanks, in advance!

halise
 
Back
Top