Question: Saving form as jpeg or gif automatically

  • Thread starter Thread starter VB Programmer
  • Start date Start date
* "VB Programmer said:
This application is going to continuously run on a PC. Which means I would
preferably like it to run in the background so I can do other things. Any
ideas how I can get around it? I tried to "Active" the form before I
captured it, but this doesn't work too well.

You can capture the visible parts of the form only.
 
Is there another sub/function that you know of to capture a control to a jpg
image? I really need the controls to be captured regardless of what
application has the current "focus".
 
Hi VBP,

Here's a way-out solution, and no idea whether it would work. Extend your
desktop in size so that it is bigger than the screen. Then run the app in the
portion that is currently not shown. The trouble is Windows may well optimise
away all the drawing.

What problems did you have with Activate? Was it trying to do the capture
before the Form got to the top and was redrawn? (Answer: add a delay) Or was
it that it was very disruptive? (Answer: Hmmm. Add a bell to warn you and a
delay?). Or something else?

Regards,
Fergus
 
Hi Herfried, VBP,

I tried that one when the topic first came up but I had problems with it -
'The object is currently in use elsewhere' was the complaint. This was on a
Control that did stuff in OnPaint. It works fine on a plain old Button. On a
Panel it gave nothing.

VBP,
If you give it a go, you can replace that entire loop with this. It gets
the OnPaint method directly. There's no need to get them all and weed out the
required one.

Dim OnPaintMethod As MethodInfo
OnPaintMethod = ctl.GetType.GetMethod ("OnPaint", _
BindingFlags.Instance Or BindingFlags.NonPublic)

Regards,
Fergus
 
Your resolution idea is worth a try.

BTW in my timer method I call my "activate" right before I call the
CaptureControl method. It's like this...
Dim MyBitmap As Bitmap
Dim MyForm As frmMain

MyFrmMain.Activate()
MyBitmap = CaptureControl(MyFrmMain.panKnob)

What happens is...
1. I launch the app and it appears.
2. I click on the window behind it.
3. I can see the app in the task bar blinking away. The images captured
are at the correct location, but they capture the active window.

Fergus, you have proved to be a great resource! Thanks a lot!!!
 
I tried this in my Main sub...

Public Sub New(ByVal dblInterval As Double)
MyBase.New(dblInterval)
MyFrmMain.SetDesktopLocation(-200, -200)
MyFrmMain.Show()
End Sub

I tried to force it off the screen, but all the jpgs were black.

I tried to show the form with ShowDialog too, to make it modal, but the
timer code wouldn't run.

Hmmm????
 
Hi VBP,

I was assuming that you'd followed the link that Herfried had given and
got the code for the alternate method.

It 'manually' triggers the OnPaint event for the target Control but passes
in the Graphics context of <your> Bitmap rather than that of the Control. So
the Control is fooled into drawing itself directly onto the Bitmap. There's a
block of code which uses Reflection to find the OnPaint method. My snippet was
just a replacement for that as it does more than is needed.

As I mentioned in my previous post, it works and doesn't work, depending.

It's definitely worth trying out. To save you the hassle, here it is.

Regards,
Fergus

<code>
Public Function PaintControlToBitmap (oControl As Control) As Bitmap
Dim OnPaintMethod As MethodInfo
OnPaintMethod = oControl.GetType.GetMethod ("OnPaint", _
BindingFlags.Instance Or BindingFlags.NonPublic)

If Not OnPaintMethod Is Nothing Then
Dim bmpControl As New Bitmap (oControl.Width, oControl.Height)
Dim gr As Graphics = Graphics.FromImage (bmpControl)
Dim PaintEventArgs As New PaintEventArgs (gr, _
New Rectangle(0, 0, oControl.Width, oControl.Height))
OnPaintMethod.Invoke (oControl, New Object() {PaintEventArgs})
gr.Dispose
Return bmpControl
End If
End Function
</code>
 
Thanks once again.

I ran the code and all that was saved was a black rectangle, even though it
was the correct size.

Hmmmm....
 
Hi VBP,

Thet's wut ah wuz afeard of. Ah gut me a nahce cullerful Butt'n bert
nuthin in mah Panel.

Is there any chance that you could put a small project together that I
could play with?

If you can, and you use VS2002, that should fine. If VS2003, could you add
any graphics as independant jpgs because I can't read v2003 resx files.
(Either way, though, - no bin or obj directories, please)

Regards,
Fergus
 
Back
Top