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. How i can Save Cimage file+ shapes & text on it?

How i can Save Cimage file+ shapes & text on it?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
10 Posts 2 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.
  • H Offline
    H Offline
    humais
    wrote on last edited by
    #1

    i load an Cimage file in my SDI MFC (non View/Doc arch.) application & draw some shapes & text on it... tell me how i can save both shapes,texts & CImage in any (jpg,bmp) file format... :confused:

    A 1 Reply Last reply
    0
    • H humais

      i load an Cimage file in my SDI MFC (non View/Doc arch.) application & draw some shapes & text on it... tell me how i can save both shapes,texts & CImage in any (jpg,bmp) file format... :confused:

      A Offline
      A Offline
      Alan Balkany
      wrote on last edited by
      #2

      I got the answer to this problem while trying to solve my problem:

      CImage image;

      // Draw on image...

      HRESULT hr = image.Save(_T("C:\\test.jpg"), Gdiplus::ImageFormatJPEG);

      H 1 Reply Last reply
      0
      • A Alan Balkany

        I got the answer to this problem while trying to solve my problem:

        CImage image;

        // Draw on image...

        HRESULT hr = image.Save(_T("C:\\test.jpg"), Gdiplus::ImageFormatJPEG);

        H Offline
        H Offline
        humais
        wrote on last edited by
        #3

        i'm drawing shapes on it(like circle,rectangle etc) using OnPaint(); .... will it work now?? :confused:

        A 1 Reply Last reply
        0
        • H humais

          i'm drawing shapes on it(like circle,rectangle etc) using OnPaint(); .... will it work now?? :confused:

          A Offline
          A Offline
          Alan Balkany
          wrote on last edited by
          #4

          No; when you use the CDC in OnPaint, your drawing is going to the screen. To draw on a bitmap you need to select the bitmap into a device context, then draw on that device context. Like:

          CDC \*mdc = GetDC ();
          
          HGDIOBJ tmp = mdc->SelectObject(hbm);     // hbm is a handle of a bitmap.
          
          // Draw on mdc, e.g. mdc->TextOutW (50, 50, L"Testing...");
          
          image.Attach (hbm);
          HRESULT hr = image.Save(\_T("C:\\\\test.tif"), Gdiplus::ImageFormatTIFF);
          
          H 1 Reply Last reply
          0
          • A Alan Balkany

            No; when you use the CDC in OnPaint, your drawing is going to the screen. To draw on a bitmap you need to select the bitmap into a device context, then draw on that device context. Like:

            CDC \*mdc = GetDC ();
            
            HGDIOBJ tmp = mdc->SelectObject(hbm);     // hbm is a handle of a bitmap.
            
            // Draw on mdc, e.g. mdc->TextOutW (50, 50, L"Testing...");
            
            image.Attach (hbm);
            HRESULT hr = image.Save(\_T("C:\\\\test.tif"), Gdiplus::ImageFormatTIFF);
            
            H Offline
            H Offline
            humais
            wrote on last edited by
            #5

            i'm not using cdc....i'm using CPaintDC dc...so now wht i have to do??

            A 1 Reply Last reply
            0
            • H humais

              i'm not using cdc....i'm using CPaintDC dc...so now wht i have to do??

              A Offline
              A Offline
              Alan Balkany
              wrote on last edited by
              #6

              CPaintDC is derived from CDC, so you can perform all the same operations on it. It's an object rather than a pointer, so you have to use "." to invoke methods instead of "->". You can select a bitmap into the CPaintDC:

              dc.SelectObject (hbm);

              Where hbm is the handle of a bitmap. You can also select a pointer to a CBitmap. (I don't see an overload for CImage). You then use the drawing methods of CDC. Then (as in my example) you can attach your bitmap to a CImage object, and save it. There may be a better way of doing this, but this should work.

              H 1 Reply Last reply
              0
              • A Alan Balkany

                CPaintDC is derived from CDC, so you can perform all the same operations on it. It's an object rather than a pointer, so you have to use "." to invoke methods instead of "->". You can select a bitmap into the CPaintDC:

                dc.SelectObject (hbm);

                Where hbm is the handle of a bitmap. You can also select a pointer to a CBitmap. (I don't see an overload for CImage). You then use the drawing methods of CDC. Then (as in my example) you can attach your bitmap to a CImage object, and save it. There may be a better way of doing this, but this should work.

                H Offline
                H Offline
                humais
                wrote on last edited by
                #7

                what is hbm? plz write full code in detail... :confused:

                A 1 Reply Last reply
                0
                • H humais

                  what is hbm? plz write full code in detail... :confused:

                  A Offline
                  A Offline
                  Alan Balkany
                  wrote on last edited by
                  #8

                  hbm is a handle to a bitmap. The Windows type is HBITMAP.

                  H 1 Reply Last reply
                  0
                  • A Alan Balkany

                    hbm is a handle to a bitmap. The Windows type is HBITMAP.

                    H Offline
                    H Offline
                    humais
                    wrote on last edited by
                    #9

                    Runtime error: tht hbm is not initialized....?

                    A 1 Reply Last reply
                    0
                    • H humais

                      Runtime error: tht hbm is not initialized....?

                      A Offline
                      A Offline
                      Alan Balkany
                      wrote on last edited by
                      #10

                      Use CreateCompatibleBitmap (): http://msdn.microsoft.com/en-us/library/dd183488%28VS.85%29.aspx[^] In your OnPaint method, you have a device context, which can be used to create a compatible bitmap. You can then draw on this bitmap, then attach it to a CImage object and save it.

                      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