Visual Studio C++ pictureBox

Associate
Joined
4 Jun 2003
Posts
770
Location
United Kingdom
Hi all,

If I use a picture box in a program and use the command:

pictureBox1->Load

to draw a picture to the pictureBox how do I then access the pixels of that picture and get the length and width back?

Thanks
 
Minority said:
Hi all,

If I use a picture box in a program and use the command:

pictureBox1->Load

to draw a picture to the pictureBox how do I then access the pixels of that picture and get the length and width back?

Thanks
Is this C++.NET?

If so:
Code:
int width = pictureBox1->Image->Width;
int height = pictureBox1->Image->Height;

If you want to access the pixels individually:
Code:
// Convert to Bitmap to allow manipulation
Bitmap imageBitmap = new Bitmap(pictureBox1->Image);

int x = 0;
int y = 0;
Color pixelColour = Color::Black;

// Set a pixel to black
imageBitmap->SetPixel(x, y, pixelColour);

// Get the pixel's colour again
pixelColour = imageBitmap->GetPixel(x, y);
If it's not .NET I can't help :p
 
Last edited:
I was going to do it in standard C++ but I'll change to .NET, can't be hard to pick up.

How do I start a C++ .NET project in Visual Studio 2005? I need it to have a Windows Forms feel to it i.e. and easy to implement and change GUI.

Thanks
 
Just create a new Windows Forms Application project. It will generate all the code needed to display the form, so you can just build the form in designer view with minimal hassle. :)
 
I've got it stuck in C++ mode, it seems to auto create a C++ project.

Do you know how I change it?

(I'm not overly familiar with VS, I'm used to Borland Builder)
 
Ahh I think I understand, it's possible to use multiple language types in one project? Selecting Windows Forms Application allows C++ code and C++ .NET code to be written into the same project?

Where should I declare the Bitmap imageBitmap so that it is accessible throughout the program?

Again, probably a stupid question, but Borland works differently.
 
I'm having to use the following instead of what you advised:

Bitmap imageBitmap = gcnew Bitmap(pictureBox1->Image);

imageBitmap.SetPixel(x, y, pixelColour);

Anyhow, I can now draw to pixels and get their colours back, but how do I update the pictureBox image with the modified imageBitmap? I tried

pictureBox1->Load(imageBitmap);

but it didn't like it.

Last question, how do I get the individual RGB values for the pixels of an image?

Thanks
 
Minority said:
I'm having to use the following instead of what you advised:

Bitmap imageBitmap = gcnew Bitmap(pictureBox1->Image);

imageBitmap.SetPixel(x, y, pixelColour);
Whatever works :p

I'm a C# man myself, so I'm just trying to give a rough idea of how you'd do it in C++ .NET, as they use the same libraries. I'm not fully up to scratch on the differences between C++ and C#, so my code isn't likely to be 100% correct :)

Minority said:
Anyhow, I can now draw to pixels and get their colours back, but how do I update the pictureBox image with the modified imageBitmap? I tried

pictureBox1->Load(imageBitmap);
Just do the following:
Code:
pictureBox1->Image = imageBitmap;
... and it'll redraw itself.

Minority said:
Last question, how do I get the individual RGB values for the pixels of an image?
Simple; use the R, G and B (and A if needed) properties of the Color returned by GetPixel :)
 
Last edited:
Minority said:
Ahh I think I understand, it's possible to use multiple language types in one project? Selecting Windows Forms Application allows C++ code and C++ .NET code to be written into the same project?
Again, I'm not that familiar with C++, but as far as I know, Visual Studio doesn't allow multiple languages in one project. You can, however, have multiple languages in one solution. Just add another project to the solution in the appropriate language, and you can make calls between them easily.

This is going beyond the extent of my C++ knowledge though, really, so you're better off asking one of the resident C++ gurus I think :)

Minority said:
Where should I declare the Bitmap imageBitmap so that it is accessible throughout the program?
I'm not really sure, because I don't know how your program works, but it would probably be sufficient just to use local variables where you need to.
 
Thanks for the guidance, I'll have a go.

I'm still having trouble redrawing the bitmap to the pictureBox image though, it gives me an error:

cannot convert parameter 1 from 'System::Drawing::Bitmap' to 'System::Drawing::Image ^'

Maybe I would be better if I could get it to create a C++ .NET project?

When I got File->New->Project the project types are listed as Visual C++ and from there I select Windows Forms Application. There's no mention of .NET anywhere.
 
Minority said:
I'm still having trouble redrawing the bitmap to the pictureBox image though, it gives me an error:

cannot convert parameter 1 from 'System::Drawing::Bitmap' to 'System::Drawing::Image ^'
Again, this is just C++ technicalities to me, but I guess you could try:
Code:
Bitmap^ imageBitmap = gcnew Bitmap(pictureBox1->Image);
[/CODE

[QUOTE=Minority]When I got File->New->Project the project types are listed as Visual C++ and from there I select Windows Forms Application.  There's no mention of .NET anywhere.[/QUOTE]
That would be because the whole of Visual Studio 2005 is .NET :)
 
Last edited:
Inquisitor said:
Code:
Bitmap^ imageBitmap = gcnew Bitmap(pictureBox1->Image);

THANKS!

You're a star, this has enabled me to use

Code:
imageBitmap->SetPixel(x,y,pixelColour);

&

pictureBox->Image = imageBitmap;

I think I'm beginning to understand VS now, it has some things which are a lot nicer than Borland. I just need to understand this .NET malarky now, might have to get a good book.
 
Minority said:
I think I'm beginning to understand VS now, it has some things which are a lot nicer than Borland. I just need to understand this .NET malarky now, might have to get a good book.
I'd definitely recommend getting a book, they're a huge help when it comes to learning a language (even if you already understand the gist of it). Perhaps one that focuses mainly on the .NET framework, rather than C++ itself?
 
Inquisitor said:
I'd definitely recommend getting a book, they're a huge help when it comes to learning a language (even if you already understand the gist of it). Perhaps one that focuses mainly on the .NET framework, rather than C++ itself?

Yeh definitely. I have a pretty good understanding of C++ so picking up most languages isn't a problem but a book definitely helps. I do need toget to grips with .NET though.

Am I right in thinking its Microsofts version of Java essentially?
 
Minority said:
Yeh definitely. I have a pretty good understanding of C++ so picking up most languages isn't a problem but a book definitely helps. I do need toget to grips with .NET though.

Am I right in thinking its Microsofts version of Java essentially?
Kind of. .NET is a set of common libraries and standards that are shared by all .NET languages. C# could be considered the Microsoft equivelant of Java, as it is Microsoft's current flagship language, and was designed from the ground up around the .NET framework. It is in fact so similar to Java, that its internal development name was "I Can't Believe It's Not Java" :p

The primary advantage of the .NET framework (and reason for its development) is that all languages that support it can easily interact with each other, and that it provides a ready-made library of very useful tools. For example, in Visual Studio 2005, you can have a solution that contains a C# project, a VB.NET project, and a C++ project (managed). These three projects can make calls between eachother just as if they are in the same language, which is very useful.

I believe Sun and Oracle are developing their own version of the .NET framework in fact.
 
Last edited:
Back
Top Bottom