PictureBox Bug, throws exception.

G

Guest

Sirs

Maybe I have a reproductible bug that trows the folowing exception

Invalid Parameter Use

at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs
at System.Windows.Forms.Control.WmPaint(Message& m
at System.Windows.Forms.Control.WndProc(Message& m
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context
at System.Windows.Forms.Application.Run(Form mainForm
at ImageWarpingClient.Form1.Main() in c:\documents and settings\camilo\my documents\visual studio projects\imagewarpingpanoramic\form1.cs:line 170" strin

I have one app with 2 PictureBox
I have two buttons to load this picture box. One of the buttons have the folowing code. The another is the same changing the picturebox

private void button1_Click(object sender, System.EventArgs e

openFileDialog1.InitialDirectory = "c:\\"
openFileDialog1.Filter = "jpg files (*.jpg)|*.mpg|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = true

if(openFileDialog1.ShowDialog() == DialogResult.OK

string str = openFileDialog1.FileName

if (pictureBox1.Image != null
pictureBox1.Image.Dispose()
pictureBox1.Image = new Bitmap(str)
pictureBox1.Width = pictureBox1.Image.Width
pictureBox1.Height = pictureBox1.Image.Height
pictureBox1.Visible = true



If I load the picturebox2 and after picturebox1 nothing happens, but if I load the picturebox1 and after the picturebox2 in any paint my program crashes

Any help? Ideas

Camilo
 
J

Justin Rogers

You are probably getting a Paint message after disposing the graphic attached to
your pictureBox1.Image property.

Image disposeLater = pictureBox1.Image;
pictureBox1.Image = new Bitmap(str);
disposeLater.Dispose();

I imagine your exception is due to a disposed, but non null image reference
being passed into DrawImage.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Camilo Telles said:
Sirs,

Maybe I have a reproductible bug that trows the folowing exception.

Invalid Parameter Used

at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,
Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+I
MsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32
pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ImageWarpingClient.Form1.Main() in c:\documents and settings\camilo\my
documents\visual studio projects\imagewarpingpanoramic\form1.cs:line 170" string
I have one app with 2 PictureBox.
I have two buttons to load this picture box. One of the buttons have the
folowing code. The another is the same changing the picturebox:
private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "jpg files (*.jpg)|*.mpg|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string str = openFileDialog1.FileName;

if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
pictureBox1.Image = new Bitmap(str);
pictureBox1.Width = pictureBox1.Image.Width;
pictureBox1.Height = pictureBox1.Image.Height;
pictureBox1.Visible = true;
}
}

If I load the picturebox2 and after picturebox1 nothing happens, but if I load
the picturebox1 and after the picturebox2 in any paint my program crashes.
 

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