M
Morgan Cheng
I am writing a very simple Win Form app.
One PictureBox (named ViewPicBox) in the main form (named ViewForm). In
the constructor of ViewForm, I set the Image property of ViewPicBox.
ViewForm()
{
InitializeComponent(); // auto generated by VS2005
ViewPocBox.Image = Image.FromFile(@"C:\abc.jpg");
}
After Ctrl+F5, it works fine and display the image.
Then I tried to use Graphics and modify the code to be:
ViewForm()
{
InitializeComponent(); // auto generated by VS2005
drawImage(@"C:\abc.jpg");
}
void drawImage(string fileName)
{
Image pic = Image.FromFile(fileName);
Graphics graph = ViewPicBox.CreateGraphics();
graph.DrawImage(pic, 0, 0, pic.Width, pic.Height);
graph.Dispose();
pic.Dispose();
}
It will not diplay the image after launched. I tried to call drawImage
in menu item event handler, it works fine. So, I suppose there is
something happened after ViewForm constructor that clear the content of
ViewPicBox.
Anyone knows what happens after the form constructor?
One PictureBox (named ViewPicBox) in the main form (named ViewForm). In
the constructor of ViewForm, I set the Image property of ViewPicBox.
ViewForm()
{
InitializeComponent(); // auto generated by VS2005
ViewPocBox.Image = Image.FromFile(@"C:\abc.jpg");
}
After Ctrl+F5, it works fine and display the image.
Then I tried to use Graphics and modify the code to be:
ViewForm()
{
InitializeComponent(); // auto generated by VS2005
drawImage(@"C:\abc.jpg");
}
void drawImage(string fileName)
{
Image pic = Image.FromFile(fileName);
Graphics graph = ViewPicBox.CreateGraphics();
graph.DrawImage(pic, 0, 0, pic.Width, pic.Height);
graph.Dispose();
pic.Dispose();
}
It will not diplay the image after launched. I tried to call drawImage
in menu item event handler, it works fine. So, I suppose there is
something happened after ViewForm constructor that clear the content of
ViewPicBox.
Anyone knows what happens after the form constructor?