Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GDI / GDI+

GDI / GDI+

Scheduled Pinned Locked Moved C / C++ / MFC
winformsgraphicsannouncement
21 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Adno

    pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); // ^ this shows the window Image* pBitmap = new Image(L"clockbg.bmp"); ^ this does not they are the same image loaded differently. thank you

    H Offline
    H Offline
    Hamid Taebi
    wrote on last edited by
    #5

    And where's clockbg.bmp(path of file)?


    WhiteSky


    A 1 Reply Last reply
    0
    • H Hamid Taebi

      And where's clockbg.bmp(path of file)?


      WhiteSky


      A Offline
      A Offline
      Adno
      wrote on last edited by
      #6

      the image is in my project folder and i know it loads because i can display it like this: Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); and it shows :confused: thanks

      M 1 Reply Last reply
      0
      • A Adno

        the image is in my project folder and i know it loads because i can display it like this: Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); and it shows :confused: thanks

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #7

        Bitmap is derived from Image. If Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); works, then Image* pBitmap = new Image(L"clockbg.bmp"); graphics.DrawImage(pBitmap, 0, 0); should work. Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        A 1 Reply Last reply
        0
        • M Mark Salsbery

          Bitmap is derived from Image. If Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); works, then Image* pBitmap = new Image(L"clockbg.bmp"); graphics.DrawImage(pBitmap, 0, 0); should work. Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          A Offline
          A Offline
          Adno
          wrote on last edited by
          #8

          i know Mark thats not my problem, but when i use GDI+ to load the image: Image* pBitmap = new Image(L"clockbg.bmp"); or Bitmap. as appose to: HBITMAP pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); and i use it for my: UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA ); the window doesn't show, as i've posted in the code above is the same image and i now it loads, i dont now.. thank you

          M 1 Reply Last reply
          0
          • A Adno

            i know Mark thats not my problem, but when i use GDI+ to load the image: Image* pBitmap = new Image(L"clockbg.bmp"); or Bitmap. as appose to: HBITMAP pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); and i use it for my: UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA ); the window doesn't show, as i've posted in the code above is the same image and i now it loads, i dont now.. thank you

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #9

            Sorry about that - I should have read the first post more closely :) Maybe this will work better...

            Image* pBitmap = new Image(L"clockbg.bmp");

            HDC pDC = GetDC(hwnd);
            HDC dcMem;
            dcMem = CreateCompatibleDC(pDC);
            COLORREF colour;
            colour = RGB(255,255,255);

            // Assuming white is the transparent color
            HBITMAP hBitmap; pBitmap->GetHBITMAP(Color(0xFF,0xFF,0xFF), &hBitmap);

            SelectObject(dcMem, hBitmap);
            ...

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            A 1 Reply Last reply
            0
            • M Mark Salsbery

              Sorry about that - I should have read the first post more closely :) Maybe this will work better...

              Image* pBitmap = new Image(L"clockbg.bmp");

              HDC pDC = GetDC(hwnd);
              HDC dcMem;
              dcMem = CreateCompatibleDC(pDC);
              COLORREF colour;
              colour = RGB(255,255,255);

              // Assuming white is the transparent color
              HBITMAP hBitmap; pBitmap->GetHBITMAP(Color(0xFF,0xFF,0xFF), &hBitmap);

              SelectObject(dcMem, hBitmap);
              ...

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              A Offline
              A Offline
              Adno
              wrote on last edited by
              #10

              Thanks Mark, but my aim is to use the transparency of the png and Alpha channel to define the transparency, perpixelvlue as it were. am goind all wrong about it? thanks

              M 1 Reply Last reply
              0
              • A Adno

                Thanks Mark, but my aim is to use the transparency of the png and Alpha channel to define the transparency, perpixelvlue as it were. am goind all wrong about it? thanks

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #11

                I "think" you may be able to use a 32-bit ARGB DIBsection - I put together this test just to make sure the alpha channel stuff was working. (My "hdc" would be your "dcMem" and you wouldn't want to delete it or un-select your alpha bitmap like I have if you pass it to UpdateLayeredWindow())

                Image TransparentSrcBitmap(L"clockbg.bmp");

                LONG lImageWidth = TransparentSrcBitmap.GetWidth();
                LONG lImageHeight = TransparentSrcBitmap.GetHeight();
                WORD wBitsPerPixel = 32;

                //LONG lBytesPerRow = (((lImageWidth * (long)wBitsPerPixel + 31L) & (~31L)) / 8L);
                LONG lBytesPerRow = lImageWidth * 4;

                BITMAPINFO bmi;
                memset(&bmi, 0, sizeof(BITMAPINFO));
                bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                bmi.bmiHeader.biWidth = lImageWidth;
                bmi.bmiHeader.biHeight = lImageHeight;
                bmi.bmiHeader.biPlanes = 1;
                bmi.bmiHeader.biBitCount = wBitsPerPixel;
                bmi.bmiHeader.biCompression = BI_RGB;
                bmi.bmiHeader.biSizeImage = lBytesPerRow * lImageHeight;
                //bmi.bmiHeader.biXPelsPerMeter = 0;
                //bmi.bmiHeader.biYPelsPerMeter = 0;
                //bmi.bmiHeader.biClrUsed = 0;
                //bmi.bmiHeader.biClrImportant = 0;

                HDC hdc = ::CreateCompatibleDC(0);

                BYTE* pBitmapBits;
                HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);

                if (hBitmap)
                {
                memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);

                HGDIOBJ hOldBitmap = ::SelectObject(hdc, hBitmap);

                Graphics DstGraphics(hdc);
                DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

                //** Test - blt the memory dc to the screen to make sure alpha channel data carried over
                BLENDFUNCTION bf;
                bf.BlendOp = AC_SRC_OVER;
                bf.BlendFlags = 0;
                bf.SourceConstantAlpha = 0x7F; // Alpha multiplier applied overall - use 0xFF - 0x7F is a test
                bf.AlphaFormat = AC_SRC_ALPHA;

                HDC hClientDC = ::GetDC(*this);
                ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(),
                hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf);
                ::ReleaseDC(*this, hClientDC);
                //** End Test

                ::SelectObject(hdc, hOldBitmap); //<-- Don't do this when using with UpdateLayeredWindow()

                ::DeleteObject(hBitmap); //<-- Don't do this when using with UpdateLayeredWindow()
                }

                ::DeleteDC(hdc); //<-- Don't do this when using with UpdateLayeredWindow()

                Sorry about all the strange extra variables - I cut and paste from assorted worki

                A 1 Reply Last reply
                0
                • M Mark Salsbery

                  I "think" you may be able to use a 32-bit ARGB DIBsection - I put together this test just to make sure the alpha channel stuff was working. (My "hdc" would be your "dcMem" and you wouldn't want to delete it or un-select your alpha bitmap like I have if you pass it to UpdateLayeredWindow())

                  Image TransparentSrcBitmap(L"clockbg.bmp");

                  LONG lImageWidth = TransparentSrcBitmap.GetWidth();
                  LONG lImageHeight = TransparentSrcBitmap.GetHeight();
                  WORD wBitsPerPixel = 32;

                  //LONG lBytesPerRow = (((lImageWidth * (long)wBitsPerPixel + 31L) & (~31L)) / 8L);
                  LONG lBytesPerRow = lImageWidth * 4;

                  BITMAPINFO bmi;
                  memset(&bmi, 0, sizeof(BITMAPINFO));
                  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                  bmi.bmiHeader.biWidth = lImageWidth;
                  bmi.bmiHeader.biHeight = lImageHeight;
                  bmi.bmiHeader.biPlanes = 1;
                  bmi.bmiHeader.biBitCount = wBitsPerPixel;
                  bmi.bmiHeader.biCompression = BI_RGB;
                  bmi.bmiHeader.biSizeImage = lBytesPerRow * lImageHeight;
                  //bmi.bmiHeader.biXPelsPerMeter = 0;
                  //bmi.bmiHeader.biYPelsPerMeter = 0;
                  //bmi.bmiHeader.biClrUsed = 0;
                  //bmi.bmiHeader.biClrImportant = 0;

                  HDC hdc = ::CreateCompatibleDC(0);

                  BYTE* pBitmapBits;
                  HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);

                  if (hBitmap)
                  {
                  memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);

                  HGDIOBJ hOldBitmap = ::SelectObject(hdc, hBitmap);

                  Graphics DstGraphics(hdc);
                  DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

                  //** Test - blt the memory dc to the screen to make sure alpha channel data carried over
                  BLENDFUNCTION bf;
                  bf.BlendOp = AC_SRC_OVER;
                  bf.BlendFlags = 0;
                  bf.SourceConstantAlpha = 0x7F; // Alpha multiplier applied overall - use 0xFF - 0x7F is a test
                  bf.AlphaFormat = AC_SRC_ALPHA;

                  HDC hClientDC = ::GetDC(*this);
                  ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(),
                  hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf);
                  ::ReleaseDC(*this, hClientDC);
                  //** End Test

                  ::SelectObject(hdc, hOldBitmap); //<-- Don't do this when using with UpdateLayeredWindow()

                  ::DeleteObject(hBitmap); //<-- Don't do this when using with UpdateLayeredWindow()
                  }

                  ::DeleteDC(hdc); //<-- Don't do this when using with UpdateLayeredWindow()

                  Sorry about all the strange extra variables - I cut and paste from assorted worki

                  A Offline
                  A Offline
                  Adno
                  wrote on last edited by
                  #12

                  thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D

                  M 2 Replies Last reply
                  0
                  • A Adno

                    thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #13

                    No problem! Can you show your latest code? Mark

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    1 Reply Last reply
                    0
                    • A Adno

                      thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #14

                      What's "image" and what's "program window" in your screenshot? Never mind I get it LOL I'd still like to see your updated code if you can :) Mark

                      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                      A 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        What's "image" and what's "program window" in your screenshot? Never mind I get it LOL I'd still like to see your updated code if you can :) Mark

                        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                        A Offline
                        A Offline
                        Adno
                        wrote on last edited by
                        #15

                        Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here

                        M 2 Replies Last reply
                        0
                        • A Adno

                          Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #16

                          Try replacing int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, hdc, &ptSrc, NULL, &bf, ULW_COLORKEY ); with int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, hdc, &ptSrc, NULL, &bf, ULW_ALPHA ); You probably want to set bf.SourceConstantAlpha = 0xFF; (instead of 0x7F) too. Mark

                          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                          1 Reply Last reply
                          0
                          • A Adno

                            Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #17

                            And comment out these lines - they were there for testing only (leave in the BLENDFUNCTION since you're using that)... // comment these out HDC hClientDC = ::GetDC(hwnd); ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf); ::ReleaseDC(hwnd, hClientDC);

                            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                            A 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              And comment out these lines - they were there for testing only (leave in the BLENDFUNCTION since you're using that)... // comment these out HDC hClientDC = ::GetDC(hwnd); ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf); ::ReleaseDC(hwnd, hClientDC);

                              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                              A Offline
                              A Offline
                              Adno
                              wrote on last edited by
                              #18

                              thanks a million one more thing since i've got you here. will i be able to draw on this window? i.e: Graphics graphics(GetDC(hwnd)); Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 100,100); i did a quick test it dont seem to be working. my goal would be to be as great as you one day lol

                              M 1 Reply Last reply
                              0
                              • A Adno

                                thanks a million one more thing since i've got you here. will i be able to draw on this window? i.e: Graphics graphics(GetDC(hwnd)); Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 100,100); i did a quick test it dont seem to be working. my goal would be to be as great as you one day lol

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #19

                                Lamefif wrote:

                                will i be able to draw on this window?

                                Absolutely not! Just kidding. The DIBSection bitmap is already in a DC which is associated with a Graphics object (DstGraphics). That's where you should draw. Example...

                                // redraw the image background
                                DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

                                // draw some stuff on top of the background
                                Bitmap bitmap(L"clockbg.bmp");
                                DstGraphics.DrawImage(&bitmap, 100,100);

                                You have full access to the bitmap that IS your window. You can write directly to the pixel bytes, use GDI+ to draw, use GDI to draw, or any combination of those :) You may need to call UpdateLayeredWindow() again to see changes (i think the hdcSrc param can be NULL for updates). Mark

                                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                                A 1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  Lamefif wrote:

                                  will i be able to draw on this window?

                                  Absolutely not! Just kidding. The DIBSection bitmap is already in a DC which is associated with a Graphics object (DstGraphics). That's where you should draw. Example...

                                  // redraw the image background
                                  DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());

                                  // draw some stuff on top of the background
                                  Bitmap bitmap(L"clockbg.bmp");
                                  DstGraphics.DrawImage(&bitmap, 100,100);

                                  You have full access to the bitmap that IS your window. You can write directly to the pixel bytes, use GDI+ to draw, use GDI to draw, or any combination of those :) You may need to call UpdateLayeredWindow() again to see changes (i think the hdcSrc param can be NULL for updates). Mark

                                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                                  A Offline
                                  A Offline
                                  Adno
                                  wrote on last edited by
                                  #20

                                  Cheers Mark man i new i could do it. :->

                                  M 1 Reply Last reply
                                  0
                                  • A Adno

                                    Cheers Mark man i new i could do it. :->

                                    M Offline
                                    M Offline
                                    Mark Salsbery
                                    wrote on last edited by
                                    #21

                                    :beer:

                                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups