WolfmanYoda Posted March 31, 2004 Posted March 31, 2004 I'm working through a book, "Professional C#" from Wrox publishing, and there is a part about displaying an image I'm working on. Here is the OnPaint() code from the book, which works great, scrollbars and all: protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics dc = e.Graphics; dc.ScaleTransform(scaleX, scaleY); dc.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); dc.DrawImage(piccy, piccyBounds); } Love it. Now I wanted to do more with it, so I added my own buttons to zoom in or out of the image. I can get it to resize the image, but if I use the scrollbars it screws up the image. Here is my code: private void btnZoomOut_Click(object sender, System.EventArgs e) { scaleX = scaleX / 2; scaleY = scaleY / 2; ((Control)sender).Parent.Refresh(); } private void btnZoomIn_Click(object sender, System.EventArgs e) { scaleX = scaleX * 2; scaleY = scaleY * 2; ((Control)sender).Parent.Refresh(); } I figure if I add a line to each button_Click to make OnPaint() fire up, it will solve the scroll problem. I've searched(in the book, MSDN, on the net, and here) to no avail on how to make OnPaint() fire. I found some info on Delegates, but I didn't understand the explanation on them. So...can anyone tell me how to make OnPaint() fire? Quote
Arch4ngel Posted March 31, 2004 Posted March 31, 2004 this.OnPaint(null); Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
WolfmanYoda Posted March 31, 2004 Author Posted March 31, 2004 Ahhh...thanks. I tried this.OnPaint() with all kinds of arguments except Null. Man I feel dumb :) Quote
Leaders dynamic_sysop Posted March 31, 2004 Leaders Posted March 31, 2004 you can also use invalidate... private void button1_Click(object sender, System.EventArgs e) { this.Invalidate(); } Quote
Arch4ngel Posted March 31, 2004 Posted March 31, 2004 yup ! and I think that this.Invalidate() is better than my dummy call... Hummmmmm... to tell the truth I don't know which one is best. On my part I prefer OnPaint(null). I'm sure it don't do nothing stupid :D Hehe one way or another the job's done. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
WolfmanYoda Posted March 31, 2004 Author Posted March 31, 2004 I tried them both, I used this.Invalidate(); for my zoom out button and this.OnPaint(null); for my zoom in button. They both work great, I was just about to ask which one is better. Thanks guys :) Quote
*Gurus* divil Posted April 1, 2004 *Gurus* Posted April 1, 2004 You should not call OnPaint(null). If you do, how are you going to actually use the method to paint anything, since you have no PaintEventArgs and therefore no Graphics? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.