Click to See Complete Forum and Search --> : 'Me' is not valid within a Module


zhshqzyc
May 21st, 2008, 08:58 AM
Hi,

I use the code below to call another class "ScreenCapture.vb".
But I got a lot of errors.

'Me' is not valid within a Module

Thanks for your help.




Module ModuleSC
Sub Main()
Dim SC As New ScreenShot.ScreenCapture

'grabs image of object handle (in this case, the form)
Dim MyWindow As Image = SC.CaptureWindow(Me.Handle)

'grabs image of entire desktop
Dim MyDesktop As Image = SC.CaptureScreen

'captures entire desktop straight to file
SC.CaptureScreenToFile("c:\desktop2.jpg", Imaging.ImageFormat.Jpeg)

'captures image of object handle (in this case, the form) straight to file
SC.CaptureWindowToFile(Me.Handle, "c:\window2.jpg", Imaging.ImageFormat.Jpeg)

'returns bitmap of region of desktop by passing in a rectangle
Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(Me.Location.X, Me.Location.Y, _
Me.Width, Me.Height), Me.Width, Me.Height)
MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)
'above example gets rectangle of form that it is called in, you can get
'desktop by calling me.hide before the actual function call in order to
'get the desktop that is in the bounds of the form (handy when using a
'transparent Form or control in order to make a "viewfinder" for the capture...)
'You can pass in any rectangle you want, this was so you can see how
'it works...
End Sub
End Module


The other class is briefly listed as

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace ScreenShot
Public Class ScreenCapture
'/ Creates an Image object containing a screen shot of the entire desktop
Public Function CaptureScreen() As Image
Return CaptureWindow(User32.GetDesktopWindow())
End Function 'CaptureScreen
'/ Creates an Image object containing a screen shot of a specific window
Public Function CaptureWindow(ByVal handle As IntPtr) As Image
Dim SRCCOPY As Integer = &HCC0020
' get te hDC of the target window
Dim hdcSrc As IntPtr = User32.GetWindowDC(handle)
'......many lines then

Marraco
May 21st, 2008, 09:22 AM
Dim MyWindow As Image = SC.CaptureWindow(Me.Handle)Modules does not have images. You need to provide the handle of a thing with an image, like a Form, or a PictureBox.

zhshqzyc
May 21st, 2008, 09:47 AM
I changed it as a form. Errors are "Declaration expected". I need help.
Thanks

Imports System.Windows.Forms

Public Class GC
Dim SC As New ScreenShot.ScreenCapture

'grabs image of object handle (in this case, the form)
Dim MyWindow As Image = SC.CaptureWindow(Me.Handle)

'grabs image of entire desktop
Dim MyDesktop As Image = SC.CaptureScreen

'captures entire desktop straight to file
SC.CaptureScreenToFile("c:\desktop2.jpg", Imaging.ImageFormat.Jpeg)'SC

'captures image of object handle (in this case, the form) straight to file
SC.CaptureWindowToFile(Me.Handle, "c:\window2.jpg", Imaging.ImageFormat.Jpeg)'SC

'returns bitmap of region of desktop by passing in a rectangle
Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(Me.Location.X, Me.Location.Y, _
Me.Width, Me.Height), Me.Width, Me.Height)
MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)'MyBitMap
'above example gets rectangle of form that it is called in, you can get
'desktop by calling me.hide before the actual function call in order to
'get the desktop that is in the bounds of the form (handy when using a
'transparent Form or control in order to make a "viewfinder" for the capture...)
'You can pass in any rectangle you want, this was so you can see how
'it works...
End Class

dglienna
May 21st, 2008, 10:41 AM
Should have searched:

http://www.vbdotnetheaven.com/UploadFile/mgold/ScreenCapturingForm04212005081136AM/ScreenCapturingForm.aspx

Marraco
May 21st, 2008, 11:32 AM
¿Are you utilising the class on this page?

http://www.developerfusion.co.uk/show/4630/

it does not work;
those lines are broken:
Private Class GDI32

Public SRCCOPY As Integer = &HCC0020
' BitBlt dwRop parameter
Public Shared<DllImport("gdi32.dll")> _
Function BitBlt(hObject As IntPtr, nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As Integer, hObjectSource As IntPtr, nXSrc As Integer, nYSrc As Integer, dwRop As Integer) As Boolean

Public Shared<DllImport("gdi32.dll")> _
Function CreateCompatibleBitmap(hDC As IntPtr, nWidth As Integer, nHeight As Integer) As IntPtr

Public Shared<DllImport("gdi32.dll")> _
Function CreateCompatibleDC(hDC As IntPtr) As IntPtr

Public Shared<DllImport("gdi32.dll")> _
Function DeleteDC(hDC As IntPtr) As Boolean

Public Shared<DllImport("gdi32.dll")> _
Function DeleteObject(hObject As IntPtr) As Boolean

Public Shared<DllImport("gdi32.dll")> _
Function SelectObject(hDC As IntPtr, hObject As IntPtr) As IntPtr
End Class 'GDI32

zhshqzyc
May 21st, 2008, 12:18 PM
I am using this one and get three errors.

Declaration expected
SC and MyBitMap

Please see my previous thread. Hopefully somebody would assist me.

http://www.vbforums.com/showthread.php?t=385497

dglienna
May 21st, 2008, 12:58 PM
Follow the other link that I posted. If it works, you can go back and figure out whats wrong with your version. I have a version that uses this:
Disposes immediately.


Public Sub Capture()
_windowDCHandle = User32.GetWindowDC(_windowHandle)
Dim bitmapHandle As IntPtr = GetBitmapHandle()
_capturedImage = Image.FromHbitmap(bitmapHandle)
GDI32.DeleteObject(bitmapHandle)
End Sub


Private Function GetBitmapHandle() As IntPtr

Dim windowSize As Size = GetWindowSize()

With windowSize
Dim windowDCDestHandle As IntPtr = GDI32.CreateCompatibleDC(_windowDCHandle)
Dim bitmapHandle As IntPtr = GDI32.CreateCompatibleBitmap(_windowDCHandle, .Width, .Height)
Dim windowOldHandle As IntPtr = GDI32.SelectObject(windowDCDestHandle, bitmapHandle)

GDI32.BitBlt(windowDCDestHandle, 0, 0, .Width, .Height, _windowDCHandle, 0, 0, GDI32.SRCCOPY)
GDI32.SelectObject(windowDCDestHandle, windowOldHandle)
GDI32.DeleteDC(windowDCDestHandle)

Return bitmapHandle
End With
End Function

Marraco
May 21st, 2008, 01:21 PM
I am using this one and get three errors.

Declaration expected
SC and MyBitMap

Please see my previous thread. Hopefully somebody would assist me.

http://www.vbforums.com/showthread.php?t=385497Ok. It works now, with that class.

I had created a new application, copy-pasted your code on it, and it works OK. (no errors raised). See files attached.

By the way, you need to enclose your code into a Sub or Function:
I changed it as a form. Errors are "Declaration expected". I need help....

Imports System.Windows.Forms

Public Class GC
'Do not start running code here!
'This place is for global variables
Dim SC As New ScreenShot.ScreenCapture '<-- global variable in Class GC, not code run

'grabs image of object handle (in this case, the form)
Dim MyWindow As Image = SC.CaptureWindow(Me.Handle)

'grabs image of entire desktop
Dim MyDesktop As Image = SC.CaptureScreen

...


_

zhshqzyc
May 21st, 2008, 01:48 PM
Can you attach it again?
The downloaded file has a format efw. Can't be opened.
I put it into Main(). It does work but no files generated on C driver.

Imports System.Windows.Forms

Public Class Form1
Dim SC As New ScreenShot.ScreenCapture

'grabs image of object handle (in this case, the form)
Dim MyWindow As Image = SC.CaptureWindow(Me.handle)

'grabs image of entire desktop
Dim MyDesktop As Image = SC.CaptureScreen
Sub Main()

'captures entire desktop straight to file
SC.CaptureScreenToFile("c:\desktop2.jpg", Imaging.ImageFormat.Jpeg)

'captures image of object handle (in this case, the form) straight to file
SC.CaptureWindowToFile(Me.Handle, "c:\window2.jpg", Imaging.ImageFormat.Jpeg)

'returns bitmap of region of desktop by passing in a rectangle
Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(Me.Location.X, Me.Location.Y, _
Me.Width, Me.Height), Me.Width, Me.Height)
MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)
'above example gets rectangle of form that it is called in, you can get
'desktop by calling me.hide before the actual function call in order to
'get the desktop that is in the bounds of the form (handy when using a
'transparent Form or control in order to make a "viewfinder" for the capture...)
'You can pass in any rectangle you want, this was so you can see how
'it works...
End Sub
End Class
Thank you so much.

Marraco
May 21st, 2008, 04:44 PM
Can you attach it again?
The downloaded file has a format efw. No .efw file. Is a VB.NET 2008 project file, compressed with winrar (download Winrar from here:http://www.rarlab.com/rar/wrar371.exe), you can also use GNU 7Zip (http://downloads.sourceforge.net/sevenzip/7z457.exe).
To open it double click WindowsApplication1.vbproj file...
I put it into Main(). It does work but no files generated on C driver.
Try:
Imports System.Windows.Forms

Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim SC As New ScreenShot.ScreenCapture

'grabs image of object handle (in this case, the form)
Dim MyWindow As Image = SC.CaptureWindow(Me.handle)

'grabs image of entire desktop
Dim MyDesktop As Image = SC.CaptureScreen

'captures entire desktop straight to file
SC.CaptureScreenToFile("c:\desktop2.jpg", Imaging.ImageFormat.Jpeg)

'captures image of object handle (in this case, the form) straight to file
SC.CaptureWindowToFile(Me.Handle, "c:\window2.jpg", Imaging.ImageFormat.Jpeg)

'returns bitmap of region of desktop by passing in a rectangle
Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(Me.Location.X, Me.Location.Y, _
Me.Width, Me.Height), Me.Width, Me.Height)
MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)
'above example gets rectangle of form that it is called in, you can get
'desktop by calling me.hide before the actual function call in order to
'get the desktop that is in the bounds of the form (handy when using a
'transparent Form or control in order to make a "viewfinder" for the capture...)
'You can pass in any rectangle you want, this was so you can see how
'it works...
End Sub
End Class

HanneSThEGreaT
May 22nd, 2008, 10:23 AM
To get back to the original problem :
'Me' is not valid within a Module

From MSDN, it says:
A keyword related to class instances, such as Me or MyBase, is used inside a module.

This means that Me cannot be used inside a module, because the Module is not a class. What you can do is to create a object of that class ( so, if you used this code in a class called Form1, you should create an object of Form1 ) :
Dim ExampleFormClass As New Form1

Then, instead of using the word Me, you could've used the word ExamplFormClass

Just in case anyone was interested :rolleyes: