Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
(Update_2 Information)

I found a strange condition which may cause the problem.

1. I update the paint event as below :

C#
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 :

MIDL
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();
}
......
Posted
Updated 7-Dec-10 23:31pm
v4

First, get rid of the ...cc.CreateGraphics line. The Graphics object is given to you in the PaintEventArgs. Use that instead.

You also left out some of most important code to figure out your problem. Specifically, the list that starts with ObjPicture=.... We have no idea how ObjPicture is declared, nor how it is assigned its value.
 
Share this answer
 
Comments
Sports Kuo 7-Dec-10 20:28pm    
========================================
Thanks of your infomation. I also will refer to the suggetion from Johannesnestler, let's discuss togehter.
Sports Kuo 9-Dec-10 3:42am    
Hi Dave, I've solved the problem, the root cause is due to the ObjPicture filepath setting, it should be assigned to be System.Environment.CurrentDirectory, or, it will be impacted when user select file in different directory. Plus, I update the line to use e.Graphics instead of g.CreateGraphics, is that right !? Thanks.
Dave Kreskowiak 9-Dec-10 7:59am    
CurrentDirectory wouldn't have anything to do with it. CurrentDriectory can also change depending on what your app does over its lifetime. I suspect tat your real problem is that you tried to create a Bitmap object from a filename, but you only gave the filename (mypic.bmp) and not the fully qualified path to the file (C:\Program Files\SomeFolder\mypic.bmp).
Yep as Dave mentioned - don't use CreateGraphics in Paint method.
Something mit ObjPicture is wrong - so find out why - debug all assignments of that variable, or show us the relevant code :doh:
 
Share this answer
 
Comments
Sports Kuo 7-Dec-10 21:16pm    
========================================
Sorry, there is line limitation when 'add comment', please see my update information in my 'question context'.
Sports Kuo 9-Dec-10 3:44am    
===========================================================
Hi johannesnestler, I've solved the problem, please see the comment in above Dave answer. Thansk.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900