Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted
this.OnPaint(null);

"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

Posted

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.

"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

Posted

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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...