How do I scale and rotate bitmaps?

Posted on 2000-02-16
Last Modified: 2013-12-03
I'd like to be able to draw bitmaps in a CDC in various sizes and rotated at certain angles.
Question by:JonRead
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 31

Expert Comment

ID: 2526539

Expert Comment

ID: 2526661

  To enlarge the bitmap loaded on a dialog, first put the following code in the else part of IsIconic loop of OnPaint. Here  IDB_BITMAP1 is the bitmap ID,


CPaintDC dc(this);
HBITMAP hbitmap = ::LoadBitmap(::AfxGetInstanceHandle(),                                                              MAKEINTRESOURCE(IDB_BITMAP1));
HDC hMemDC = ::CreateCompatibleDC(NULL);
SelectObject(hMemDC, hbitmap);
            ::StretchBlt(dc.m_hDC, 50, 50, width, hieght, hMemDC, 0, 0, 250,250, SRCCOPY);
width and hieght variables are of type integer with values 100 each, and they are declared as global.
To increase the size of the bitmap, increase the values of width and hieght.
from wherever is needed and call InvalidateRect fuction from there.

Hope this will help u.

LVL 31

Expert Comment

ID: 2526786
Hi krisp,

how can you know that JonRead want's to draw the bitmap loaded from resource onto a dialog???


Accepted Solution

Viz earned 100 total points
ID: 2528534
Rotating bitmaps can only be done on 32 bits GDI, that means on NT, but not on Windows 95 and 98. Even if the system is 32 bit, the GDI is not.
Here's the following code, you can copy and paste in your application. Then call the "DrawBitmap" to display a bitmap at a given scale, with a given radian angle, and a given transparent color.

DrawBitmap(pDC, IDB_MYBITMAP, 100, 500, 1, LEFTBOTTOM, 2, RGB(0,255,0));

Here, IDB_MYBITMAP is the ID of your resource, (100, 500) is the position of your insert or attach point, LEFTBOTTOM means the attach point is the lower left corner of your bitmap, 1 is the rotation angle in radius, 2 is the scaling factor, so twice its original size, and RGB(0,255,0) the transparent color, here the light green color will be transparent.
Here's all the source code you need, hoping the copy-paste will keep the source identations :

#include <math.h>

// attach point defines
#define LEFTTOP                                                0
#define CENTERTOP                                          1
#define RIGHTTOP                                          2
#define LEFTMIDDLE                                    3
#define CENTERMIDDLE                              4
#define RIGHTMIDDLE                                    5
#define LEFTBOTTOM                                    6
#define CENTERBOTTOM                              7
#define RIGHTBOTTOM                                    8

// don't forget to call BitmapMask.DeleteObject when you finished with it
// returns FALSE if there was a trouble

BOOL CreateMaskBmp(
                                                       CDC *pDC,                                                      //device context
                                                       CBitmap& Bitmap,                                    //input bitmap
                                                       CBitmap& BitmapMask,                        //output mask
                                                       COLORREF colorTransparent)      //transparent color
      BITMAP     StrBitmap;
      CBitmap    bmAndObject;
      CBitmap    *pbmBackOld, *pbmObjectOld;
      CDC        dcBack, dcObject, dcTemp;
      CPoint     ptSize;

      CBitmap                  *pOldBitmap;

      if (!(HBITMAP)Bitmap)
       return FALSE;

      pOldBitmap=dcTemp.SelectObject(&Bitmap);            // Select the bitmap

      Bitmap.GetObject(sizeof(StrBitmap), &StrBitmap);
      ptSize.x = StrBitmap.bmWidth;                                          // Get width of bitmap
      ptSize.y = StrBitmap.bmHeight;                                          // Get height of bitmap
      dcTemp.DPtoLP(&ptSize, 1);                                                // Convert from device to logical points

      // Create some DCs to hold temporary data.

      // Create a bitmap for each DC. DCs are required for a number of
      // GDI functions.

      // Monochrome DC
      BitmapMask.CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

      // Monochrome DC
      bmAndObject.CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

      // Each DC must select a bitmap object to store pixel data.
      pbmBackOld   = dcBack.SelectObject(&BitmapMask);
      pbmObjectOld = dcObject.SelectObject(&bmAndObject);

      // Set proper mapping mode.

      // Set the background color of the source DC to the color.
      // contained in the parts of the bitmap that should be transparent
      dcTemp.SetBkColor(colorTransparent);  // transparent color

      // Create the object mask for the bitmap by performing a BitBlt
      // from the source bitmap to a monochrome bitmap.
      dcObject.BitBlt(0, 0, ptSize.x, ptSize.y, &dcTemp, 0, 0, SRCCOPY);

      // Create the inverse of the object mask.
      dcBack.BitBlt(0, 0, ptSize.x, ptSize.y, &dcObject, 0, 0, NOTSRCCOPY);

      // Delete the memory bitmaps.

      // Delete the memory DCs.
      return TRUE;

// Computes the bitmap position corners depending on the attach point type

void GetBitmapCorners(
                                           const CPoint& AttachPnt,                  //attach point position
                                           double dfAngle,                                          //rotation angle in radian around the attach point
                                           LONG lWidth,                                                      //width of the bitmap
                                           LONG lHeight,                                                //height of the bitmap
                                           WORD wAP,                                                            //attach point type
                                           CPoint& PntLeftTop,                              //output top left point of the bitmap
                                           CPoint& PntLeftBottom,                        //output bottom left point of the bitmap
                                           CPoint& PntRightTop,                              //output top right point of the bitmap
                                           CPoint& PntRightBottom)                  //output bottom right point of the bitmap
      double dfSinAngle=sin(dfAngle);
      double dfCosAngle=cos(dfAngle);
      double dfXAp=AttachPnt.x;
      double dfYAp=AttachPnt.y;

      switch (wAP)
            case LEFTBOTTOM:



            case RIGHTBOTTOM:




            case LEFTTOP:




            case RIGHTTOP:




            case CENTERBOTTOM:




            case CENTERTOP:




            case LEFTMIDDLE:




            case RIGHTMIDDLE:




            case CENTERMIDDLE:




void DrawBitmap(
                        CDC  *pDC,                                                //device context
                        WORD wBitmapID,                                    //bitmap object resource ID
                        LONG lX,                                                      //X coordinate of the attach point
                        LONG lY,                                                      //Y coordinate of the attach point
                        double dfAngle,                                    //angle in Radians
                        WORD wAP,                                                      //attach point type : LEFTBOTTOM, LEFTTOP, ...
                        double dfSizeFactor,                  //size factor
                        COLORREF RgbTransparent)      //color of the transparent color, default is ColorLtGreen
      BITMAP            StrBitmap;
      CBitmap       BitmapTmp;
      CBitmap            BitmapMask;
      CBitmap            *pOldBitmap;
      CDC                   MemDC;
      POINT                  Pnt[3];
      CPoint            PntLeftTop, PntLeftBottom, PntRightTop, PntRightBottom;


      if (!CreateMaskBmp(pDC, BitmapTmp, BitmapMask, RgbTransparent))

      // now displays the bitmap in a given position
      pOldBitmap = MemDC.SelectObject (&BitmapTmp);
      BitmapTmp.GetObject (sizeof(StrBitmap), &StrBitmap);

       GetBitmapCorners(CPoint(lX, lY), dfAngle, (LONG)(dfSizeFactor*StrBitmap.bmWidth), (LONG)(dfSizeFactor*StrBitmap.bmHeight), wAP, PntLeftTop, PntLeftBottom, PntRightTop, PntRightBottom);




      pDC->PlgBlt(Pnt, &MemDC, 0, 0, StrBitmap.bmWidth, StrBitmap.bmHeight, BitmapMask, 0, 0);
      MemDC.SelectObject (pOldBitmap);



Author Comment

ID: 2528684
Thanks for your help.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question