I need an unlimited undo/redo code for my app...
i've found a code for RichTextBoxes but it didnt support
pictureboxes so i relly need som help with this! ;)
Printable View
I need an unlimited undo/redo code for my app...
i've found a code for RichTextBoxes but it didnt support
pictureboxes so i relly need som help with this! ;)
It's a painting program, right? You could just save the picture to an array of invisible Image controls, but it would consume a lot of memory if you have a large number of undos :)
yeah...i know that i can do that but i just need a code example;)
Well, right now I don't have time to code that to you :)
But I can tell you how...
VB Code:
'Use this to load a control Load imgUndo(index) 'Use this to unload a control UnLoad imgUndo(index)
All you need is imgUndo(0) already in the form (invisible and without any picture).
* Before the user draws something, load another Image control into the array and copy the image that is on the screen to it.
* To undo, just copy the image of the last control to the image that is on screen, and unload that control.
Hope that helped ;)
thanks....ill try that later :)
You could create a RECT and a smaller picturebox, and just put in it what changed. Eg. If a 30x30 area at 0,0 was changed...
RECT.Right = 29
RECT.Bottom = 29
RECT.Top = 0
RECT.Left = 0
Picturebox.Picture = 0,0 -> 29,29 region
Yeah that's a good idea if you're not a begginer ;)
ok... i'll try that when i've got some free time...:)
GDI provides many graphical tools, including unlimited undo/redo stack functionality, you can use SaveDC to push the current state of the DC, that is after you perform a change and RestoreDC when you want to Redo, that is Pop state from the stack, and you can choose how many steps you want to backtrack as well :)
that sounds realy cool!
im a bit new on vb so can you tell me just a little bit how to do it?
that would be great!:)
Hey kedaman, Will 18 billion people even be alive during my lifetime? =P
Z.
Zaei, Maybe not at the same time :p
cyborg, I was wrong, SaveDC and RestoreDC saves the state information of all graphical objects attached to DC's, which i mistoken for saving the whole objects, so I guess you wont have use for them. You could though save a lot of handles if you use GDI only to develope your graphics.
But, that doesn't stop you from doing it, you could use a imagelist and add upp the images as you do changes to it, then retrieve them when you undo.
You don't need an image list, an array of DCs will do just fine :)
What about saving them into a temp file?? instead of an array?? and load from the temp file.
Waste of resources :pQuote:
Originally posted by Jotaf98
You don't need an image list, an array of DCs will do just fine :)
That would take some time to save and loadQuote:
What about saving them into a temp file?? instead of an array?? and load from the temp file.
Well, how does photoshop do it without using up alot of memory space???? I always wonder... maybe it saves into temp file????
Dunno, I don't have photoshop, I use PSP, which I noticed stores stuff on the harddrive, but not everything, just what it is enough down the stack.Quote:
Originally posted by MoMad
Well, how does photoshop do it without using up alot of memory space???? I always wonder... maybe it saves into temp file????
Yeah, like, when the stack is using about 10mb or something, it saves older images to the hard drive so they can be accessed later :)
I use phoshop often and it amazes me how i can open 10 images of 100+MB each and edit them, do filters, selections, etc... without even taking up any more memory than it had at start-up.
So i figured the only way this happens is that photoshop saves it on the HD, how else could it handle such tremendous amount of graphics????
And performance/speed is not affected at all.
maybe you can load the data into a struct, and save an array of that struct into a file and load one struct at a time instead of the whole array.
Something like random access files?
But that is slow, so you'll need to make a dll that uses the C-style of file processing.Code:'global
' a whole bunch of api calls go here
' you also need a class for handling the undo file stuff
dim undoFile as new clsUndoHandler
sub editImg_onChange()
Dim temp as udtImage
static theid as integer
temp.id = theid
temp.fid = currentImg
temp.imagedata = getImgData(editImg(currentImg).hdc)
temp.hdc = editImg(currentImg).hdc
'...
' basically append adds to the file.
undoFile.Append(temp)
theid = theid + 1 ' increment the id
end sub
-that is just off my head, ud have to use it as an idea.PHP Code:// append
__declspec(dllexport) void _stdcall append(const udtUndo * temp)
{
FILE* f = NULL;
udtUndo s;
char fnom[128];
sprintf(fnom, "~undo%d.dat", udtUndo->fid);
f = fopen(fnom, "a+");
fwrite(temp, 1, sizeof(s), f); // only append one
fclose(f);
}
// load
__declspec(dllexport) udtUndo _stdcall load(int fid, int id)
{
FILE* f = NULL;
udtUndo s;
char fnom[128];
sprintf(fnom, "~undo%d.dat", fid);
f = fopen(fnom, "r");
fread(s, id*sizeof(s), sizeof(s), f); // only append one
fclose(f);
return utdUndo;
}
Yeah, that should work :)
Maybe those programs save just something like "did a blur filter in the whole image" (in variables, not text :D ) and they "invert" the algorythm to get the image back :)
Of course, in some cases, like opaque lines, it would still have to save some pixels (eg.: the pixels below the line)... but it would use just a few bytes in memory instead of saving a big image :)
Yes EXACTLY, each effect has its own (undo) algorythm. So basically, u just save the effects that u used in the order that u used them... and if you used something like paint or select or whatevere... you just say so and save the dimensions and sthe location of that thingie. Its not really that hard unless you want to complicate urself :) Photoshop now has unlimited undo/redo, and infact, i found out that it creates a file for undo/redo history. That is a fact.
i've tried this.....but then i call StoreUndo it gives me the
"Object variable or With block variable not set" error!
Why?
Can it be fixed somehow?
Here's the code:
Dim UndoCount As Integer
Private Type UndoStack
UndoPicture As PictureBox
End Type
Dim Undoer(1000) As UndoStack
Private Sub Undo()
UndoCount = UndoCount - 1
Picture1.Picture = Undoer(UndoCount).UndoPicture.Picture
End Sub
Private Sub StoreUndo()
UndoCount = UndoCount + 1
Undoer(UndoCount).UndoPicture.Picture = Picture1.Image
End Sub
Whoa!! Its not that simple, you have to put all of the changes in an array, for example, if your program is a line drawing program, you simply put every line on the stack and when someone does an undo, you just decrement the array count and redraw. simple? Well in something more complex like a drawing program, you have to do something much more complex.
What ur doing is not very good, you cant just save the entire picture into memory, instead, just the parts that changed.
To make a long story short, well this is a long story and i cant really make it short, but i can tell you it will take a lot more than just a few lines to accomplish such a task.
I think the best choice would be the invert algorithm technique and then for say the pencil tool u could just write to the file something like: Pset (X,Y)Color where x is x, y is y, and color is the original color. Ofcourse vb wont just read it and do it. You would have to have some kind of engine setup to use the invert algorithms for whatever the value is. You run into the problem when they do a gradient....
hmmm....maybe i could store each pixel thats been changed and the color value of it and then the undo code could just put those pixels back...but that would use pretty much memory too...
its just that im pretty new in programming vb and i just want to do a simple paint program...
if the whole picture is stored in the memory every time the user changes something and i limit the undos to say 10 times..that wouldn't use so much memory...
is there any simple way to do that?
I've made a code that erases the drawn lines, but theres some problems....
it just erases the last line! i have no idea why!
i've attached the code here, so please have a look!
i've named the variables pretty strange but i think you'll know how they work :P