473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

What happens after Form constructor?

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?

Oct 9 '06 #1
4 1505
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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?
This is the #1 question in Bob Powell's faq (link below). Whatever is drawn
onto your form is not permanent and needs to be redrawn every time i new
part of your window is show. For example if the window is minimised then the
picture will need to be redrawn when it is next maximised, or if another
window covers part of it it will need to be redrawn. The solution is to draw
the image in the onpaint event.

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
>

Oct 9 '06 #2
Thanks.

But, call CreateGraphics and then dispose it in each Paint event
repeatedly seems a little wasteful. Will it impact performance?


Michael C wrote:
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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?

This is the #1 question in Bob Powell's faq (link below). Whatever is drawn
onto your form is not permanent and needs to be redrawn every time i new
part of your window is show. For example if the window is minimised then the
picture will need to be redrawn when it is next maximised, or if another
window covers part of it it will need to be redrawn. The solution is to draw
the image in the onpaint event.

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
Oct 9 '06 #3
You don't need to call CreateGrapics there. OnPaint (or Paint event
handler) has a PaintEventArgs parameter. Check out
PaintEventArgs.Graphics.

Morgan Cheng wrote:
Thanks.

But, call CreateGraphics and then dispose it in each Paint event
repeatedly seems a little wasteful. Will it impact performance?


Michael C wrote:
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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?
This is the #1 question in Bob Powell's faq (link below). Whatever is drawn
onto your form is not permanent and needs to be redrawn every time i new
part of your window is show. For example if the window is minimised then the
picture will need to be redrawn when it is next maximised, or if another
window covers part of it it will need to be redrawn. The solution is to draw
the image in the onpaint event.

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
>
Oct 9 '06 #4
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Thanks.

But, call CreateGraphics and then dispose it in each Paint event
repeatedly seems a little wasteful. Will it impact performance?
As morgan said you can just use e.Graphics in the paint event. Loading the
bitmap from file each time is more of a issue though. You should store the
bitmap somewhere and reuse it. This might seam wasteful but the bitmap has
to be stored somewhere, if you used a picturebox it would keep the bitmap in
memory.

Michael
Oct 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas | last post by:
Currently we have SQL Server running on a single RAID5 array. The data and the logs are all written to this array. We don't have huge volume of activity, but it is growing. Most activity is from...
17
by: wolfing1 | last post by:
Like, I know if several checkboxes have the same name, I can get the ones that were checked after submit with a request.form("name") and maybe do a split(request.form("name"),",") to cycle through...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
2
by: swesoc | last post by:
Hello Friend, I know this question is been asked many times, if a have base pointer pointing to derived object & if i delete that pointer , if the base class destructor is not virtual. then...
6
by: Tony | last post by:
What happens before main()? Perhaps someone can explain it succintly and perhaps someone has a link where it is described? crt0.lib or something like that comes to mind (surely I have the...
7
by: Nikola Skoric | last post by:
I noticed that System.Windows.Forms.Form constructor treats his parameters as though they are passed by reference even if I omit the "ref" keyword. Why's (and how does he do) that? Shouldn't...
0
by: Lextendo | last post by:
Hi, This is what I want to achieve: I hav a lookup-form used by different parent forms to present a database table. So form X uses the lookup to search table tblA, form Y uses the lookup to...
3
by: raylopez99 | last post by:
Oh, I know, I should have provided complete code in console mode form. But for the rest of you (sorry Jon, just kidding) I have an example of why, once again, you must pick the correct entry point...
2
by: RAVISEKHAR | last post by:
what happens if i call a base class virtual function in a derived class constructor?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.