VB 2005 equivalent of VB 6 PictureBox.Point, VB 2005 generalconfusion.

J

jason.cipriani

I am trying to get a pixel value from a picture box in VB 2005. I know
how to do it in VB 6:

clr& = Picture1.Point(x, y)

I can't figure out how to do it in 2005. Even the VB 6 import wizard
in VB 2005 can't seem to figure it out. The documentation says this:

"In Visual Basic 2005, the Point method no longer exists. You can use
the M:System.Drawing.Bitmap.GetPixel(System.Int32,System.Int32) method
to retrieve a color value from a bitmap."

And to that I say... "M:System.Drawing.Bitmap.GetPixel"? What? What is
that "M:", it appears to be invalid syntax? And System.Drawing.Bitmap
doesn't even seem to have a method named GetPixel... at least
according to auto-complete. And I see that that function takes two
Int32's, which I'm guessing are X and Y coordinates... but what does
that have to do with my picture box?

I noticed my PictureBox has an "Image" property, but that does not
have a GetPixel member either, and in general I'm unsure how any of
this is connected to "M:System.Drawing.Bitmap.GetPixel".

How do I do this? The documentation is frustrating... and VB 2005 in
general is giving me a lot of problems; it seems like everything I do
in VB 2005 requires double the amount of code it required in VB 6...
is this normal? I have had VB 2005 Express installed on my machine for
a while, and I keep meaning to learn it; but every time I need to put
something together with VB I end up using VB 6 since I already know
how to use it. Now I'm trying to just bite the bullet and do it, but
I'm having a ton of trouble making the transition. I can do everything
I want in VB 6, it's like it's directly connected to my brain, but
every time I start up VB 2005 I seem to end up fighting with it then
giving up.

Thanks,
Jason
 
A

Armin Zingler

I am trying to get a pixel value from a picture box in VB 2005. I
know how to do it in VB 6:

clr& = Picture1.Point(x, y)

I can't figure out how to do it in 2005. Even the VB 6 import wizard
in VB 2005 can't seem to figure it out. The documentation says this:

"In Visual Basic 2005, the Point method no longer exists. You can
use the M:System.Drawing.Bitmap.GetPixel(System.Int32,System.Int32)
method to retrieve a color value from a bitmap."

And to that I say... "M:System.Drawing.Bitmap.GetPixel"? What? What
is that "M:", it appears to be invalid syntax?

Probably an error on the page. Forget the "M:"
And
System.Drawing.Bitmap doesn't even seem to have a method named
GetPixel...

Hmm,,, but it's there:

"
Public Function GetPixel(ByVal x As Integer, ByVal y As Integer) As
System.Drawing.Color

Member of System.Drawing.Bitmap
"

It's an instance method not a shared method, if you expected this.
at least according to auto-complete. And I see that that
function takes two Int32's, which I'm guessing are X and Y
coordinates... but what does that have to do with my picture box?

Retrieving the pixel from the screen like VB6 did it is not recommended
because reading video RAM is very slow. In addition, data layer should
be separated from presentation layer. In this case it means, the data is
a Bitmap, the presentation (container) is the picturebox on the screen.
So, to work with the data, including retrieving pixel data, should be
done in the data layer, which means calling the GetPixel method on a
Bitmap object.
I noticed my PictureBox has an "Image" property, but that does not
have a GetPixel member either, and in general I'm unsure how any of
this is connected to "M:System.Drawing.Bitmap.GetPixel".

How do I do this?

What do you display in the Picturebox? You are probably painting in it's
Paint event, right? Instead of doing this, paint on a Bitmap and have
the Picturebox display the Bitmap by assigning the Bitmap to the
Picturebox' Image property. To paint on a Bitmap:

dim g as graphics
dim bmp as new bitmap(100,50))

g = graphics.fromimage(bmp)

g.drawrectangle
g.drawstring
g.DrawPie

g.dispose


Of course, you don't have to create the Bitmap each time you paint.
Create it once.

The documentation is frustrating... and VB 2005 in
general is giving me a lot of problems; it seems like everything I
do in VB 2005 requires double the amount of code it required in VB
6... is this normal?

No. At the end of the day, you have to write *much* *less* code in
VB.Net.
I have had VB 2005 Express installed on my
machine for a while, and I keep meaning to learn it; but every time
I need to put something together with VB I end up using VB 6 since I
already know how to use it. Now I'm trying to just bite the bullet
and do it, but I'm having a ton of trouble making the transition. I
can do everything I want in VB 6, it's like it's directly connected
to my brain, but every time I start up VB 2005 I seem to end up
fighting with it then giving up.

Once you got it, you will love it. Really.


Armin
 
A

Aaron Smith

Armin Zingler said:
Once you got it, you will love it. Really.


Armin

I agree. I felt the same way when I started converting a major application
to Vb.Net 2003... I ended up fighting everything just to do things like I
*used* to do them in VB6. I broke down and bought a good book... I haven't
looked back since.

Aaron
 
J

jason.cipriani

Thanks for the response, guys.

Hmm,,, but it's there:

Public Function GetPixel(ByVal x As Integer, ByVal y As Integer) As
System.Drawing.Color

It's an instance method not a shared method, if you expected this.

That must be what I was (incorrectly) expecting.
Retrieving the pixel from the screen like VB6 did it is not recommended
because reading video RAM is very slow. In addition, data layer should
be separated from presentation layer. In this case it means, the data is
a Bitmap, the presentation (container) is the picturebox on the screen.
So, to work with the data, including retrieving pixel data, should be
done in the data layer, which means calling the GetPixel method on a
Bitmap object.

I'm not interested in performance or proper software design, in this
case. Here, I am trying to use VB to quickly perform certain tasks,
one-shot programs that I don't even bother saving (for example, load
an image, remove saturation, save image, or other various simple image
processing tasks), but I also like to visualize what is happening on
the screen rather than doing everything in an offscreen bitmap. For
VB6 it was convenient, and exactly what I wanted, to use PictureBox's
Point and PSet.

How do I get a Bitmap object from a VB2005 PictureBox...? That is the
connection I was having trouble seeing. How can I get something from a
PictureBox that I can call GetPixel on?
What do you display in the Picturebox? You are probably painting in it's
Paint event, right? Instead of doing this, paint on a Bitmap and have
the Picturebox display the Bitmap by assigning the Bitmap to the
Picturebox' Image property. To paint on a Bitmap:

I am loading a picture from a file (by setting the picture property at
design-time), operating on it and drawing it back to the picturebox,
and then (in VB6 at least), I could use SavePicture (IIRC, sorry I
don't have VB6 open to verify right now) if I wanted to save the
modified image back out (or write out whatever other data I was trying
to generate with other code).

So do you recommend, as the best way to do all this, doing all work
(Point and PSet and anything else) in a Bitmap and displaying it in
the PictureBox? It's handy being able to browse to the bitmap file I
want to work with graphically at design time (through the property
editor on a PictureBox) rather than having to type lines of code to do
it. But I guess typing out the path to the file in code like (not real
VB code): MyBitmap = LoadBitmapFromFile("c:\documents and settings
\jason\my documents\my pictures\images\whatever\sure\bitmap.bmp" is a
small sacrifice to make, although it's a little inconvenient
sometimes.

I think my problem is my typical usage of VB is not very... typical. I
use it as a really quick prototyping tool, or a tool to perform some
tasks really, really quickly. A lot of one-shot stuff. To me it serves
a similar purpose to, say, a shell script or batch file -- a great
tool for automating a simple one-time task. So I become overly
frustrated when I have to start doing tedious things or worry about
actual good software design practices when I'm working with these
things; one of VB's greatest strengths for me has always been the
ability to start up VB, wham bam thank you maam, all done.

Don't get me wrong, I do a lot of real, serious application
development in VB as well. And for that, VB2005 looks like it might be
a lot better than VB6 (based on what you guys say and what I've seen).
But for the quicky one-shot tools; I wonder if I should just make sure
I don't lose my VB6 install CDs?
dim g as graphics
dim bmp as new bitmap(100,50))

g = graphics.fromimage(bmp)

g.drawrectangle
g.drawstring
g.DrawPie

g.dispose

Of course, you don't have to create the Bitmap each time you paint.
Create it once.

Thanks for that example.
No. At the end of the day, you have to write *much* *less* code in
VB.Net.

That is encouraging. I was hoping that somebody would say something
like that. If it's just a matter of me getting more familiar with it,
that's something I can deal with :)
Once you got it, you will love it. Really.

Thanks a lot (and Aaron Smith too), that helps and gives me good
keywords to look for, too.

Jason
 
A

Armin Zingler

I'm not interested in performance or proper software design, in this
case. Here, I am trying to use VB to quickly perform certain tasks,
one-shot programs that I don't even bother saving (for example, load
an image, remove saturation, save image, or other various simple
image processing tasks), but I also like to visualize what is
happening on the screen rather than doing everything in an offscreen
bitmap. For VB6 it was convenient, and exactly what I wanted, to use
PictureBox's Point and PSet.

How do I get a Bitmap object from a VB2005 PictureBox...?

No, a Picturebox is there to display a Bitmap, not the other way round.
:)
That is
the connection I was having trouble seeing. How can I get something
from a PictureBox that I can call GetPixel on?


You can call an API function also called GetPixel. You'd have to get the
device context first, which is returned from graphics.GetHDC. GetHDC is
an instance member called on a graphics object that you have to create
from the control first.

In the latest framework, there is a managed function that creates a
screenshot, maybe it's also available for a single control like a
picturebox. Unfortunatelly, I currently don't find it.

I am loading a picture from a file (by setting the picture property
at design-time), operating on it and drawing it back to the
picturebox, and then (in VB6 at least), I could use SavePicture
(IIRC, sorry I don't have VB6 open to verify right now) if I wanted
to save the modified image back out (or write out whatever other
data I was trying to generate with other code).

So do you recommend, as the best way to do all this, doing all work
(Point and PSet and anything else) in a Bitmap and displaying it in
the PictureBox?

Yes. However, processing pixel by pixel can get very slow. If you have
to do many operations on the Bitmap, you may consider calling the
Bitmap's LockBits function. (and some do the processing in C# or even
C++ because it can be even faster)
It's handy being able to browse to the bitmap file I
want to work with graphically at design time (through the property
editor on a PictureBox) rather than having to type lines of code to
do it. But I guess typing out the path to the file in code like (not
real VB code): MyBitmap = LoadBitmapFromFile("c:\documents and
settings \jason\my documents\my
pictures\images\whatever\sure\bitmap.bmp" is a small sacrifice to
make, although it's a little inconvenient
sometimes.

Sorry, I don't understand what you want to say by this.
I think my problem is my typical usage of VB is not very... typical.
I use it as a really quick prototyping tool, or a tool to perform
some tasks really, really quickly. A lot of one-shot stuff. To me it
serves a similar purpose to, say, a shell script or batch file -- a
great tool for automating a simple one-time task. So I become overly
frustrated when I have to start doing tedious things or worry about
actual good software design practices when I'm working with these
things; one of VB's greatest strengths for me has always been the
ability to start up VB, wham bam thank you maam, all done.

I see. Maybe for very small projects there is a little bit more to do
compared to VB6, but I can't say it in general. For example, the generic
lists are very useful now; you did not even have them in VB 2003, not to
mention VB6. You had to write your own collection for each object type.
Now you simply write List(Of whatever) and you have a typed collection.
Don't get me wrong, I do a lot of real, serious application
development in VB as well. And for that, VB2005 looks like it might
be a lot better than VB6 (based on what you guys say and what I've
seen). But for the quicky one-shot tools; I wonder if I should just
make sure I don't lose my VB6 install CDs?

Well, it always depends. I wouldn't lose them anyways. ;-)


Armin
 
A

Armin Zingler

Armin Zingler said:
In the latest framework, there is a managed function that creates a
screenshot, maybe it's also available for a single control like a
picturebox. Unfortunatelly, I currently don't find it.

Found it: System.Windows.Forms.Control.DrawToBitmap

And there is System.Drawing.Graphics.CopyFromScreen. (note that screen
coordinates are expected; see control.pointtoscreen method)


Armin
 

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