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. Inserting an image into a view

Inserting an image into a view

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 1 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 Offline
    J Offline
    Jose Luis Sogorb
    wrote on last edited by
    #1

    Hi: which is the easiest way to insert a little image (maybe a BMP 30x30 pixels) in specific coordinates (x, y) into a view? Thanks.

    A 1 Reply Last reply
    0
    • J Jose Luis Sogorb

      Hi: which is the easiest way to insert a little image (maybe a BMP 30x30 pixels) in specific coordinates (x, y) into a view? Thanks.

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      In the OnDraw handler of the view class, first create a HBITMAP struct. Then use LoadImage to load the bitmap image from a file into the struct. Then create a CBitmap object and attach it to the HBITMAP. Then use standard BitBlt techniques to blit the bitmap into the view. Here's a code sample:

      void CMyView::OnDraw(CDC* pDC)
      {
      HBITMAP hbmpMyBitmap;
      hbmpMyBitmap = (HBITMAP) LoadImage( NULL, "C:\\Windows\\MyBitmap.bmp",
      IMAGE_BITMAP, 0, 0,
      LR_LOADFROMFILE );

      CBitmap objBitmap;
      objBitmap.Attach( hbmpMyBitmap );

      CDC tempDC;
      tempDC.CreateCompatibleDC( pDC );
      CBitmap* pOldBitmap = (CBitmap*) tempDC.SelectObject( &objBitmap );

      pDC->BitBlt(x, y, width, height, &tempDC, 0, 0, SRCCOPY);

      tempDC.SelectObject( pOldBitmap );

      return;
      }

      Something like this. Remember to replace file path, destination coordinates and bitmap width and height with the correct values. Hope it helps, Antti Keskinen ---------------------------------------------- "If we wrote a report stating we saw a jet fighter with a howitzer, who's going to believe us ?" -- R.A.F. pilot quote on seeing a Me 262 armed with a 50mm Mauser cannon.

      J 1 Reply Last reply
      0
      • A Antti Keskinen

        In the OnDraw handler of the view class, first create a HBITMAP struct. Then use LoadImage to load the bitmap image from a file into the struct. Then create a CBitmap object and attach it to the HBITMAP. Then use standard BitBlt techniques to blit the bitmap into the view. Here's a code sample:

        void CMyView::OnDraw(CDC* pDC)
        {
        HBITMAP hbmpMyBitmap;
        hbmpMyBitmap = (HBITMAP) LoadImage( NULL, "C:\\Windows\\MyBitmap.bmp",
        IMAGE_BITMAP, 0, 0,
        LR_LOADFROMFILE );

        CBitmap objBitmap;
        objBitmap.Attach( hbmpMyBitmap );

        CDC tempDC;
        tempDC.CreateCompatibleDC( pDC );
        CBitmap* pOldBitmap = (CBitmap*) tempDC.SelectObject( &objBitmap );

        pDC->BitBlt(x, y, width, height, &tempDC, 0, 0, SRCCOPY);

        tempDC.SelectObject( pOldBitmap );

        return;
        }

        Something like this. Remember to replace file path, destination coordinates and bitmap width and height with the correct values. Hope it helps, Antti Keskinen ---------------------------------------------- "If we wrote a report stating we saw a jet fighter with a howitzer, who's going to believe us ?" -- R.A.F. pilot quote on seeing a Me 262 armed with a 50mm Mauser cannon.

        J Offline
        J Offline
        Jose Luis Sogorb
        wrote on last edited by
        #3

        Thank 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