CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2001
    Posts
    17

    Drawing into DC with Bitmap

    Hello everybody:

    I have done a function for painting in a DC with a bitmap as a pen.
    I don't know if this is very efficient. Is there any other way to do this?

    int Pintar(CDC * panel,RECT limites,POINT p,int r_trans,int g_trans,int b_trans)
    {
    //// draw the bitmap with identifier IDB_BITMAP1 in the p position of the panel
    //// r_trans , g_trans and b_trans are the r g b values of the color that will be transparent
    //// limites is a rect with the limits of the window of the panel

    int i,j,l;
    BITMAP bm;
    CString t;

    POINT punto;
    HINSTANCE hInst=AfxGetInstanceHandle();
    HBITMAP hbitmap=(HBITMAP)::LoadImage(hInst,MAKEINTRESOURCE(IDB_BITMAP1),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
    GetObject(hbitmap, sizeof(BITMAP), (LPVOID) &bm);

    int anchura,altura,nbpp,bsl;
    anchura=bm.bmWidth;
    altura=bm.bmHeight;
    nbpp=bm.bmBitsPixel / 8; // bits por pixel , dividimos para bytes
    bsl=bm.bmWidthBytes; // bytes por columna

    unsigned char *data;
    data=(unsigned char *)bm.bmBits;
    int r,g,b;

    for (i=0;i<altura;i++)
    {
    for (j=0,l=0;j<anchura;j++,l+=nbpp)
    {

    punto.x=p.x-(anchura/2)+j;
    punto.y=p.y+(altura/2)-i;

    b=*data++;
    g=*data++;
    r=*data++;

    if (punto.x < 2 || punto.y < 2 || punto.y>limites.bottom-limites.top-2 || punto.x>limites.right-limites.left-2 ) continue;

    if (r!=r_trans || g!=g_trans || b!=b_trans)
    {

    panel->SetPixel(punto,RGB(r,g,b));
    }


    }
    }

    return 0;
    }


    Thanks in advance
    (sorry for my english)

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Drawing into DC with Bitmap

    I'm not exactly sure what you're painting, but you might want to check out CDC::MoveTo and CDC::LineTo functions. After selecting the proper pen into the DC, you can draw lines with these functions. There are others as well, for drawing ellipses, rectangles, poly-lines, etc. These functions are much faster then you setting individual bits.

    Viggy

  3. #3
    Join Date
    Nov 2005
    Posts
    162

    Re: Drawing into DC with Bitmap

    If you can attach a screen shot about the result for your drawing,maybe I can give you some advice about this problem.

    --------------------------------------------------------------------------
    For high quality flow/diagram MFC/C++ Visio Like visualization Source Code,download XD++ at:
    http://www.********.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured