(Update_2 Information)
I found a strange condition which may cause the problem.
1. I update the paint event as below :
try
{
bm = new Bitmap(ObjPicture);
Graphics g = cc.CreateGraphics();
g.DrawImage(bm, 0, 0, cc.Width, cc.Height);
}
catch (Exception ex)
{
Console.WriteLine("bm exception : " + ex.ToString());
}
2. I write new line for openfiledialog.filter in button2 event as below :
OpenFileDialog.Filter = "All Files (*.*)|*.*";
3. When I click button to select some file with different filename extension, such as *.gmz (will be used in my program), *.gmj, *.abc etc, it then bring out the problem, but if I then select another file name, such as *.vgh (also will be used in my program), *.pdb etc, it then can be resumed to be ok.
4. Is this the possible root cause ? How to correct it ? Thanks.
==============================================
(Update Information)
I would provide more information for 'question answer', and to all of you to help in solving problem. Since the code is not so simple, and is not easy to show in clear, so I write description in lines below.
(A) Regarding to the ObjPicture
1. The ObjPicture is declared as String type.
2. Normally, the whole process works well.
3. When problem happen (see below item 4), I debug and trace the
ObjPicture, it shows the ObjPicture has correct string filepath as expected. But, why it pops up the exception !?
4. The problem happen every time when click button to do openfiledialog
function to select file and load(read) data into buffer.
5. Since the original button event to load file is more complicated, I
write a simple button event below to simulate the scenario, the problem
then will not happen every time, but so often.
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog.Filter = "all file (*.*)|*.*";
OpenFileDialog.ShowDialog();
}
(B)Regarding to CreateGraphics in paint event
1. If do not use CreateGraphics, any other way to use ?
2. The program will allow user to create several panel objects in
form, the user then will click on panel object to move or resize it.
3. Each panel object will be assigned an image file, and now I use
CreateGraphics in paint event to refresh it. I do not set the
BackgroundImage attribute for panel object, since I found that :
3.1 the movement will be slower.
3.2 when resize the panel object, the image will not be resized
together (the image will keep it size).
4. Is there any better or correct way to reach the requirement ?
==========================================================
(First description)
I write a Window form application, it announce bm as Bitmap type (A),
create ControlPanel of Panel object (B), with paint event (C), and then my program will do movement on the object.
The condition is that when I click a button (D), to allow user to select a file, however, when user select any file and press 'OK' on openfiledialog dialog, it then may (most of time) pop up an ArgumentException exception on 'bm = new Bitmpa(ObjPicture)' line !
I trace my program, it seems be no problem, so what's the possible root cause ? Can any body help to solve thsi problem ? Thanks.
(A)
// declare bm as Bitmap type
Bitmap bm;
....
(B)
// create ControlPanel of Panel object
Panel ControlPanel = new Panel();
// announce paint evnet for this panel object
ControlPanel.Paint += new PaintEventHandler(ControlPanel_Paint);
....
(C)
// the paint event
void ControlPanel_Paint(object sender, PaintEventArgs e)
{
Control cc = (Control)sender;
......
ObjPicture = …… (to get the file path)
bm = new Bitmap(ObjPicture);
Graphics g = cc.CreateGraphics();
g.DrawImage(bm, 0, 0, cc.Width, cc.Height);
}
......
(D)
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "all file (*.*)|*.*";
openFileDialog1.ShowDialog();
}
......