By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,683 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,683 IT Pros & Developers. It's quick & easy.

Question: Saving form as jpeg or gif automatically

P: n/a
Briefly, how do you save the current form to a jpeg or gif?

Details: I have a form with a speedometer control on it. It continually
loops through a table in a db that has different current "speeds". I want
to "set" the speedometer to the current speed, then save the form as a
picture, then go to the next one, etc... The end result would be a gif or
jpeg that has a "snapshot" of the gauge. (This would eventually show up on
a aspx webform in an image control.)

Any ideas?

Thanks!
Nov 20 '05 #1
Share this Question
Share on Google+
30 Replies


P: n/a
This link is from an earlier post

<http://groups.google.de/groups?q=gro...=de&lr=&ie=UTF
-8&selm=cuwHx16CCHA.1908%40cpmsftngxa07&rnum=1>

"VB Programmer" <gr*********@go-intech.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
Briefly, how do you save the current form to a jpeg or gif?

Details: I have a form with a speedometer control on it. It continually
loops through a table in a db that has different current "speeds". I want
to "set" the speedometer to the current speed, then save the form as a
picture, then go to the next one, etc... The end result would be a gif or
jpeg that has a "snapshot" of the gauge. (This would eventually show up on a aspx webform in an image control.)

Any ideas?

Thanks!

Nov 20 '05 #2

P: n/a
Hi VBP

The code below will capture the given Control's imagery and return a
BitMap.

You can the use bmp.Save (sFilePath, Imaging.ImageFormat.Whatever)

Credit to Armin for this one - I think I got it from one of his posts
elsewhere.

Regards,
Fergus

<code>
Public Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, _
ByVal ySrc As Integer, _
ByVal dwRop As Integer _
) As Integer

'================================================= ==================
Public Function CaptureControl(ByVal c As Control) As Bitmap

Dim bmp As Bitmap
Dim gDest, gSource As Graphics
Dim hdcSource, hdcDest As IntPtr

bmp = New Bitmap(c.Width, c.Height)

gSource = c.CreateGraphics
Try
gDest = Graphics.FromImage(bmp)
Try
hdcSource = gSource.GetHdc
Try
hdcDest = gDest.GetHdc
Try
BitBlt( _
hdcDest, 0, 0, _
c.Width, c.Height, _
hdcSource, 0, 0, SRCCOPY _
)
Finally
gDest.ReleaseHdc(hdcDest)
End Try
Finally
gSource.ReleaseHdc(hdcSource)
End Try
Finally
gDest.Dispose()
End Try
Finally
gSource.Dispose()
End Try

Return bmp
End Function
</code>
Nov 20 '05 #3

P: n/a
AWESOME! Thanks!
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Hi VBP

The code below will capture the given Control's imagery and return a
BitMap.

You can the use bmp.Save (sFilePath, Imaging.ImageFormat.Whatever)

Credit to Armin for this one - I think I got it from one of his posts
elsewhere.

Regards,
Fergus

<code>
Public Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, _
ByVal ySrc As Integer, _
ByVal dwRop As Integer _
) As Integer

'================================================= ==================
Public Function CaptureControl(ByVal c As Control) As Bitmap

Dim bmp As Bitmap
Dim gDest, gSource As Graphics
Dim hdcSource, hdcDest As IntPtr

bmp = New Bitmap(c.Width, c.Height)

gSource = c.CreateGraphics
Try
gDest = Graphics.FromImage(bmp)
Try
hdcSource = gSource.GetHdc
Try
hdcDest = gDest.GetHdc
Try
BitBlt( _
hdcDest, 0, 0, _
c.Width, c.Height, _
hdcSource, 0, 0, SRCCOPY _
)
Finally
gDest.ReleaseHdc(hdcDest)
End Try
Finally
gSource.ReleaseHdc(hdcSource)
End Try
Finally
gDest.Dispose()
End Try
Finally
gSource.Dispose()
End Try

Return bmp
End Function
</code>

Nov 20 '05 #4

P: n/a
3 quick questions...

I put a button on my winform and tried to call your CaptureControl function
but it kept giving me a "Invalid parameter used" error (unhandled exception
in system.drawing.dll) for the Dim line. Here's the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x As New Bitmap("zzz.bmp")
x = CaptureControl(Me)
End Sub

1. Any ideas? Am I invoking this correctly?
2. Also, it says I have to declare SRCCOPY. Do I just declare it as an
integer?
3. What is BitBlt? I don't see any code? Where is the code?

Thanks.

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Hi VBP

The code below will capture the given Control's imagery and return a
BitMap.

You can the use bmp.Save (sFilePath, Imaging.ImageFormat.Whatever)

Credit to Armin for this one - I think I got it from one of his posts
elsewhere.

Regards,
Fergus

<code>
Public Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, _
ByVal ySrc As Integer, _
ByVal dwRop As Integer _
) As Integer

'================================================= ==================
Public Function CaptureControl(ByVal c As Control) As Bitmap

Dim bmp As Bitmap
Dim gDest, gSource As Graphics
Dim hdcSource, hdcDest As IntPtr

bmp = New Bitmap(c.Width, c.Height)

gSource = c.CreateGraphics
Try
gDest = Graphics.FromImage(bmp)
Try
hdcSource = gSource.GetHdc
Try
hdcDest = gDest.GetHdc
Try
BitBlt( _
hdcDest, 0, 0, _
c.Width, c.Height, _
hdcSource, 0, 0, SRCCOPY _
)
Finally
gDest.ReleaseHdc(hdcDest)
End Try
Finally
gSource.ReleaseHdc(hdcSource)
End Try
Finally
gDest.Dispose()
End Try
Finally
gSource.Dispose()
End Try

Return bmp
End Function
</code>

Nov 20 '05 #5

P: n/a
Hi VBP,

Sorry, I forgot to copy SRCCOPY across.
Const SRCCOPY As Integer = &HCC0020

BitBlt is part of the WinApi, specifically the GDI dll.
This is shown by the declaration:
Public Declare Function BitBlt Lib "gdi32" ( ...) As Integer
The Declare part tells the compiler that there's going to be mention of a
function BitBlt but that the code isn't going to be given at this time. The
Lib "gdi32" part tells it to find the actual routine externally.

Try this.
Dim x As New Bitmap 'Just to say that you want a BitMap
x = CaptureControl(Me)
x.Save ("zzz.bmp", Imaging.ImageFormat.Bmp)

Regards,
Fergus
Nov 20 '05 #6

P: n/a
Thanks. Awesome responses!

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
Hi VBP,

Sorry, I forgot to copy SRCCOPY across.
Const SRCCOPY As Integer = &HCC0020

BitBlt is part of the WinApi, specifically the GDI dll.
This is shown by the declaration:
Public Declare Function BitBlt Lib "gdi32" ( ...) As Integer
The Declare part tells the compiler that there's going to be mention of a function BitBlt but that the code isn't going to be given at this time. The Lib "gdi32" part tells it to find the actual routine externally.

Try this.
Dim x As New Bitmap 'Just to say that you want a BitMap
x = CaptureControl(Me)
x.Save ("zzz.bmp", Imaging.ImageFormat.Bmp)

Regards,
Fergus

Nov 20 '05 #7

P: n/a
"VB Programmer" <gr*********@go-intech.com> scripsit:
Details: I have a form with a speedometer control on it. It continually
loops through a table in a db that has different current "speeds". I want
to "set" the speedometer to the current speed, then save the form as a
picture, then go to the next one, etc... The end result would be a gif or
jpeg that has a "snapshot" of the gauge. (This would eventually show up on
a aspx webform in an image control.)


<http://www.mvps.org/dotnet/dotnet/samples/windowsandforms/downloads/Screenshot.zip>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
"Fergus Cooney" <fi******@tesco.net> scripsit:
function BitBlt but that the code isn't going to be given at this time. The
Lib "gdi32" part tells it to find the actual routine externally.

Try this.
Dim x As New Bitmap 'Just to say that you want a BitMap
I still don't understand why you declare 'x' as 'New Bitmap'.
x = CaptureControl(Me)
x.Save ("zzz.bmp", Imaging.ImageFormat.Bmp)


--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #9

P: n/a
Thank you. I appreciate your help.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
"VB Programmer" <gr*********@go-intech.com> scripsit:
Details: I have a form with a speedometer control on it. It continually loops through a table in a db that has different current "speeds". I want to "set" the speedometer to the current speed, then save the form as a
picture, then go to the next one, etc... The end result would be a gif or jpeg that has a "snapshot" of the gauge. (This would eventually show up on a aspx webform in an image control.)

<http://www.mvps.org/dotnet/dotnet/sa...nloads/Screens
hot.zip>
--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #10

P: n/a
Hi Herfried,

Got to keep that garbage collector on its toes!! ;-)

VBP had this.
Dim x As New Bitmap("zzz.bmp")

I took off the (..) but forgot to remove the New too.

Regards,
Fergus
Nov 20 '05 #11

P: n/a
Something weird is happening. It captures whatever is directly BEHIND the
control, rather than the control itself. For example, if I launch it using
Windows Explorer a jpg will be create but it will be of a portion of
Explorer (directly behind the control, and the same size)!

Any ideas?

Here's how I call the capture code (just test code)...
Dim MyBitmap As Bitmap
MyBitmap = CaptureControl(grpGauge)
MyBitmap.Save("c:\zzzz.jpeg", Imaging.ImageFormat.Jpeg)

Here is the CaptureControl code...
Public Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, _
ByVal ySrc As Integer, _
ByVal dwRop As Integer _
) As Integer

Public Function CaptureControl(ByVal c As Control) As Bitmap
Dim bmp As Bitmap
Dim gDest, gSource As Graphics
Dim hdcSource, hdcDest As IntPtr
Const SRCCOPY As Integer = &HCC0020
bmp = New Bitmap(c.Width, c.Height)

gSource = c.CreateGraphics
Try
gDest = Graphics.FromImage(bmp)
Try
hdcSource = gSource.GetHdc
Try
hdcDest = gDest.GetHdc
Try
BitBlt( _
hdcDest, 0, 0, _
c.Width, c.Height, _
hdcSource, 0, 0, SRCCOPY _
)
Finally
gDest.ReleaseHdc(hdcDest)
End Try
Finally
gSource.ReleaseHdc(hdcSource)
End Try
Finally
gDest.Dispose()
End Try
Finally
gSource.Dispose()
End Try
Return bmp
End Function

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
Hi VBP,

Sorry, I forgot to copy SRCCOPY across.
Const SRCCOPY As Integer = &HCC0020

BitBlt is part of the WinApi, specifically the GDI dll.
This is shown by the declaration:
Public Declare Function BitBlt Lib "gdi32" ( ...) As Integer
The Declare part tells the compiler that there's going to be mention of a function BitBlt but that the code isn't going to be given at this time. The Lib "gdi32" part tells it to find the actual routine externally.

Try this.
Dim x As New Bitmap 'Just to say that you want a BitMap
x = CaptureControl(Me)
x.Save ("zzz.bmp", Imaging.ImageFormat.Bmp)

Regards,
Fergus

Nov 20 '05 #12

P: n/a
NM I think the problem is that the first set of code was in Form_load rather
than being called from a button. When I did this it worked. Don't
understand it yet, but, oh well... it works!

"VB Programmer" <gr*********@go-intech.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Something weird is happening. It captures whatever is directly BEHIND the
control, rather than the control itself. For example, if I launch it using Windows Explorer a jpg will be create but it will be of a portion of
Explorer (directly behind the control, and the same size)!

Any ideas?

Here's how I call the capture code (just test code)...
Dim MyBitmap As Bitmap
MyBitmap = CaptureControl(grpGauge)
MyBitmap.Save("c:\zzzz.jpeg", Imaging.ImageFormat.Jpeg)

Here is the CaptureControl code...
Public Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, _
ByVal ySrc As Integer, _
ByVal dwRop As Integer _
) As Integer

Public Function CaptureControl(ByVal c As Control) As Bitmap
Dim bmp As Bitmap
Dim gDest, gSource As Graphics
Dim hdcSource, hdcDest As IntPtr
Const SRCCOPY As Integer = &HCC0020
bmp = New Bitmap(c.Width, c.Height)

gSource = c.CreateGraphics
Try
gDest = Graphics.FromImage(bmp)
Try
hdcSource = gSource.GetHdc
Try
hdcDest = gDest.GetHdc
Try
BitBlt( _
hdcDest, 0, 0, _
c.Width, c.Height, _
hdcSource, 0, 0, SRCCOPY _
)
Finally
gDest.ReleaseHdc(hdcDest)
End Try
Finally
gSource.ReleaseHdc(hdcSource)
End Try
Finally
gDest.Dispose()
End Try
Finally
gSource.Dispose()
End Try
Return bmp
End Function

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
Hi VBP,

Sorry, I forgot to copy SRCCOPY across.
Const SRCCOPY As Integer = &HCC0020

BitBlt is part of the WinApi, specifically the GDI dll.
This is shown by the declaration:
Public Declare Function BitBlt Lib "gdi32" ( ...) As Integer
The Declare part tells the compiler that there's going to be mention

of a
function BitBlt but that the code isn't going to be given at this time.

The
Lib "gdi32" part tells it to find the actual routine externally.

Try this.
Dim x As New Bitmap 'Just to say that you want a BitMap
x = CaptureControl(Me)
x.Save ("zzz.bmp", Imaging.ImageFormat.Bmp)

Regards,
Fergus


Nov 20 '05 #13

P: n/a
Hi VBP,

There will only be background visible during Form_Load because the Form
hasn't been shown yet. This happens when Form_Load returns. You can, however,
call Me.Show explicitly within Form_Load - and then do your capture. You might
have to add a Me.Refresh too, I haven't tested it.

Regards,
Fergus
Nov 20 '05 #14

P: n/a
* "VB Programmer" <gr*********@go-intech.com> scripsit:
NM I think the problem is that the first set of code was in Form_load rather
than being called from a button. When I did this it worked. Don't
understand it yet, but, oh well... it works!


You can only capture visible parts of the window. If the window isn't
visible ('Form_Load'), it won't be captured.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #15

P: n/a
Cor
This is the first bitmap sample that goes to my ofline HKW brainconnector is
it OK?
Nov 20 '05 #16

P: n/a
* "Cor" <no*@non.com> scripsit:
This is the first bitmap sample that goes to my ofline HKW brainconnector is
it OK?


;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #17

P: n/a
Quick question....

This seems to work well if the form has the focus. But, if I switch to
another application the images that are captured are in the correct
"location", but the picture is of whatever application has the focus. Any
ideas on how to FORCE the capture on the correct form?

Thanks!

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
Hi VBP,

Sorry, I forgot to copy SRCCOPY across.
Const SRCCOPY As Integer = &HCC0020

BitBlt is part of the WinApi, specifically the GDI dll.
This is shown by the declaration:
Public Declare Function BitBlt Lib "gdi32" ( ...) As Integer
The Declare part tells the compiler that there's going to be mention of a function BitBlt but that the code isn't going to be given at this time. The Lib "gdi32" part tells it to find the actual routine externally.

Try this.
Dim x As New Bitmap 'Just to say that you want a BitMap
x = CaptureControl(Me)
x.Save ("zzz.bmp", Imaging.ImageFormat.Bmp)

Regards,
Fergus

Nov 20 '05 #18

P: n/a
Hi VBP,

Do you mean that the 'focus' application (or some other window) is lying
across the capture area and thus getting picked up?

I believe that this capture works by going to the screen bitmap, so it
doesn't actually care about who's who and what's what. I think it's just a
dumb 'Gimme a Rect, I'll give you some bits' type of operator.

I imagine that you'll have to get that target window out from under before
doing the capture.

Let me know if I'm wrong though, this is the kind of thing where that
would be useful. ;-)

Regards,
Fergus
Nov 20 '05 #19

P: n/a
You are exactly right Fergus.

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.

Thanks!!!!

"Fergus Cooney" <fi*****@post.com> wrote in message
news:u9**************@tk2msftngp13.phx.gbl...
Hi VBP,

Do you mean that the 'focus' application (or some other window) is lying across the capture area and thus getting picked up?

I believe that this capture works by going to the screen bitmap, so it
doesn't actually care about who's who and what's what. I think it's just a
dumb 'Gimme a Rect, I'll give you some bits' type of operator.

I imagine that you'll have to get that target window out from under before doing the capture.

Let me know if I'm wrong though, this is the kind of thing where that
would be useful. ;-)

Regards,
Fergus

Nov 20 '05 #20

P: n/a
* "VB Programmer" <gr*********@go-intech.com> scripsit:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #21

P: n/a
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".

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bn************@ID-208219.news.uni-berlin.de...
* "VB Programmer" <gr*********@go-intech.com> scripsit:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #22

P: n/a
* "VB Programmer" <gr*********@go-intech.com> scripsit:
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".


<http://www.google.com/groups?selm=sQVN4qGXDHA.2000%40cpmsftngxa06.phx.gb l>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #23

P: n/a
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
Nov 20 '05 #24

P: n/a
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
Nov 20 '05 #25

P: n/a
I'm sorry Fergus, but you just lost me. :(

How can your example help me? (Sorry.)

"Fergus Cooney" <fi*****@post.com> wrote in message
news:uV**************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #26

P: n/a
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!!!

"Fergus Cooney" <fi*****@post.com> wrote in message
news:O2*************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #27

P: n/a
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????

"VB Programmer" <gr*********@go-intech.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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!!!

"Fergus Cooney" <fi*****@post.com> wrote in message
news:O2*************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #28

P: n/a
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>
Nov 20 '05 #29

P: n/a
Thanks once again.

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

Hmmmm....

"Fergus Cooney" <fi*****@post.com> wrote in message
news:u3*************@TK2MSFTNGP10.phx.gbl...
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>

Nov 20 '05 #30

P: n/a
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
Nov 20 '05 #31

This discussion thread is closed

Replies have been disabled for this discussion.