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

  • SEARCH
  • Home
  • 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 3622766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:18:01+00:00 2026-05-18T23:18:01+00:00

In my code I quickly generate images on the fly, and I want to

  • 0

In my code I quickly generate images on the fly, and I want to display them as quickly as possible. So the first time I create my image, I create a new BITMAP, but instead of deleting the old one and creating a new one for every subsequent image, I just want to copy my data back into the existing one. Here is my code to do both the initial creation and the updating. The creation works just fine, but the updating one doesn’t work.

BITMAPINFO bi;
HBITMAP Frame::CreateBitmap(HWND hwnd, int tol1, int tol2, bool useWhite, bool useBackground)
{
  ZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));
  bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bi.bmiHeader.biWidth = width;
  bi.bmiHeader.biHeight = height;
  bi.bmiHeader.biPlanes = 1;
  bi.bmiHeader.biBitCount = 24;
  bi.bmiHeader.biCompression = BI_RGB;
  ZeroMemory(bi.bmiColors, sizeof(RGBQUAD));

  // Allocate memory for bitmap bits
  int size = height * width;
  Pixel* newPixels = new Pixel[size];

  // Recompute the output
  //memcpy(newPixels, pixels, size*3);
  ComputeOutput(newPixels, tol1, tol2, useWhite, useBackground);

  HBITMAP bitmap = CreateDIBitmap(GetDC(hwnd), &bi.bmiHeader, CBM_INIT, newPixels, &bi, DIB_RGB_COLORS);
  delete newPixels;

  return bitmap;
}

and

void Frame::UpdateBitmap(HWND hwnd, HBITMAP bitmap, int tol1, int tol2, bool useWhite, bool useBackground)
{
  ZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));
  bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

  HDC hdc = GetDC(hwnd);
  if(!GetDIBits(hdc, bitmap, 0, bi.bmiHeader.biHeight, NULL, &bi, DIB_RGB_COLORS))
    MessageBox(NULL, "Can't get base image info!", "Error!", MB_ICONEXCLAMATION | MB_OK);

  // Allocate memory for bitmap bits
  int size = height * width;
  Pixel* newPixels = new Pixel[size];

  // Recompute the output
  //memcpy(newPixels, pixels, size*3);
  ComputeOutput(newPixels, tol1, tol2, useWhite, useBackground);

  // Push back to windows
  if(!SetDIBits(hdc, bitmap, 0, bi.bmiHeader.biHeight, newPixels, &bi, DIB_RGB_COLORS))
      MessageBox(NULL, "Can't set pixel data!", "Error!", MB_ICONEXCLAMATION | MB_OK);

  delete newPixels;
}

where the Pixel struct is just this:

struct Pixel { unsigned char b, g, r; };

Why does my update function not work. I always get the MessageBox for “Can’t set pixel data!” I used code similar to this when I was loading in the original bitmap from file, then editing the data, but now when I manually create it, it doesn’t work.

  • 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-18T23:18:01+00:00Added an answer on May 18, 2026 at 11:18 pm

    to write to a bitmap you have to Select it into a DeviceContext:

    hdcMemDC = CreateCompatibleDC(hdc); 
    ... // adjust hdcMem to right sizes (see MSDN : Capturing an Image)
    SelectObject(hdcMemDC,bitmap);
    ... // your Bitmap drawing
    BitBlt( hdcMemDC, ... ) // to copy to hdc.
    
    ReleaseDC(hWnd, hdcMemDC);
    

    Also CreateDIBitmap( GetDC(hwnd),… ) needs a ReleaseDC(hWnd ,??? );

    You must assign GetDC(…) to a variable so you can release it.

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

Sidebar

Related Questions

Is it possible to move quickly from one place in code to another where
Possible Duplicate: Generate MD5 hash in Java Hi, I want to compute the MD5
How can I write the following code quickly in emacs? \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B}
During a code review I presented a method quickly to the team that I
I love using irb to quickly check how my ruby code reacts. Im wondering
Here is an old add in to quickly comment out HTML code. Maybe I
Logging can get complicated, quickly. Considering that you have some code, how do you
I was reading through this interesting post about using JavaScript to generate an image
I need to generate quickly lots of random numbers from binomial distributions for dramatically
What tools have you used to create class source code from xml files? Is

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.