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

ArgumentException: Invalid parameter used at Bitmap.SetPixel

P: n/a
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.Format16bppRgb5 65 );

// 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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


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

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

P: n/a
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.

Morten Wennevikwrote: 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)

--
Happy Coding!
Morten Wennevik [C# MVP][/quote:1b6849140c]

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #3

P: n/a
Add using System.Text;
then error
An unhandled exception of type 'System.IO.DirectoryNotFoundException'
occurred in mscorlib.dll

Additional information: DirectoryNotFoundException
"halise" <ha********@yahoo-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
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.Format16bppRgb5 65 );

// 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
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.