Thread: Capturing a form's contents

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    25

    Capturing a form's contents

    I'm creating a drawing program of sorts, and I'm trying to implement undo and redo. Rather than go through the pain of saving everything ever drawn as a trackable object, I'd like to figure out how to grab what the form looks like before something is drawn, save that, and then save what it looks like after the user is done with a mouse stroke.

    Program Starts
    1. The blank canvas is added to the undo stack.
    2. User draws.
    3. Left mouse button lifts
    4. Current state of canvas is added to the undo stack.
    5. User presses 'U'
    6. The stack is popped, and the form's content is changed to the image saved.

    The logic itself makes sense, and should work, but I'm having trouble locating the correct way to capture the content itself. I tried backgroundImage, but that seems to not be affected by what a Graphics object draws on it. Does anyone have any tips or ideas?

    Thanks,
    Shmoo

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Every .net control has a .DrawToBitmap function which saves the current state of the control to a Bitmap object. I guess this is ideal for what you're wanting to do. However I strongly urge you to keep a list of Points rather the Images for your undo history management. Otherwise memory usage will get needlessly high.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Thanks for the tip. I'm not too concerned about memory management. I'm going to limit the size of the two stacks. Also, I'm using generative brushes, so point management would be very cumbersome.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by drshmoo View Post
    Thanks for the tip. I'm not too concerned about memory management.
    The future of programming has never looked so bright...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you're literally drawing on a bitmap, just copy it when the mouse button is pressed down and put it on the stack. To undo, pop the stack and replace the current bitmap with the one you popped.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows form C++ - referencing the form
    By rocketman50 in forum Windows Programming
    Replies: 2
    Last Post: 05-22-2010, 11:58 AM
  2. Open a Form within a Form
    By Coding in forum Windows Programming
    Replies: 3
    Last Post: 01-29-2008, 08:13 PM
  3. Accessing Controls on a different form from one form.
    By Tperry23 in forum C# Programming
    Replies: 7
    Last Post: 06-09-2005, 06:13 PM
  4. Capturing ALT in C
    By johnnyd in forum C Programming
    Replies: 2
    Last Post: 03-10-2002, 10:03 PM
  5. long form of year to short form?
    By bc120 in forum C Programming
    Replies: 2
    Last Post: 12-31-2001, 05:34 PM