CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    10

    Red face 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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    If you're using forms, you're in the wrong forum. Regardless, without seeing your code, I doubt anybody can help.

  3. #3
    Join Date
    Jan 2014
    Posts
    10

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

    Sorry, I'm new to this. Which forum should I post in?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.

  5. #5
    Join Date
    Jan 2014
    Posts
    10

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

    Quote Originally Posted by Eri523 View Post
    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.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

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

    Quote Originally Posted by Zinn View Post
    [...] 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.

  7. #7
    Join Date
    Jan 2014
    Posts
    10

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

    Quote Originally Posted by Eri523 View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured