If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Windows Forms: obtaining window handle. HWND isn't recognized

Hi Guys, I am developing an app that shows a pixelated picture in a window. I am trying to capture the image, which is composed of a number of tiles processed from a mother image, but I am having difficulty since I can't reference the image on the screen. I have placed a rectangle around the image and my understanding is that I have get the window handle for the rectangle in order to save the Image as a jpg file. In the literature there is something about a Device Context and a window handle but my compiler throws out HWND and I don't really understand the device context. I am using Windows Forms, not native code. If anyone can help it would be appreciated

Re: Windows Forms: obtaining window handle. HWND isn't recognized

Re: Windows Forms: obtaining window handle. HWND isn't recognized

So your image on screen is a System::Windows::Forms::PictureBox? In this case you don't need to mess around with native window handles and such. The PictureBox exposes the image it displays as its Image property, and the exposed object features a Save() method to conveniently save the image to file. Somewhat like this:

Code:

pictureBox1->Image->Save("MyImage.jpg", ImageFormat::Jpeg);

As I interpret your description, your "rectangle trick" probably wouldn't have worked that way anyway, BTW.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: Windows Forms: obtaining window handle. HWND isn't recognized

Originally Posted by Eri523

So your image on screen is a System::Windows::Forms::PictureBox? In this case you don't need to mess around with native window handles and such. The PictureBox exposes the image it displays as its Image property, and the exposed object features a Save() method to conveniently save the image to file. Somewhat like this:

Code:

pictureBox1->Image->Save("MyImage.jpg", ImageFormat::Jpeg);

As I interpret your description, your "rectangle trick" probably wouldn't have worked that way anyway, BTW.

Thanks for your response. Yes, I know that method but the problem I have is that I don't have the name of the imgage file. I had a mother image file that I divided into rectangles and then cloned each rectangle to analyse each rectangle's color content:
g3->DrawRectangle(redPen,r);
image2 = scaledImage->Clone(r,scaledImage->PixelFormat);
where g3 is a graphics representation of a pictureBox. I then used the FillRectangle method to replace each of the cloned rectangles with the average color of the piece of mother picture analysed :
Color brushColor = Color::FromArgb(rav,gav,bav);
SolidBrush^ myBrush =gcnew SolidBrush(brushColor);
g3->FillRectangle(myBrush, r);
so I end up with a pixelated picture on the screen but without any way of accessing the image. When I use your save method I merely save the original mother image. I hope this makes my problem clearer. What I really need is a method of changing the mother image as each rectangle is processed.

Re: Windows Forms: obtaining window handle. HWND isn't recognized

Originally Posted by Zinn

[...] the problem I have is that I don't have the name of the imgage file. [...]

I'm afraid I don't understand this part ot your problem. When writing an image to an external disk file, in the vast majototy of cases, you're one of these three scenarios: The output file is entirely under your control, in which case you may pick any valid file neme you like. You store the file for further processing by another program; in this case you've either been told the required file name up-front, or you pick a name and pass it on for further processing. Finally, your code may have been passed a file handle or other file object to write the image to; in this case you won't get in touch with the file name at all anyway.

As to the rest of your problem description: So you're obtaining your graphics object for painting like this?

Code:

Graphics ^g3 = pictureBox1->CreateGraphics();

This way you're painting onto the picture box object representing the image, rather than onto the original image itself. If you're doing that the right way, in the picture box' paint event handler (in this case you'd probably better be referring to the Graphics as e->Graphics from inside the handler, however, rather than creating a new one), you may be able to get a representation of the processed image for saving by using the PictureBox::DrawToBitmap() method. Otherwise your only choice (as long as you stick to painting on the screen representation rather than the actual subject image) is taking a screen shot of the processed image using the Graphics::CopyFromScreen() method. You may technically get away with that, but it's by far the most complicated option, and why take so much effort?

Why not simply paint onto the original image directly? You can obtain a Graphics that enables you to do that somewhat like this:

Code:

Graphics ^g3 = Graphics::FromImage(pictureBox1->Image);

I'd expect that to require few to zero changes to your existing code, especially if resolutions of the original image and its screen representation are identical. And if the resolutions differ between the two, working directly with the original may well even improve the quality of the result of your processing.

I'm not entirely sure, however, whether drawing to an image while at the same time have it displayed in an active picture box is safe. If you experience that this is problematic (or see that documented somewhere), you may Clone() the image backing the picture box, apply your modifications to the clone, and eventually assign the clone to the PB's Image property to display it.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: Windows Forms: obtaining window handle. HWND isn't recognized

Originally Posted by Eri523

I'm afraid I don't understand this part ot your problem. When writing an image to an external disk file, in the vast majototy of cases, you're one of these three scenarios: The output file is entirely under your control, in which case you may pick any valid file neme you like. You store the file for further processing by another program; in this case you've either been told the required file name up-front, or you pick a name and pass it on for further processing. Finally, your code may have been passed a file handle or other file object to write the image to; in this case you won't get in touch with the file name at all anyway.

As to the rest of your problem description: So you're obtaining your graphics object for painting like this?

Code:

Graphics ^g3 = pictureBox1->CreateGraphics();

This way you're painting onto the picture box object representing the image, rather than onto the original image itself. If you're doing that the right way, in the picture box' paint event handler (in this case you'd probably better be referring to the Graphics as e->Graphics from inside the handler, however, rather than creating a new one), you may be able to get a representation of the processed image for saving by using the PictureBox::DrawToBitmap() method. Otherwise your only choice (as long as you stick to painting on the screen representation rather than the actual subject image) is taking a screen shot of the processed image using the Graphics::CopyFromScreen() method. You may technically get away with that, but it's by far the most complicated option, and why take so much effort?

Why not simply paint onto the original image directly? You can obtain a Graphics that enables you to do that somewhat like this:

Code:

Graphics ^g3 = Graphics::FromImage(pictureBox1->Image);

I'd expect that to require few to zero changes to your existing code, especially if resolutions of the original image and its screen representation are identical. And if the resolutions differ between the two, working directly with the original may well even improve the quality of the result of your processing.

I'm not entirely sure, however, whether drawing to an image while at the same time have it displayed in an active picture box is safe. If you experience that this is problematic (or see that documented somewhere), you may Clone() the image backing the picture box, apply your modifications to the clone, and eventually assign the clone to the PB's Image property to display it.

Thanks again for your time and interest. Yes, I have used : Graphics ^g3 = pictureBox1->CreateGraphics() and therefore I am painting on the picturebox. I tried using DrawToBitMap() but it only allowed me to save the original picture. I will try using the image directly. I started out doing it that way but had some difficulty modifying the image ( I've forgotten the details) but I'll have another go. I'll keep you informed. Thanks again.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.