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

Drawimage

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicslearning
12 Posts 6 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.
  • J john5632

    I am trying to draw image using bitblt method. When I try to

    CBitmap bitmap;
    bitmap.LoadBitmap(IDB_BITMAP1);

    it works and image is getting drawn but when I use

    Bitmap *bitmap = Bitmap::FromFile(L"d:\\Projects\\res\\state.bmp");

    It does not draw any image... Pleas help to sort out the problem. I need to draw image directly from path, not from resource and using bitblt only..

    A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #2

    I coded like this for my application.

    CString bmpFilePath;
    HBITMAP hBmp;
    hBmp = (HBITMAP)::LoadImage(NULL,bmpFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    CBitmap bmp;
    bmp.Attach(hBmp);
    CDC dcImage;
    if(!dcImage.CreateCompatibleDC(pDC))
    return;
    BITMAP bm;
    CBitmap* pOldBitmap;
    bmp.GetBitmap(&bm);
    pOldBitmap = dcImage.SelectObject(&bmp);
    pDC->BitBlt(350,100, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
    dcImage.SelectObject(pOldBitmap);

    Anu

    J 2 Replies Last reply
    0
    • A Anu_Bala

      I coded like this for my application.

      CString bmpFilePath;
      HBITMAP hBmp;
      hBmp = (HBITMAP)::LoadImage(NULL,bmpFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
      CBitmap bmp;
      bmp.Attach(hBmp);
      CDC dcImage;
      if(!dcImage.CreateCompatibleDC(pDC))
      return;
      BITMAP bm;
      CBitmap* pOldBitmap;
      bmp.GetBitmap(&bm);
      pOldBitmap = dcImage.SelectObject(&bmp);
      pDC->BitBlt(350,100, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
      dcImage.SelectObject(pOldBitmap);

      Anu

      J Offline
      J Offline
      john5632
      wrote on last edited by
      #3

      LoadImage is returning NULL? and nothing is getting drawn.

      A 1 Reply Last reply
      0
      • A Anu_Bala

        I coded like this for my application.

        CString bmpFilePath;
        HBITMAP hBmp;
        hBmp = (HBITMAP)::LoadImage(NULL,bmpFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
        CBitmap bmp;
        bmp.Attach(hBmp);
        CDC dcImage;
        if(!dcImage.CreateCompatibleDC(pDC))
        return;
        BITMAP bm;
        CBitmap* pOldBitmap;
        bmp.GetBitmap(&bm);
        pOldBitmap = dcImage.SelectObject(&bmp);
        pDC->BitBlt(350,100, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
        dcImage.SelectObject(pOldBitmap);

        Anu

        J Offline
        J Offline
        john5632
        wrote on last edited by
        #4

        Thanks its working...but what I need to change in code If I load a png/jpg image...

        enhzflepE T 2 Replies Last reply
        0
        • J john5632

          LoadImage is returning NULL? and nothing is getting drawn.

          A Offline
          A Offline
          Anu_Bala
          wrote on last edited by
          #5

          Check bmp file is avaialble in folder.Otherwise it returns NULL.

          Anu

          1 Reply Last reply
          0
          • J john5632

            I am trying to draw image using bitblt method. When I try to

            CBitmap bitmap;
            bitmap.LoadBitmap(IDB_BITMAP1);

            it works and image is getting drawn but when I use

            Bitmap *bitmap = Bitmap::FromFile(L"d:\\Projects\\res\\state.bmp");

            It does not draw any image... Pleas help to sort out the problem. I need to draw image directly from path, not from resource and using bitblt only..

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #6

            You've not shown the code that actually draws the image, but I'll take a guess at the problem. In your first code snippet, it is a CBitmap instance that will be used for drawing. In your second snippet, it is a pointer to a Bitmap instance that is used. Your drawing code will need to be adjusted accordingly. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            J 1 Reply Last reply
            0
            • J john5632

              Thanks its working...but what I need to change in code If I load a png/jpg image...

              enhzflepE Offline
              enhzflepE Offline
              enhzflep
              wrote on last edited by
              #7

              I use this code

              // BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
              HBITMAP mLoadImg(WCHAR *szFilename)
              {
              HBITMAP result=NULL;

              Gdiplus::Bitmap\* bitmap = new Gdiplus::Bitmap(szFilename,false);
              bitmap->GetHBITMAP(NULL, &result);
              delete bitmap;
              return result;
              

              }

              Note: You have to startup and shutdown the GDI+ system. I usually do this in apps initialization and destruction (winmain or main if not mfc apps)

              int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
              {
              Gdiplus::GdiplusStartupInput gdiplusStartupInput;
              ULONG_PTR gdiplusToken;
              Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

              hInst = hInstance;
              InitCommonControls();
              // The user interface is a modal dialog box
              DialogBox(hInstance, MAKEINTRESOURCE(DLG\_MAIN), NULL, (DLGPROC)DialogProc);
              Gdiplus::GdiplusShutdown(gdiplusToken);
              

              }

              1 Reply Last reply
              0
              • C Chris Meech

                You've not shown the code that actually draws the image, but I'll take a guess at the problem. In your first code snippet, it is a CBitmap instance that will be used for drawing. In your second snippet, it is a pointer to a Bitmap instance that is used. Your drawing code will need to be adjusted accordingly. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                J Offline
                J Offline
                john5632
                wrote on last edited by
                #8

                :), basically my motive is to draw image (png/jpg) using bitblt...I will adjust code accordingly.

                A 1 Reply Last reply
                0
                • J john5632

                  :), basically my motive is to draw image (png/jpg) using bitblt...I will adjust code accordingly.

                  A Offline
                  A Offline
                  Anu_Bala
                  wrote on last edited by
                  #9

                  See this link.It may help you. http://stackoverflow.com/questions/2490661/how-to-load-png-jpeg-images-using-mfc[^]

                  Anu

                  J 1 Reply Last reply
                  0
                  • A Anu_Bala

                    See this link.It may help you. http://stackoverflow.com/questions/2490661/how-to-load-png-jpeg-images-using-mfc[^]

                    Anu

                    J Offline
                    J Offline
                    john5632
                    wrote on last edited by
                    #10

                    Thank you for help...I am able to draw image. I need your help in one more problem as described below: I need to draw image on the screen using XOR so that diff of 2 images can be draw as a complete image. How to solve this?

                    1 Reply Last reply
                    0
                    • J john5632

                      I am trying to draw image using bitblt method. When I try to

                      CBitmap bitmap;
                      bitmap.LoadBitmap(IDB_BITMAP1);

                      it works and image is getting drawn but when I use

                      Bitmap *bitmap = Bitmap::FromFile(L"d:\\Projects\\res\\state.bmp");

                      It does not draw any image... Pleas help to sort out the problem. I need to draw image directly from path, not from resource and using bitblt only..

                      W Offline
                      W Offline
                      wangafei
                      wrote on last edited by
                      #11

                      first you can get the path,then set ID and path link! so,your first method can do it

                      1 Reply Last reply
                      0
                      • J john5632

                        Thanks its working...but what I need to change in code If I load a png/jpg image...

                        T Offline
                        T Offline
                        ThatsAlok
                        wrote on last edited by
                        #12

                        MFC by default support only BMP file, however you can take services of different library that suits your need, one of them is gdiplus, search Google for more information

                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                        Never mind - my own stupidity is the source of every "problem" - Mixture

                        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                        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