Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:28:21+00:00 2026-05-22T15:28:21+00:00

Question is rather simple but I couldn’t find nice & clean solution to my

  • 0

Question is rather simple but I couldn’t find nice & clean solution to my problem on the Internet.
What I got are some drawings on my window. Now, I can save those using BitBlt function from window device context to image device context, and also from there to bitmap handle:

HDC bitmapDC = CreateCompatibleDC(dc);
HBITMAP bitmap = CreateCompatibleBitmap(bitmapDC, 200, 200);
SelectObject(bitmapDC,bitmap);
BitBlt(bitmapDC, 0, 0, 200, 200, dc, 200, 200, SRCCOPY);

But from there I’m lost. I had a look at GDI+ Bitmap class which got save function, and I found how to implement code for retrieving CLSID of picture encoding. However I don’t know if I use loading to that class correctly. There’s overloaded constructor for HBITMAP, but it’s also asking for some palette, which I set to NULL:

Bitmap image(bitmap,NULL);

I tried to save png file but it resulted in black quare without those drawings I was expecting. If you’d like, full code for my painting procedure:

void GetCLSID(const WCHAR* format, CLSID* pClsid){
    UINT  num = 0;          // number of image encoders
    UINT  size = 0;         // size of the image encoder array in bytes

    ImageCodecInfo* pImageCodecInfo = NULL;

    GetImageEncodersSize(&num, &size);

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));

    GetImageEncoders(num, size, pImageCodecInfo);

    for(UINT j = 0; j < num; ++j)
    {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
      }    
    }
}

void OnPaint(HDC dc){
    RECT rect; rect.bottom = 0; rect.top = 20; rect.left = 0; rect.right = 100;
    HBRUSH blueBrush = CreateSolidBrush(RGB(0,0,200));
    FillRect(dc, &rect, blueBrush);

    Graphics graphics(dc);
    Pen      pen(Color(255, 0, 0, 255));
    graphics.DrawLine(&pen, 0, 0, 200, 100);

    SolidBrush greenBrush(Color(0,200,0));
    Rect ellipseRect(20,20,20,20);
    graphics.FillEllipse(&greenBrush, ellipseRect);

    SolidBrush redBrush(Color(200,0,0));
    Rect boxRectangle(0,40,20,100);
    graphics.FillRectangle(&redBrush, boxRectangle);

    pen.SetColor(Color(200,0,200));
    pen.SetWidth(20);
    graphics.DrawBezier(&pen, 100, 20, 130, 40, 200, 10, 230, 20);

    HDC bitmapDC = CreateCompatibleDC(dc);
    HBITMAP bitmap = CreateCompatibleBitmap(bitmapDC, 200, 200);
    SelectObject(bitmapDC,bitmap);
    BitBlt(bitmapDC, 0, 0, 500, 500, dc, 500, 500, SRCCOPY);

    Bitmap image(bitmap,NULL);
    CLSID clsID;
    GetCLSID(L"image/png", &clsID);
    image.Save(L"pic.png", &clsID);

}

I couldn’t even imagine that simple saving will be such problem, so I’ll be glad for any help, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:28:22+00:00Added an answer on May 22, 2026 at 3:28 pm

    I gave code here which does pretty much what you want:
    How to save the client area of a child Window to a Bitmap file?

    It is very verbose in C. It’s a lot better in C++ because of CImage

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched google but couldn't find an answer to this rather simple question. I
My question is rather simple, but I couldn't find an answer, could be because
This is a simple question with a possible answer, but couldn't I find it
Rather a simple question. But the implications are vast. Over the last few weeks
I think, the answer to my question is rather simple, but I just can't
simple and annoying question for you, but i need the answer rather quick and
I'm hoping this is a rather simple question, but I'm pretty new to MVC
I'm having difficulties believing this question hasn't been asked before, but I couldn't find
Question is pretty self explanitory. I want to do a simple find and replace,
This question was asked already here , but rather than answering the specific question,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.