Results 1 to 4 of 4

Thread: Expand Graphics Object Bounds ?

  1. #1
    ALX
    ALX is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2005
    Location
    Columbia, SC
    Posts
    187
    Reputation
    143

    Expand Graphics Object Bounds ?

    I have a Graphics clipping issue that I can't seem to figure out on my own, here... On a form that uses "Me.CreateGraphics" to draw a bargraph to the form, I want to create a graphics object in the Form.Load sub and use that same graphics object throughout the entire class. If the user starts the form at its smallest size and then resizes to a larger size, the graphics are clipped to what the form's size was when the graphics object was created. I could create a new graphics object every time the graph needs to be redrawn or resized to solve this, but it seems like a waste and I'm not entirely sure I'm not leaking memory resources by doing that. I've tried "graphics.SetClip" in an attempt to expand the bounds of the graphics object without success. Any insights ???

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,778
    Reputation
    1703
    I didn't read your post past the half-way mark in your second sentence because what you're trying to do is just wrong in the first place. Do not EVER call CreateGraphics. It is unfortunate that a lot of GDI+ examples on the web do so for simplicity without mentioning that it is for simplicity and that it should NEVER be done in practice. If you want to draw on a form or other control then you do so on its Paint event only. That means overriding the OnPaint method inside the object or handling the Paint event outside the object. In the case of a form, you can do either. You put all your drawing code inside the OnPaint method or Paint event handler and then, whenever you want to change what's drawn, you call Refresh or Invalidate and Update. Here are a few examples of mine:

    Very Simple Drawing Program
    Manipulating GDI+ Drawings
    Draw Common "Picture" on All Controls

  3. #3
    ALX
    ALX is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2005
    Location
    Columbia, SC
    Posts
    187
    Reputation
    143
    Thanks for the reply JM, and in theory, I agree with your assessment. I have other form classes that override the paint event to handle the graphics, but on this particular form I had issues with flickering that Double Buffering wasn't suppressing as the process goes through various records and updates the graph as the data is processed. By using the Paint Event, the page is cleared prior to repainting and that creates the flicker. I found that updating the graph manually produced the kind of results I was looking for and that's why I chose to use the "CreateGraphics" function here. This way I can alter the graph without first clearing the page. In spite of the unconventional use of this function, I'm still trying to find a way to make this happen. If the user starts the form class in a maximized state, then everything works fine. I'm thinking, maybe I need to set the graphics object to it's maximum size when it is created... Not too sure how to do that, tho...

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,080
    Reputation
    2802
    Quote Originally Posted by ALX View Post
    By using the Paint Event, the page is cleared prior to repainting and that creates the flicker.
    You can invalidate only the parts you want to paint.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •