-
Nov 28th, 2010, 02:39 AM
#1
Thread Starter
Member
Getpixel is not working in vb.net
Look at this code, it's so simple, yet I can't manage to get it to work!! PLEASE HELP ME, I'VE BEEN TRYING TO GET IT TO WORK FOR HOURS!!!!
on the top of form1 is
Code:
Option Strict On
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Code:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Dim TempBmp As Bitmap = New Bitmap(PictureBox1.Image.Size.Width, PictureBox1.Image.Size.Height)
Dim MyColor As Color
TempBmp = System.Drawing.Bitmap.FromHbitmap(PictureBox1.Handle)
MyColor = (TempBmp.GetPixel(1, 1))
Label4.Text = MyColor.ToString
end sub
And the error happens at this line
Code:
Dim TempBmp As Bitmap = New Bitmap(PictureBox1.Image.Size.Width, PictureBox1.Image.Size.Height)
The error message that I get is
Code:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
That's everything and there is a picture loaded (partial screenshot of desktop) yet it does not work. How could this be?
Last edited by replaced; Nov 28th, 2010 at 04:02 AM.
-
Nov 28th, 2010, 04:48 PM
#2
Re: Getpixel is not working in vb.net
Hi replaced, the only explanation for that error can be that PictureBox1.Image didn't exist at the moment you clicked it. Maybe you put the screenshot in the picture box's BackgroundImage instead? BB
-
Nov 28th, 2010, 07:08 PM
#3
Thread Starter
Member
Re: Getpixel is not working in vb.net
Originally Posted by boops boops
Hi replaced, the only explanation for that error can be that PictureBox1.Image didn't exist at the moment you clicked it. Maybe you put the screenshot in the picture box's BackgroundImage instead? BB
It stills does not work if I do
Code:
Dim TempBmp As Bitmap = New Bitmap(PictureBox1.BackgroundImage.Size.Width, PictureBox1.BackgroundImage.Size.Height)
What I do is bitblt the desktop to the picturebox then try and read the pixel using the code i've shown. Anything else?
-
Nov 28th, 2010, 07:20 PM
#4
Re: Getpixel is not working in vb.net
Yes. Save your screen shot to a bitmap. Although then you don't even need a PictureBox because you can use the bitmap's GetPixel method directly.
Blitting to part of the screen that happens to be occupied by a PictureBox doesn't set the Image property or anything else. It just sets the screen pixels.
BB
-
Nov 28th, 2010, 10:14 PM
#5
Thread Starter
Member
Re: Getpixel is not working in vb.net
Originally Posted by boops boops
Yes. Save your screen shot to a bitmap. Although then you don't even need a PictureBox because you can use the bitmap's GetPixel method directly.
Blitting to part of the screen that happens to be occupied by a PictureBox doesn't set the Image property or anything else. It just sets the screen pixels.
BB
So how would I save my screenshot to a bitmap?
Here is my working bitblt code, I would prefer using bitblt to do the actual transfer.
The follow code below successfully transfers an image from the desktop to a picturebox. I would prefer that any bitmap solution would be based off this code (using bitblt at least).
Code:
Imports System.Runtime.InteropServices
Module Module1
<DllImport("user32.dll")> _
Public Function GetDesktopWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function GetDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function GetWindowDC(ByVal ptr As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
End Function
<DllImport("gdi32.dll")> _
Public Function BitBlt(ByVal hdcDest As IntPtr, ByVal xDest As Integer, ByVal yDest As Integer, ByVal wDest As Integer, ByVal hDest As Integer, ByVal hdcSource As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal rop As CopyPixelOperation) As Boolean
End Function
Public Function GetScreenSnapshot()
Dim hwNd As Long
Dim targetDC As Long
Dim picHandle As Long
Dim picHDC As Long
Const vbSRCCOPY As Long = &HCC0020
hwNd = GetDC(GetDesktopWindow)
targetDC = GetWindowDC(hwNd)
picHandle = Form1.PictureBox1.Handle
picHDC = GetDC(picHandle)
BitBlt(picHDC, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, hwNd, 0, 0, vbSRCCOPY)
ReleaseDC(picHandle, picHDC)
ReleaseDC(hwNd, targetDC)
End Function
End Module
-
Nov 29th, 2010, 07:54 AM
#6
Re: Getpixel is not working in vb.net
If you want to use BitBlt, you could study or use screen capture code posted to this forum's Code Bank - for example this project by VBDT or this one by Gigemboy.
All the same, I advise you to use Graphics.CopyFromScreen instead. It only takes about three lines of code, and the claims made that it leaks memory are rubbish. It's true that CopyFromScreen is a bit slower than BitBlt: on my fairly modest PC with a 1680*1050 screen, a full screen capture takes 24 ms. with CopyFromScreen as against 16 ms. for BitBlt. But I don't think you should worry about performance until you are able to get some code working.
BB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|