Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / MFC
Article

CPJAImage - Yet another image drawing class

Rate me:
Please Sign up or sign in to vote.
4.98/5 (24 votes)
26 Aug 2003CPOL4 min read 233.5K   5.6K   80   44
A class that draws an image with various options - transparently, disabled or grayscale, centered on or stretched to fit a given rectangle.

Sample Image - PJAImage.jpg

Updates

Aug 23, 2003 - Fixed transparent drawing bug

Introduction

After I wrote my CFileEditCtrl edit control, I got a request to place an image on the browse button, and then another request to make the control flat and hot to the mouse. That was becoming quite a task, searching on this site and www.codeguru.com for drawing code that would draw images, either bitmaps or icons, either normally or transparently, grey-scaled or disabled, possibly stretched or at least centered on the button. So after finding some code that solved one bit or another (but not quite the way I wanted), I put them together, filled in the pieces that were missing and came up with the CPJAImage class. While the reason I wrote the class was for the browse button, I thought it was a shame not to share it with you as yet another separate image rendering class.

Acknowledgments

Jean-Edouard Lachand-Robert for his DitherBlt() function.

Paul Reynolds for the "True Mask" transparency function.

Zafir Anjum and Guy Gascoigne for their greyscale algorithm.

Using the CPJAImage class

The first step is to include the PJAImage.h and PJAImage.cpp files in your project. Then after you have loaded or created your bitmap or icon object in the normal fashion, select it into the CPJAImage object using the SetImage() function.

CBitmap Bitmap;
Bitmap.LoadBitmap(IDB_BITMAP1);
CPJAImage PJAImage;
PJAImage.SetImage(Bitmap, PJAI_BITMAP);

Then when you are ready to draw the image, use the DrawImage() function to draw the image onto the given device context.

PJAImage.DrawImage(pDC, left, top, width, height, DrawFlags);

The drawing flags you specify in the DrawImage() function determine how the image is drawn.

User functions

CPJAImage::CPJAImage()

The class constructor creates an empty CPJAImage object.

CPJAImage::~CPJAImage()

The class destructor destroys the image handle

void CPJAImage::DrawImage(CDC *pToDC, int x, int y, int w, int h, DWORD DrawFlags = 0)

The DrawImage() does the actual drawing.

Parameters

pToDCA pointer to the device context the image is to be drawn on.
xThe left side of the bounding rectangle where the image is to be drawn
yThe top of the bounding rectangle
wThe width of the bounding rectangle
hThe height of the bounding rectangle
DrawFlagsThe flags used to control the drawing

Flags used in the DrawFlags parameter

PJAI_CENTERED

Draws the image centered on the supplied rectangle. If the image is larger than the rectangle, it will be clipped. This flag can not be used with the PJAI_STRETCHED flag.
PJAI_STRETCHEDDraws the image so that it fills the supplied rectangle. Bitmaps are stretched using the StretchBlt() API function. Icons are stretched using the DrawIconEx() API function. This flag can not be used with the PJAI_CENTERED flag.
PJAI_DISABLEDDraws the image as disabled (3D Monochrome). This flag can not be used with the PJAI_GRAYSCALE flag.
PJAI_GRAYSCALEDraws the image in grayscale. This flag can not be used with the PJAI_DISABLED flag.
PJAI_TRANSPARENTDraws the image transparently. If the transparent color is not specified with the SetTransparentColor() function, the color at the top left corner (GetPixel(0, 0)) of the image is used as the transparent color. This flag has no effect if the image is an icon, as icons are transparent by default.

CSize CPJAImage::GetSize()

Returns a CSize object that contains the size of the image currently contained in the CPJAImage.

BOOL CPJAImage::SetImage(HANDLE hImage, DWORD Flags)

Selects the supplied image into the CPJAImage object. Returns TRUE if the supplied image was successfully selected, FALSE if not.

Parameters

hImageThe HANDLE of the image, can be a HBITMAP (DIB or DDB) or a HICON
FlagsSpecifies the image flags.

Flags used by the SetImage() function

PJAI_BITMAPThe given handle is an HBITMAP
PJAI_ICONThe given handle is a HICON
PJAI_AUTODELETEThe given handle will be deleted and the memory freed when a new image is set or the CPJAImage object is deleted. If this flag is not set, the user of this class is responsible for freeing the image handle when it is no longer needed

COLORREF CPJAImage::SetTransparentColor(COLORREF clr = CLR_DEFAULT)

Sets the color to be used as the transparent color for the bitmap image. Returns the previous transparent color. If CLR_DEFAULT (0xFF000000) is set as the transparent color, the color of the pixel at the top left corner ( GetPixel (0, 0) ) is used as the transparent color.

Parameters

clrThe transparent color

Demonstration Application

Be sure to check out the demo app to see the CPJAImage class in action. While the demo has a preloaded image in it, you can drag 'n drop your favorite picture files ( *.ico, *.bmp, *.jpg, or *.gif ) onto the demo app.

To change the transparent color, click the Set Color button, and then use the mouse to select the color from the picture displayed.

Enjoy!

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

 
BugBug? Pin
retinex19-Nov-11 5:17
retinex19-Nov-11 5:17 
QuestionWhat a great class! Pin
coder_kevin11-Feb-09 10:38
coder_kevin11-Feb-09 10:38 
GeneralTwice Pin
Mike Gaskey15-Sep-06 12:37
Mike Gaskey15-Sep-06 12:37 
QuestionIs this a bug? Pin
IvanBohannon27-Jan-06 17:49
IvanBohannon27-Jan-06 17:49 
AnswerRe: Is this a bug? Pin
PJ Arends28-Jan-06 6:45
professionalPJ Arends28-Jan-06 6:45 
GeneralRe: Is this a bug? Pin
IvanBohannon28-Jan-06 7:08
IvanBohannon28-Jan-06 7:08 
Generalyou are really a superstar!!!! Pin
Angus He28-Sep-05 17:37
Angus He28-Sep-05 17:37 
General5 - But a question on Picture/Static for Placeholder Pin
Garth J Lancaster31-Jul-05 17:35
professionalGarth J Lancaster31-Jul-05 17:35 
GeneralRe: 5 - But a question on Picture/Static for Placeholder Pin
PJ Arends1-Aug-05 11:03
professionalPJ Arends1-Aug-05 11:03 
GeneralRe: 5 - But a question on Picture/Static for Placeholder Pin
Garth J Lancaster1-Aug-05 19:51
professionalGarth J Lancaster1-Aug-05 19:51 
Generalone very BIG 5... Pin
Max Santos25-Jul-05 14:21
Max Santos25-Jul-05 14:21 
GeneralRe: one very BIG 5... Pin
PJ Arends31-Jul-05 7:33
professionalPJ Arends31-Jul-05 7:33 
GeneralGrayscaling a 32bpp icon kills the alpha channel Pin
sps-itsec4624-May-05 11:58
sps-itsec4624-May-05 11:58 
GeneralRe: Grayscaling a 32bpp icon kills the alpha channel Pin
sps-itsec4624-May-05 13:16
sps-itsec4624-May-05 13:16 
GeneralRe: Grayscaling a 32bpp icon kills the alpha channel Pin
PJ Arends24-May-05 16:59
professionalPJ Arends24-May-05 16:59 
GeneralRe: Grayscaling a 32bpp icon kills the alpha channel Pin
PJ Arends24-May-05 17:23
professionalPJ Arends24-May-05 17:23 
GeneralRe: Grayscaling a 32bpp icon kills the alpha channel Pin
sps-itsec4624-May-05 21:16
sps-itsec4624-May-05 21:16 
GeneralTwo transparent images on the same DC Pin
samson197820-Jan-05 12:22
samson197820-Jan-05 12:22 
GeneralRe: Two transparent images on the same DC Pin
PJ Arends21-Jan-05 9:55
professionalPJ Arends21-Jan-05 9:55 
GeneralRe: Two transparent images on the same DC Pin
samson197821-Jan-05 11:21
samson197821-Jan-05 11:21 
GeneralMake CPJAImage a Drag& Drop Control Pin
Len202017-Mar-04 11:24
Len202017-Mar-04 11:24 
GeneralRe: Make CPJAImage a Drag& Drop Control Pin
PJ Arends20-Mar-04 11:09
professionalPJ Arends20-Mar-04 11:09 
GeneralRe: Make CPJAImage a Drag& Drop Control Pin
Len202026-Mar-04 11:18
Len202026-Mar-04 11:18 
GeneralRe: Make CPJAImage a Drag& Drop Control Pin
PJ Arends26-Mar-04 17:23
professionalPJ Arends26-Mar-04 17:23 
GeneralRe: Make CPJAImage a Drag& Drop Control Pin
Len202031-Mar-04 4:20
Len202031-Mar-04 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.