Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I scale and rotate bitmaps?

Posted on 2000-02-16
5
Medium Priority
?
1,631 Views
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.
0
Comment
Question by:JonRead
[X]
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
5 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 2526539
0
 

Expert Comment

by:krisp
ID: 2526661
Hi,

  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);
::DeleteDC(hMemDC);
::DeleteObject(hbitmap);
--------------------------------------------------------------------------------------------
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.

cheers,
Krisp
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2526786
Hi krisp,

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

ZOPPO
0
 

Accepted Solution

by:
Viz earned 300 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;

      dcTemp.CreateCompatibleDC(pDC);
      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.
      dcBack.CreateCompatibleDC(pDC);
      dcObject.CreateCompatibleDC(pDC);

      // 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.
      dcTemp.SetMapMode(pDC->GetMapMode());

      // 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.
      dcBack.SelectObject(pbmBackOld);
      dcObject.SelectObject(pbmObjectOld)->DeleteObject();
      dcTemp.SelectObject(pOldBitmap);

      // Delete the memory DCs.
      dcBack.DeleteDC();
      dcObject.DeleteDC();
      dcTemp.DeleteDC();
      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:
                  PntLeftTop.x=(int)(dfXAp-lHeight*dfSinAngle);
                  PntLeftTop.y=(int)(dfYAp-lHeight*dfCosAngle);
      
                  PntLeftBottom=AttachPnt;

                  PntRightBottom.x=(int)(dfXAp+lWidth*dfCosAngle);
                  PntRightBottom.y=(int)(dfYAp-lWidth*dfSinAngle);

                  PntRightTop.x=PntRightBottom.x-(AttachPnt.x-PntLeftTop.x);
                  PntRightTop.y=PntRightBottom.y-(AttachPnt.y-PntLeftTop.y);
                  break;

            case RIGHTBOTTOM:
                  PntLeftBottom.x=(int)(dfXAp-lWidth*dfCosAngle);
                  PntLeftBottom.y=(int)(dfYAp+lWidth*dfSinAngle);

                  PntRightBottom=AttachPnt;

                  PntRightTop.x=(int)(dfXAp-lHeight*dfSinAngle);
                  PntRightTop.y=(int)(dfYAp-lHeight*dfCosAngle);

                  PntLeftTop.x=PntLeftBottom.x-(AttachPnt.x-PntRightTop.x);
                  PntLeftTop.y=PntLeftBottom.y-(AttachPnt.y-PntRightTop.y);
                  break;

            case LEFTTOP:
                  PntLeftTop=AttachPnt;

                  PntRightTop.x=(int)(dfXAp+lWidth*dfCosAngle);
                  PntRightTop.y=(int)(dfYAp-lWidth*dfSinAngle);

                  PntLeftBottom.x=(int)(dfXAp+lHeight*dfSinAngle);
                  PntLeftBottom.y=(int)(dfYAp+lHeight*dfCosAngle);

                  PntRightBottom.x=PntRightTop.x+(PntLeftBottom.x-AttachPnt.x);
                  PntRightBottom.y=PntRightTop.y+(PntLeftBottom.y-AttachPnt.y);
                  break;

            case RIGHTTOP:
                  PntRightTop=AttachPnt;

                  PntLeftTop.x=(int)(dfXAp-lWidth*dfCosAngle);
                  PntLeftTop.y=(int)(dfYAp+lWidth*dfSinAngle);

                  PntRightBottom.x=(int)(dfXAp+lHeight*dfSinAngle);
                  PntRightBottom.y=(int)(dfYAp+lHeight*dfCosAngle);

                  PntLeftBottom.x=PntLeftTop.x+(PntRightBottom.x-AttachPnt.x);
                  PntLeftBottom.y=PntLeftTop.y+(PntRightBottom.y-AttachPnt.y);
                  break;

            case CENTERBOTTOM:
                  PntLeftBottom.x=(int)(dfXAp-.5*lWidth*dfCosAngle);
                  PntLeftBottom.y=(int)(dfYAp+.5*lWidth*dfSinAngle);

                  PntRightBottom.x=(int)(dfXAp+.5*lWidth*dfCosAngle);
                  PntRightBottom.y=(int)(dfYAp-.5*lWidth*dfSinAngle);

                  PntLeftTop.x=PntLeftBottom.x-(int)(lHeight*dfSinAngle);
                  PntLeftTop.y=PntLeftBottom.y-(int)(lHeight*dfCosAngle);

                  PntRightTop.x=PntRightBottom.x-(int)(lHeight*dfSinAngle);
                  PntRightTop.y=PntRightBottom.y-(int)(lHeight*dfCosAngle);
                  break;

            case CENTERTOP:
                  PntLeftTop.x=(int)(dfXAp-.5*lWidth*dfCosAngle);
                  PntLeftTop.y=(int)(dfYAp+.5*lWidth*dfSinAngle);

                  PntRightTop.x=(int)(dfXAp+.5*lWidth*dfCosAngle);
                  PntRightTop.y=(int)(dfYAp-.5*lWidth*dfSinAngle);

                  PntLeftBottom.x=PntLeftTop.x+(int)(lHeight*dfSinAngle);
                  PntLeftBottom.y=PntLeftTop.y+(int)(lHeight*dfCosAngle);

                  PntRightBottom.x=PntRightTop.x+(int)(lHeight*dfSinAngle);
                  PntRightBottom.y=PntRightTop.y+(int)(lHeight*dfCosAngle);
                  break;

            case LEFTMIDDLE:
                  PntLeftTop.x=(int)(dfXAp-.5*lHeight*dfSinAngle);
                  PntLeftTop.y=(int)(dfYAp-.5*lHeight*dfCosAngle);

                  PntLeftBottom.x=(int)(dfXAp+.5*lHeight*dfSinAngle);
                  PntLeftBottom.y=(int)(dfYAp+.5*lHeight*dfCosAngle);

                  PntRightBottom.x=PntLeftBottom.x+(int)(lWidth*dfCosAngle);
                  PntRightBottom.y=PntLeftBottom.y+(int)(lWidth*dfSinAngle);

                  PntRightTop.x=PntLeftTop.x+(int)(lWidth*dfCosAngle);
                  PntRightTop.y=PntLeftTop.y+(int)(lWidth*dfSinAngle);
                  break;

            case RIGHTMIDDLE:
                  PntRightTop.x=(int)(dfXAp-.5*lHeight*dfSinAngle);
                  PntRightTop.y=(int)(dfYAp-.5*lHeight*dfCosAngle);

                  PntRightBottom.x=(int)(dfXAp+.5*lHeight*dfSinAngle);
                  PntRightBottom.y=(int)(dfYAp+.5*lHeight*dfCosAngle);

                  PntLeftBottom.x=PntRightBottom.x-(int)(lWidth*dfCosAngle);
                  PntLeftBottom.y=PntRightBottom.y+(int)(lWidth*dfSinAngle);

                  PntLeftTop.x=PntRightTop.x-(int)(lWidth*dfCosAngle);
                  PntLeftTop.y=PntRightTop.y+(int)(lWidth*dfSinAngle);
                  break;

            case CENTERMIDDLE:
                  PntRightTop.x=(int)(dfXAp-.5*lHeight*dfSinAngle+.5*lWidth*dfCosAngle);
                  PntRightTop.y=(int)(dfYAp-.5*lHeight*dfCosAngle-.5*lWidth*dfSinAngle);

                  PntRightBottom.x=(int)(dfXAp+.5*lHeight*dfSinAngle+.5*lWidth*dfCosAngle);
                  PntRightBottom.y=(int)(dfYAp+.5*lHeight*dfCosAngle-.5*lWidth*dfSinAngle);

                  PntLeftTop.x=(int)(dfXAp-.5*lHeight*dfSinAngle-.5*lWidth*dfCosAngle);
                  PntLeftTop.y=(int)(dfYAp-.5*lHeight*dfCosAngle+.5*lWidth*dfSinAngle);

                  PntLeftBottom.x=(int)(dfXAp+.5*lHeight*dfSinAngle-.5*lWidth*dfCosAngle);
                  PntLeftBottom.y=(int)(dfYAp+.5*lHeight*dfCosAngle+.5*lWidth*dfSinAngle);
                  break;
      }
}

//**************************************************************************************
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;

      BitmapTmp.LoadBitmap(wBitmapID);

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

      // now displays the bitmap in a given position
      MemDC.CreateCompatibleDC(pDC);
      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);

      Pnt[0].x=PntLeftTop.x;
      Pnt[0].y=PntLeftTop.y;

      Pnt[1].x=PntRightTop.x;
      Pnt[1].y=PntRightTop.y;

      Pnt[2].x=PntLeftBottom.x;
      Pnt[2].y=PntLeftBottom.y;

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

      BitmapTmp.DeleteObject();
}


0
 

Author Comment

by:JonRead
ID: 2528684
Thanks for your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

609 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