# Visual Basic > Visual Basic 6 and Earlier >  [RESOLVED] Capture and Save Animated GIF

## Elroy

Ok, this is an idea I've got, and would like to pursue.

I've searched, and I see most of the pieces, but I'd still like to throw it out here for ideas and possible pitfalls.

Here's an outline of my idea:

Select a section of the screen with a selection box (I can handle that one).Have a small "control" form with the following on it:
Framerate (hertz)."Start" button."Stop" button."Save" button.Once frames are captured (from the selection box), assemble into a GIF and save.

----------


## wqweto

Is this like literally latest thread by baka?

cheers,
</wqw>

----------


## Elroy

> Is this like literally latest thread by baka?
> 
> cheers,
> </wqw>


Ahhh, very good.  I'll study that work and see if I can get to where I'd like to go.

----------


## Elroy

Ok, Trick's GifMakerByTheTrick seems to have all the complex parts worked out.  (Again, thanks go out to The Trick for more fantastic work.)

Input I used for testing into his program:

   

Results/output:



-----------------------------------------------------------------------------------------------

Now, I just need to work out my selected-box-on-screen capture to supply it images.  I think I can handle that part.

----------


## SamOscarBrown

Why am I getting this (for example):




> ' Ôîðìà frmMain.frm - ãðàôè÷åñèêé èíòåðôåéñ è âíóòðèìîäóëüíûå ôóíêöèè' © Êðèâîóñ Àíàòîëèé Àíàòîëüåâè÷ (The trick), 2014


when I open Trick's project (which I just downloaded and unzipped)....those symbols all over the project?  :-(

----------


## Elroy

> Why am I getting this (for example):
> 
> 
> 
> 
> 
> 			
> 				' Ôîðìà frmMain.frm - ãðàôè÷åñèêé èíòåðôåéñ è âíóòðèìîäóëüíûå ôóíêöèè' © Êðèâîóñ Àíàòîëèé Àíàòîëüåâè÷ (The trick), 2014
> 			
> ...


IDK, when I grabbed it (out of the VBForums Codebank), it worked fine for me.  That looks like one of his comments, as they're all in Russian.  But I didn't get that.

FYI, I'm making progress on my "capture" part of it, and hope to have something posted later today.

----------


## SamOscarBrown

Would you mind sending what YOU downloaded?  (Either post here or PM me?)  I downloaded it from the Codebank and it will not run because of those 'words'???.

Yeah, definitely Russian....

----------


## Elroy

While I'm working on capturing & cropping screen portions, maybe someone could answer this:

What's the "*Interval*" and what's the "*threshold*"?

I'm guessing the "*Interval*" is milliseconds between frames, but I'm not sure.

I've really got no idea what "*threshold*" is.  I'm guessing it's something to do with determining transparency, but I've really got no idea.

p.s.  I do know how a GIF palette works ... 256 colors with one indicated as transparency.

----------


## baka

the first image need to be "injected" with information. thats what the propertybag is for.
if u have 10 pictures, u need 10 intervals. so 1 for each.  so u make the array and add 10 to all the values.
in my screen-grabber I use an interval of 100 (timer) so I get 10 pics/sec while "recording"
that is equivalent to the value of 10 in the propertybag.

the other stuff is optimization that I dont use.
it takes too much time and since I want speed I can't afford the user to have the game freeze that long.
but the optimization is making the image look better instead of the on the fly conversion from GDI

I do a shell to gifsicle after Im done, so I let this little tool compress the GIF for me.

----------


## Elroy

Thanks baka.   :Smilie: 

Hmmm, ok, I'm successfully drawing my "capture window", and I'm successfully capturing a "frame" into a picturebox.

But I'm now at a mild impasse.  *How do we take a StdPicture in a PictureBox and create a GDI+ image with it?*  Trick is using GdipLoadImageFromFile, but I'd rather not save it to a file just to load it.  I've also got code to create a GDI+ image from RGBa data, but that's still jumping through quite a few hoops.

*So, do we have something to just go straight from a StdPicture to a GDI+ image?*

Baka, if you're still following, how'd you do it?

----------


## Elroy

> Thanks baka.  
> 
> Hmmm, ok, I'm successfully drawing my "capture window", and I'm successfully capturing a "frame" into a picturebox.
> 
> But I'm now at a mild impasse.  *How do we take a StdPicture in a PictureBox and create a GDI+ image with it?*  Trick is using GdipLoadImageFromFile, but I'd rather not save it to a file just to load it.  I've also got code to create a GDI+ image from RGBa data, but that's still jumping through quite a few hoops.
> 
> *So, do we have something to just go straight from a StdPicture to a GDI+ image?*
> 
> Baka, if you're still following, how'd you do it?


EDIT1: Hmmm, is it just the GdipCreateBitmapFromHBITMAP?  That one always gets by me.

EDIT2: Yep, GdipCreateBitmapFromHBITMAP is working just as I want it to.

----------


## baka

well. u can use STREAM instead of FILE. that way, u can create the whole GIF into a stream. after that u should be able to load the stream? not sure.

anyway, I use:



```
Private Sub AddBitmap()
    Dim hDCMemory           As Long
    Dim hBmp                As Long
    Dim hBmpPrev            As Long

    With Target
        .Count = .Count + 1
        ReDim Preserve .Bitmap(.Count)
        hDCMemory = CreateCompatibleDC(.hdc)
        hBmp = CreateCompatibleBitmap(.hdc, .Width, .Height)
        hBmpPrev = SelectObject(hDCMemory, hBmp)
        BitBlt hDCMemory, 0, 0, .Width, .Height, .hdc, 0, 0, vbSrcCopy
        hBmp = SelectObject(hDCMemory, hBmpPrev)
        DeleteDC hDCMemory
        GdipCreateBitmapFromHBITMAP hBmp, 0, .Bitmap(.Count).Image
        DeleteObject hBmp
    End With
End Sub
```

each time I take a screenshot.
so, here we create a hdc, take the screenshot and convert into image (gdi+)
when Im done recording, I have all the images in my "target" UDT
and from there I do the next part.

----------


## Elroy

Well, I've got mine up and running.  The code is somewhat "thrown together", and I need to clean it up, but it is working.

To test, I just opened Chrome, searched, and set my "crop box" on an animating GIF, and then clicked my "Start Capture".  Let it run for a couple of seconds, then stopped it and saved the file.

Here's what I got, which is about what I was expecting:


(EDIT: I replaced the original, as it was apparently too big for VBForums.  This one was also captured with the attached program.)

I'll also attach my work at where I'm at with it:

----------


## SamOscarBrown

Geez....something wrong on my end, I guess...when I downloaded your project in post 13, I see those Russian words in the class module.  When I try to add an image (GetOpenFile), I get the setting for the parameter to search for a file like this:

----------


## Elroy

Hi Sam,

Ok, there are a lot of Russian comments in the code that were put in by The Trick (as he's Russian).  But my addition is a new "Start Capture" button (which changes to "Stop Capture" when actually capturing).

After you've captured some frames, you can then save the animated GIF to a file with the "Save" button.

But hold on ... I've just made some more tweaks, and I'm going to post it to the CodeBank.  Rather than mess with this thread anymore, wait until I get that posted and grab that one.  I've also deleted the Russian comments, as I can't read them.  I hope The Trick isn't upset by that.

Elroy

----------


## Brenker

I successfully ran a test (with a 600x600 32-BPP animated PNG); I would like to give a thumb up of the code.  

-- Because a query had been raised about the term "threshold", I searched for it in the code and then took a cursory look at Function Conv256().  "Threshold" looks like an "alpha cutoff" to me here, i.e. when the alpha value in the 4th byte is below the cutoff, ignore the RGB values, otherwise take the whole RGB values.  There is no "screen color" to be applied for the alpha.

-- Just for reference there is room for improvement:

(i)   Allow user to modify the individual "intervals", sometimes a smaller value is needed.

(ii)  Provide an option on "Save", i.e. (a) as it is (always full frames) or (b) take frame differentials only (differential between Frame 2 v 1, 3 v 2, 4 v 3 etc),  and provide respective starting offsets [x, y] instead of [0, 0]. Disposal method can be 0.  File size with Option (b) would be smaller.

(iii) Test to see if Global palette can be used, aiming at a reduced file size.

(iv) Optionally allow capture of Mouse Pointer as well.



Edited:  
The following were the buttons I used: "Clear", "Start Capture", "Stop Capture" and "Save".  I clicked "Start Capture" on running the earlier said Animated PNG file in my own program.  The purpose was to capture the processed frames in the animation (till I click "Stop Capture").  Then "Save" to an animated GIF file.   I am not interested in other buttons.

----------

