How to capture the screenshot of window?

14 07 2008


We’re familear with PrintScreen and Alt+PrintScreen shortcuts. They are used to take the screenshot of entire desktop or a particular window. Well, Is it possible to take the snapshot of a particular control or a particular window by yourself?


You can use the api – PrintWindow(). If you provide the window handle, the screenshot of the window will be drawn to the provided device context. See the screenshot application which captures the screenshot of calculator application.

See the code snippet.

void CScreenShotDlg::OnPaint()
{
    // device context for painting
    CPaintDC dc(this); 

    // Get the window handle of calculator application.
    HWND hWnd = ::FindWindow( 0, _T( "Calculator" ));

    // Take screenshot.
    PrintWindow( hWnd,
                 dc.GetSafeHdc(),
                 0 );
}


The PrintWindow() is available only from XP onwards. So for getting this compiled, you’ve to add _WIN32_WINNT=0x0501 to preprocessor definitions in project settings.

Also check WM_PRINT message, which is more or less same as PrintWindow().


Targeted Audience – Beginners.

Advertisements




How to find the GdiObject type from handle.

8 06 2008


Gdi Objects are represented by handles. Basically all handles are just an integer inside. For instance, if you’re writing a function with accepts HFONT as parameter, its quite possible to pass a typecasted handle of other Gdi object. So how can you determine the real Gdi object type from the handle?


You can use the function – GetObjectType(). See the following code snippet.

// Get the Black Brush.
HGDIOBJ hGdiObj = GetStockObject( BLACK_BRUSH );

// Check the object type with handle.
if( OBJ_BRUSH == GetObjectType( hGdiObj ))
{
    // Yes! The handle points to Gdi Brush.
}

So now add better error checking in your gdi routines. 😉


The values that GetObjectType() returns are,

  • OBJ_BITMAP – Bitmap
  • OBJ_BRUSH – Brush
  • OBJ_COLORSPACE – Color space
  • OBJ_DC Device – Context
  • OBJ_ENHMETADC – Enhanced metafile DC
  • OBJ_ENHMETAFILE – Enhanced metafile
  • OBJ_EXTPEN – Extended pen
  • OBJ_FONT – Font
  • OBJ_MEMDC – Memory DC
  • OBJ_METAFILE – Metafile
  • OBJ_METADC – Metafile DC
  • OBJ_PAL – Palette
  • OBJ_PEN – Pen
  • OBJ_REGION – Region


Targeted Audience – Beginners.





How to fill gradients in your dialog like InstallShield?

5 06 2008


Look at the dialog. What’s the first thing that come to your mind when you see this?

Yes! I can read it – the InstallShield. Its so common to our eyes, since almost all application setups comes via InstallShield. People identify installshield with its nice blue gradient in its background window. So how can we generate such nice gradient background like installShield does?


You can use the function – GradientFill(). See the code snippet to generate the InstallShield effect. Its self explanatory.

// Get the Device context.
CPaintDC dc( this );

// Get the client co-ordinates.
CRect rect;
GetClientRect( &rect );

// Two gradient points.
TRIVERTEX vert[2] = { 0 };

// Set the first vertex point at 0,0 and with blue color.
// Since the array is initialize with 0, all other
// members in struct are 0.
vert[ 0 ] .Blue   = 0xff00;

// Initialize second vertex at endcorner of
// window with black color.
vert[ 1 ] .x      = rect.Width();
vert[ 1 ] .y      = rect.Height();

// Gradient rectangle.
GRADIENT_RECT GradientRect = { 0 };
GradientRect.UpperLeft = 0;
GradientRect.LowerRight = 1;

// Now fill the gradient.
GradientFill( dc.GetSafeHdc(),
              vert, // Gradient vertices.
              2,    // No. of vertices.
              &GradientRect, // Gradient array.
              1,    // No. of items in Gradient Array.
              GRADIENT_FILL_RECT_V ); // Vertical Fill.


Don’t forget to add Msimg32.lib to your project settings.


Targeted Audience – Intermediate.





How to convert Image to Grayscale by using HLS colorspace?

27 05 2008


Many image processing application provide functionality to convert images to gray scale. How they do it? There are many ways to convert an image to gray scale. The common method is to get the gray value of a pixel by the following equation

GrayValue = 0.3*Red + 0.59*Green + 0.11*Blue

There is another method to convert an image to grayscale – By using HLS colorspace. I’ve already explaned about HLS color space here –
http://weseetips.com/2008/05/22/convert-color-in-rgb-to-hls-and-vice-versa/

In the HLS color space, we can represent a pixel in Hue, luminosity and Saturation, where Saturation represents the colorfulness of the color or how colorful the color is. If the saturation goes to minimum, that color will become grayscale and if it goes to maximum the more fluorescent it will become.


1) Get each pixel in image in RGB.
2) Convert RGB to HLS and get the saturation.
3) Set saturation to 1 and convert back to RGB.
4) Set the new image pixel.

See the code snippet below. Please note that Its not an optimized one. Just to demonstrate the process.

// Create the image.
Bitmap ColorImage( csSourceImage );

// Get the size of image.
for( UINT x = 0; x < ColorImage.GetWidth(); ++x )
{
    for( UINT y = 0; y < ColorImage.GetHeight(); ++y )
    {
        // Get the pixel at x,y
        Color PixelColor;
        ColorImage.GetPixel( x, y, &PixelColor );

        // Convert it to COLORREF
        COLORREF RgbColor = PixelColor.ToCOLORREF();

        // Convert to HLS Color space
        WORD Hue = 0;
        WORD Luminance = 0;
        WORD Saturation = -100;
        ColorRGBToHLS( RgbColor, &Hue, &Luminance, &Saturation );

        // Set the saturation to 0 so that the
        // image will be greyscale.
        Saturation = 1;

        // Now re-generate HLS to RGB
        RgbColor = ColorHLSToRGB( Hue, Luminance, Saturation );

        // Convert back to Gdi+.
        PixelColor.SetFromCOLORREF( RgbColor );

        // Set it to image.
        ColorImage.SetPixel( x, y, PixelColor );
    }
}


Download sample project from here. The sample support only Jpeg images but can be easily extended.
http://jijoraj.110mb.com//samples/grayscaleconverter/GrayScaleConverter.zip
Please not that the GUI is not so much polished. 😉

As you think the first algorithm is more faster. But still HLS can be used and this is only one usage for HLS. The code snippet can be optimized further. But this is just for demo purpose. isn’t it? 😉


Targeted Audience – Intermediate.





Convert colors in RGB to HLS and vice versa.

22 05 2008


RGB doesn’t need and introduction and If you are experienced in adobe photoshop, then you might be already familiar with HLS too.

RGB
All of us know about RGB. RGB is a color space in which, colors are represented by Red, Green and Blue components. Each component can vary from 0-255 in value. See the RGB color space below. (Thanks to MSDN for the pic)

HLS
HLS is also a Color space similar to RGB. But instead of red, green and blue components, HLS contain Hue, Luminance & Saturation components. Its a color space in which the Hue, saturation and luminance of a color can be separated and modified. See the HLS color space below.( Thanks to Wiki for the pic. )


Well, how can you convert RGB color to HLS and vice versa? You can use the api’s – ColorRGBToHLS() and ColorHLSToRGB(). See the sample code snippet below.

#include "Shlwapi.h"
...
// Red color in RGB
COLORREF RgbColor = RGB( 255, 0, 0 );

// Convert to HLS Color space
WORD Hue = 0;
WORD Luminance = 0;
WORD Saturation = 100;
ColorRGBToHLS( RgbColor, &Hue, &Luminance, &Saturation );

// Its converted to Hue, Luminance and Saturation.
// You can adjust the parameters according to your wish.

// Now convert back to RGB.
RgbColor = ColorHLSToRGB( Hue, Luminance, Saturation );


You can generate a wide variety of effects by adjusting HLS parameters which cannot be done by using RGB. Designers always use these HLS components for generating creative images. Take Photoshop and have a try!

BTW, don’t forget to add Shlwapi.lib to project settings.


Targeted Audience – Intermediate.