Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C++

Image Viewer Utility

Rate me:
Please Sign up or sign in to vote.
4.94/5 (50 votes)
10 Mar 2007CPOL18 min read 406.2K   19.2K   200  
A little utility program that allows you to view the contents of memory bitmaps and device contexts while you are stepping through your drawing code.
/***************************************************************************
ImageViewer.h : header file for the Image Viewer tool
written by PJ Arends
pja@telus.net

Version 2.1

The ShowImageList portion was originally written by J�rgen Sigvardsson <jorgen@profitab.com>

The ShowRegion portion was originally written by Waldermort <waldermort@hotmail.com>

For updates check http://www.codeproject.com/tools/imageviewer.asp

-----------------------------------------------------------------------------
This code is provided as is, with no warranty as to it's suitability or usefulness
in any application in which it may be used.

This code may be used in any way you desire. This file may be redistributed by any
means as long as it is not sold for profit, and providing that this notice and the
authors name are included.

If any bugs are found and fixed, a note to the author explaining the problem and
fix would be nice.
-----------------------------------------------------------------------------

            Instructions on using the Image Viewer utility
            ==============================================

The Image Viewer utility is used to show the contents of memory device contexts
and in memory bitmaps while you are stepping through some drawing code.

To use the image viewer utility, make sure the ImageViewer.dll file is in your
executable path, the ImageViewer.exe program is running, the ImageViewer.lib
file is in your library include path, and then simply include this file
in any file you want to use it in. This utility only works in debug builds
and you turn the tool on by #defining the 'ACTIVATE_VIEWER' macro.

  ie.
     #define ACTIVATE_VIEWER
     #include "ImageViewer.h"


This file defines the following macros :

  ShowBitmap  (HBITMAP Bitmap)
  ShowBitmap2 (HBITMAP Bitmap, LPCTSTR Text)

      Bitmap [in] - A HBITMAP or a CBitmap object
      Text   [in] - A descriptive text that is accessible from the viewer

      Displays the contents of an in-memory bitmap


  ShowDC  (HDC DC)
  ShowDC2 (HDC DC, LPCTSTR Text)

      DC   [in] - A HDC or a CDC object
      Text [in] - A descriptive text that is accessible from the viewer

      Displays the contents of a memory device context


  ShowGDIPlusBitmap  (Gdiplus::Bitmap Bitmap)
  ShowGDIPlusBitmap2 (Gdiplus::Bitmap Bitmap, LPCTSTR Text)

      Bitmap [in] - A Gdiplus Bitmap
      Text   [in] - A descriptive text that is accessible from the viewer

      Displays the contents of an in memory GDI+ Bitmap


  ShowIcon  (HICON Icon)
  ShowIcon2 (HICON Icon, LPCTSTR Text)

      Icon [in] - A HICON object
      Text [in] - A descriptive text that is accessible from the viewer

      Displays the contents of a HICON handle


  ShowImageList  (HIMAGELIST List, int Index, UINT Flags)
  ShowImageList2 (HIMAGELIST List, int Index, UINT Flags, LPCTSTR Text)

      List  [in] - A HIMAGELIST or a CImageList object
      Index [in] - Index of image in list to show, -1 for all
      Flags [in] - The ILD_* flag that specifies how to display the image
      Text  [in] - A descriptive text that is accessible from the viewer

      Displays the contents of an in-memory image list


  ShowRegion  (HRGN Region)
  ShowRegion2 (HRGN Region, LPCTSTR Text)
  ShowRegion3 (HRGN Region, HBITMAP Bitmap, LPCTSTR Text)
  ShowRegion4 (HRGN Region, HDC DC, LPCTSTR Text)

      Region [in] - A HRGN or a CRgn object
      Text   [in] - A descriptive text that is accessible from the viewer
      Bitmap [in] - The background bitmap to draw the region onto
      DC     [in] - The memory device context to draw the region onto

      Displays the shape of a HRGN object. If a bitmap or memory device context
      is supplied the region is shown by inverting the colours of the area covered
      by the region, otherwise the region is shown in black on a white background.


If the ACTIVATE_VIEWER and _DEBUG macros are not defined, the Show* macros
are ignored, The ImageViewer.lib file is not linked, and the ImageViewer.dll
file is not loaded.

If the Image Viewer tool is active, the VIEWER_ACTIVE macro will be defined,
so you can control extra code around the Show* macros by checking for the
VIEWER_ACTIVE macro.

  ie.
     #ifdef VIEWER_ACTIVE

     // Some code that requires the image viewer tool

     #endif // VIEWER_ACTIVE

****************************************************************************/

#ifndef PJA_IMAGE_VIEWER_UTILITY_HEADER_FILE_INCLUDED
#   define PJA_IMAGE_VIEWER_UTILITY_HEADER_FILE_INCLUDED

#   if _MSC_VER > 1000
#       pragma once
#   endif // _MSC_VER > 1000

#   undef VIEWER_ACTIVE

#   if defined _DEBUG && defined ACTIVATE_VIEWER
#       define VIEWER_ACTIVE
#   endif // defined _DEBUG && defined ACTIVATE_VIEWER

#   ifdef VIEWER_ACTIVE
#       pragma comment(lib, "ImageViewer.lib")

#       ifdef __cplusplus
            extern "C" {
#       endif // __cplusplus

        // Do not call these functions directly in your code, doing so
        // will cause linker and/or run time errors if the Image Viewer
        // tool is turned off. Use the Show* macros instead.
#       ifdef _UNICODE
            __declspec(dllimport) void BitmapViewW(HBITMAP, LPCWSTR, UINT, LPCWSTR);
            __declspec(dllimport) void IconViewW(HICON, LPCWSTR, UINT, LPCWSTR);
            __declspec(dllimport) void RegionViewW(HRGN, HBITMAP, LPCWSTR, UINT, LPCWSTR);
#           define BitmapView BitmapViewW
#           define IconView IconViewW
#           define RegionView RegionViewW
#       else // _UNICODE
            __declspec(dllimport) void BitmapViewA(HBITMAP, LPCSTR, UINT, LPCSTR);
            __declspec(dllimport) void IconViewA(HICON, LPCSTR, UINT, LPCSTR);
            __declspec(dllimport) void RegionViewA(HRGN, HBITMAP, LPCSTR, UINT, LPCSTR);
#           define BitmapView BitmapViewA
#           define IconView IconViewA
#           define RegionView RegionViewA
#       endif // _UNICODE

#       ifdef _INC_COMMCTRL
#           ifdef _UNICODE
                __declspec(dllimport) void ImageListViewW(HIMAGELIST, int, UINT, LPCWSTR, UINT, LPCWSTR);
#               define ImageListView ImageListViewW
#           else // _UNICODE
                __declspec(dllimport) void ImageListViewA(HIMAGELIST, int, UINT, LPCSTR, UINT, LPCSTR);
#               define ImageListView ImageListViewA
#           endif // _UNICODE
#       endif // _INC_COMMCTRL

#       ifdef __cplusplus
            }
#       endif // __cplusplus

#       define ShowBitmap(Bitmap)        BitmapView((Bitmap), _T(__FILE__), __LINE__, NULL)
#       define ShowBitmap2(Bitmap, Text) BitmapView((Bitmap), _T(__FILE__), __LINE__, (Text))

#       define ShowDC(DC)        BitmapView((HBITMAP)GetCurrentObject((DC), OBJ_BITMAP), _T(__FILE__), __LINE__, NULL)
#       define ShowDC2(DC, Text) BitmapView((HBITMAP)GetCurrentObject((DC), OBJ_BITMAP), _T(__FILE__), __LINE__, (Text))

#       define ShowIcon(Icon)        IconView ((Icon), _T(__FILE__), __LINE__, NULL)
#       define ShowIcon2(Icon, Text) IconView ((Icon), _T(__FILE__), __LINE__, (Text))

#       define ShowRegion(Region)                RegionView((Region), NULL, _T(__FILE__), __LINE__, NULL)
#       define ShowRegion2(Region, Text)         RegionView((Region), NULL, _T(__FILE__), __LINE__, (Text))
#       define ShowRegion3(Region, Bitmap, Text) RegionView((Region), (Bitmap), _T(__FILE__), __LINE__, (Text))
#       define ShowRegion4(Region, DC, Text)     RegionView((Region), (HBITMAP)GetCurrentObject((DC), OBJ_BITMAP), _T(__FILE__), __LINE__, (Text))

#       ifdef _INC_COMMCTRL   // defined in CommCtrl.h; header needed for HIMAGELIST
#           define ShowImageList(List, Index, Flags)        ImageListView((List), (Index), (Flags), _T(__FILE__), __LINE__, NULL)
#           define ShowImageList2(List, Index, Flags, Text) ImageListView((List), (Index), (Flags), _T(__FILE__), __LINE__, (Text))
#       else
#           define ShowImageList(List, Index, Flags)
#           define ShowImageList2(List, Index, Flags, Text)
#       endif // _INC_COMMCTRL

#       ifdef _GDIPLUS_H   // defined in GdiPlus.h
            // Use an inline static class member function to avoid LNK2005 errors.
            // This is placed here and not in the dll so users of the dll do not
            // have to have GDI+ installed on their machines.
            class DA9727EEC4E4438AACB39A9F5AACD96E
            {
            public:
                static void GDIPlusBitmapView (Gdiplus::Bitmap &Bitmap, LPCTSTR File, UINT Line, LPCTSTR Text)
                {
                   HBITMAP hBitmap;
                   if (Gdiplus::Ok == Bitmap.GetHBITMAP(Gdiplus::Color(192, 192, 192), &hBitmap))
                       BitmapView(hBitmap, File, Line, Text);
                }
            };
#           define ShowGDIPlusBitmap(Bitmap)        DA9727EEC4E4438AACB39A9F5AACD96E::GDIPlusBitmapView((Bitmap), _T(__FILE__), __LINE__, NULL)
#           define ShowGDIPlusBitmap2(Bitmap, Text) DA9727EEC4E4438AACB39A9F5AACD96E::GDIPlusBitmapView((Bitmap), _T(__FILE__), __LINE__, (Text))
#       else
#           define ShowGDIPlusBitmap(Bitmap)
#           define ShowGDIPlusBitmap2(Bitmap, Text)
#       endif // _GDIPLUS_H

#   else // VIEWER_ACTIVE
#       define ShowIcon(Icon)
#       define ShowBitmap(Bitmap)
#       define ShowDC(DC)
#       define ShowImageList(List, Index, Flags)
#       define ShowGDIPlusBitmap(Bitmap)
#       define ShowRegion(Region)

#       define ShowIcon2(Icon, Text)
#       define ShowBitmap2(Bitmap, Text)
#       define ShowDC2(DC, Text)
#       define ShowImageList2(List, Index, Flags, Text)
#       define ShowGDIPlusBitmap2(Bitmap, Text)
#       define ShowRegion2(Region, Text)

#       define ShowRegion3(Region, Bitmap, Text)
#       define ShowRegion4(Region, DC, Text)
#   endif // VIEWER_ACTIVE

#endif // PJA_IMAGE_VIEWER_UTILITY_HEADER_FILE_INCLUDED

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
Canada Canada
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.

Comments and Discussions