CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2006
    Posts
    326

    'Me' is not valid within a Module

    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.
    Code:
    
    
    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
    Code:
    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

  2. #2
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 'Me' is not valid within a Module

    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.
    Last edited by Marraco; May 21st, 2008 at 09:24 AM.

  3. #3
    Join Date
    Jan 2006
    Posts
    326

    Re: 'Me' is not valid within a Module

    I changed it as a form. Errors are "Declaration expected". I need help.
    Thanks
    Code:
    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
    Last edited by zhshqzyc; May 21st, 2008 at 10:42 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: 'Me' is not valid within a Module

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 'Me' is not valid within a Module

    ¿Are you utilising the class on this page?

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

    it does not work;
    those lines are broken:
    Code:
    	  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

  6. #6
    Join Date
    Jan 2006
    Posts
    326

    Re: 'Me' is not valid within a Module

    I am using this one and get three errors.

    Code:
    Declaration expected
    SC and MyBitMap

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

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

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: 'Me' is not valid within a Module

    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.


    Code:
        Public Sub Capture()
            _windowDCHandle = User32.GetWindowDC(_windowHandle)
            Dim bitmapHandle As IntPtr = GetBitmapHandle()
            _capturedImage = Image.FromHbitmap(bitmapHandle)
            GDI32.DeleteObject(bitmapHandle)
        End Sub
    Code:
        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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 'Me' is not valid within a Module

    Quote Originally Posted by zhshqzyc
    I am using this one and get three errors.

    Code:
    Declaration expected
    SC and MyBitMap

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

    http://www.vbforums.com/showthread.php?t=385497
    Ok. 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:
    Quote Originally Posted by zhshqzyc
    I changed it as a form. Errors are "Declaration expected". I need help....
    Code:
    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
    
    ...
    _
    Attached Files Attached Files

  9. #9
    Join Date
    Jan 2006
    Posts
    326

    Re: 'Me' is not valid within a Module

    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.
    Code:
    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.

  10. #10
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: 'Me' is not valid within a Module

    Quote Originally Posted by zhshqzyc
    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.
    To open it double click WindowsApplication1.vbproj file
    Quote Originally Posted by zhshqzyc
    ...
    I put it into Main(). It does work but no files generated on C driver.
    Try:
    Code:
    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

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: 'Me' is not valid within a Module

    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 ) :
    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured