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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Font that uses "minimal ink" when printing 27 66
Unix / Linux grid computing 5 163
deburging in oracle form 12 105
bigDiff challenge 17 126
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

861 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