NullReference Fehler

P

Peter Hansch

Hallo,

habe ein VS02 Projekt konvertiert auf VS05. Es handelt sich um eine Klasse
zur erzeugung eines Screenshots
Dabei tritt oben genannter Fehler auf an.

Und zwar bei markierter Stelle:

Option Explicit On
Option Strict On
Option Compare Binary

Imports System
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form

Private m_ssgen As ScreenshotGenerator

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.PictureBox1.Image = m_ssgen.Capture() '// hier solle man zuerst
auf NULL prüfen, Stelle des Fehlers //*****************
End Sub



Die Klasse sieht so aus:

Option Explicit On
Option Strict On
Option Compare Binary

Imports System
Imports System.Drawing

Public Class ScreenshotGenerator

Private Declare Function GetDesktopWindow Lib "user32.dll" () As IntPtr

Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr) As IntPtr

Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hdc As IntPtr) As Int32

Private Declare Function GetWindowRect Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByRef lpRect As RECT) As Int32

Private Declare Function ScreenToClient Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByRef lpPoint As POINTAPI) As Int32

Private Structure POINTAPI
Public x As Int32
Public y As Int32
End Structure

Private Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure

Private Declare Function StretchBlt Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal x As Int32, _
ByVal y As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Int32, _
ByVal ySrc As Int32, _
ByVal nSrcWidth As Int32, _
ByVal nSrcHeight As Int32, _
ByVal dwRop As Int32) As Int32

Private Const SRCCOPY As Int32 = &HCC0020

Private m_intptrWindow As IntPtr

' <summary>
' Gibt das Handle des zu fotografierenden
' Fensters an oder gibt es zurück. Der Wert 0 gibt
' an, dass der gesamte Desktop eingefangen werden soll.
' </summary>
' <value>Handle des zu fotografierenden Fensters.</value>
Public Property Window() As IntPtr
Get
Return m_intptrWindow
End Get
Set(ByVal Value As IntPtr)
m_intptrWindow = Value
End Set
End Property

' <summary>
' Gibt eine Bitmap in Grösse des in der
' Eigenschaft <c>Window</c> angegebenen Fensters mit
' dessen Inhalt zurück.
' </summary>
' <returns>Bitmap, die ein Bildschirmfoto
' eines Fensters enthält.</returns>
Public Function Capture() As Bitmap
Dim hWndWindowToCapture As IntPtr
If Me.Window.Equals(IntPtr.Zero) Then

' Handle auf Desktop ermitteln.
hWndWindowToCapture = GetDesktopWindow()
Else
hWndWindowToCapture = Me.Window
End If

' Rechteck mit Massen des Desktops ermitteln.
Dim rct As RECT
GetWindowRect(hWndWindowToCapture, rct)

Dim intWidth As Int32 = rct.Right - rct.Left
Dim intHeight As Int32 = rct.Bottom - rct.Top

' Koordinaten in Client-Koordinaten transformieren.
Dim pt As POINTAPI
pt.y = rct.Top
pt.x = rct.Left
ScreenToClient(hWndWindowToCapture, pt)
rct.Left = pt.x
rct.Top = pt.y

' Erstellen einer Bitmap, die den Screenshot
' aufnehmen kann (in Grösse des Desktops).
Dim b As Bitmap = New Bitmap(intWidth, intHeight)

' Erstellen eines Graphics-Objekts zur Bitmap,
' um in sie zu zeichnen.
Dim g As Graphics = Graphics.FromImage(b)

' Erstellen eines Handles auf den Device
' Context des Desktops.
Dim hdcWindow As IntPtr = GetDC(hWndWindowToCapture)

' Ermitteln eines Handles auf den Device
' Context des Graphics-Objekts
' zur Ziel-Bitmap.
Dim hdc As IntPtr = g.GetHdc()

' Zeichnen des Inhalts des Desktop-DCs
' in den DC der Bitmap.
StretchBlt( _
hdc, _
0, _
0, _
intWidth, _
intHeight, _
hdcWindow, _
rct.Left, _
rct.Top, _
intWidth, _
intHeight, _
SRCCOPY _
)

' Handles freigeben.
ReleaseDC(hWndWindowToCapture, hdcWindow)
g.ReleaseHdc(hdc)

' Erstellte Grafik zurückgeben.
Return b
End Function
End Class


Vielleicht könnt ihr mir helfen das wieder lauffähig zu bekommen.

Herzlichen Dank und Grüßle!
 
P

Peter Hansch

Habs rausgefunden, hab einfach ein:
Private m_ssgen As New ScreenshotGenerator

draus gemacht!
 
R

RobinS

Um, you might want to post to a *German* version of this newsgroup to get a
quicker response. This one is in English. Although I'm sure there's someone
around who speaks German...

Robin S.
 
H

Herfried K. Wagner [MVP]

RobinS said:
Um, you might want to post to a *German* version of this newsgroup to get
a quicker response. This one is in English. Although I'm sure there's
someone around who speaks German...

I agree. The more appropriate newsgroup is
"microsoft.public.de.german.entwickler.dotnet.vb".
 
M

Mythran

Herfried K. Wagner said:
I agree. The more appropriate newsgroup is
"microsoft.public.de.german.entwickler.dotnet.vb".

Do I want to know what entwickler means?

Mythran
 
P

Peter Hansch

I am really sorry! Please Excuse me.
From now on I will post here in english or in the appropriate german
newsgroup.
thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top