Logical matter for memory allocation while managing my objects in C++ - c++

I have some problem with my designs for function, and I do not find a proper solution (I am quite a beginner in C++).
Some days ago, I ask another question which is linked to this one.
So, I have a function that makes a screen capture. It works perfectly.
The fact is that I want this function to return an Image object from a class I implemented.
Basically, it is:
class Image {
public:
BYTE *bitPointer;
int width;
int height;
};
And my function is like this one:
Image screenCapture(int width, int height) {
HDC hdcTemp, hdc;
BYTE* bitPointer;
hdc = GetDC(HWND_DESKTOP);
hdcTemp = CreateCompatibleDC(hdc);
BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = width;
bitmap.bmiHeader.biHeight = -height;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 24;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = 0;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
SelectObject(hdcTemp, hBitmap);
BitBlt(hdcTemp, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
ReleaseDC(HWND_DESKTOP, hdc);
DeleteDC(hdcTemp);
Image screen;
screen.bitPointer = bitPointer;
screen.width = width;
screen.height = height;
return screen;
}
The bitPointer created using CreateDIBSection is actually a pointer to the value of my first pixel (if I understood well).
Then, I can use a simple loop:
for (int i = 0; i >= 0; i++) {
cout << i << ": " << (int)screenCapture(1366, 768).bitPointer[0] << endl;
}
Here is my issue.
I have to free up the hBitmap by calling DeleteObject(hBitmap) otherwise I would have no more memory space (and the loop finally crash).
However, I do not know where to do it.
I would like to do it within my function but if I call DeleteObject(), then it will destroy my bitPointer too and therefore I will not be able to access pixels from my Image object returned.
In fact, I am a little confused with the fact that my bitPointer variable is a pointer. It means that I can not copy it to prevent it being destroyed after. I can not find the solution.
Two solutions I have tried:
Create a destructor calling DeleteObject() for my class Image.
It did not work because the destructor is call from the function and not only from inside my loop.
Add an attribute HBITMAP hBitmap to my class Image, and call DeleteObject() from inside my loop.
It is not really convenient, moreover I have to declare a new Image object, I can not do it anonymously like in the loop I writed.
So, I am stuck, could anyone help me please?

You can pass hBitmap as an argument to screenCapture function.
screenCapture(int width, int height, HBITMAP& hBitmap)
This way you get to destroy it when you want without interfering with Image members. You declare an "empty" hBitmap outside screenCapture function and pass it as argument. The function will use the reference to create the object and you will be able to destroy it outside of screenCapture after you are done with using bitPointer values.
If you need storing all screenshots, you need to implement a working buffer as you have finite amount of memory.

Related

How to use CreateCompatibleDC(), SetPixel(), and BitBlt() to display an image?

I'm trying to draw and display an image(s) on a device context (variable: dc) by using CreateCompatibleDC(), SetPixel(), and BitBlt() as seen in the code below:
HDC Layout = CreateCompatibleDC(0);
HBITMAP image = CreateCompatibleBitmap(Layout, symbol->bitmap_width, symbol->bitmap_height);
// Draw the image
int bit = 0;
for (int j = 0; j < symbol->bitmap_height; j++) {
for (int k = 0; k < symbol->bitmap_width; k++) {
if (symbol->bitmap[bit] == '1')
SetPixel(Layout, j, k, rgbBlue);
else
SetPixel(Layout, j, k, rgbGreen);
bit++;
}
}
BOOL success = BitBlt(dc, 1000, 1000, 1000, 1000, BCLayout, 0, 0, SRCCOPY);
I expected the image to be displayed in said device context but the image does not display in the end. Does anyone know why that is?
A few things I should clarify:
the variable "symbol" is a struct variable that holds all the information for the image
the symbol->bitmap array is a character array that has characters that denote the color of a pixel on the bitmap representation of the image (why it's one-dimensional, I don't know. It was designed that way by a third party)
CreateCompatibleDC() creates an in-memory HDC with a 1x1 monochrome HBITMAP assigned to it by default. You need to use SelectObject() to replace that default HBITMAP with your own HBITMAP before you then use SetPixel() to change the HDC's pixels, eg:
// create an HDC...
HDC Layout = CreateCompatibleDC(0);
// create a bitmap for the HDC...
HBITMAP image = CreateCompatibleBitmap(Layout, symbol->bitmap_width, symbol->bitmap_height);
// replace the default bitmap with the new one...
// remember the old bitmap for later...
HBITMAP oldBmp = (HBITMAP) SelectObject(Layout, image);
// Draw the image as needed...
// restore the previous bitmap...
SelectObject(Layout, oldBmp);
// destroy the new bitmap...
DeleteObject(image);
// destroy the HDC...
DeleteDC(Layout);

Editing bitmap in memory using C++

I have a two dimensional array of data that I want to display as an image.
The plan goes something like this -
Create a bitmap using CreateCompatibleBitmap (this results in a solid black bitmap and I can display this with no problems)
Edit the pixels of this bitmap to match my data
BitBlt the bitmap to the window
I think that I need a pointer to the place in memory where the pixel data begins. I've tried many different methods of doing this and googled it for 3 days and still haven't been able to even edit a single pixel.
Using a loop of SetPixel(HDC, x, y, Color) to set each pixel works but VERY slowly.
I have accomplished this in C# by locking the bitmap and editing the bits, but I am new to C++ and can't seem to figure out how to do something similar.
I have mostly been trying to use memset(p, value, length)
For "p" I have tried using the handle returned from CreateCompatibleBitmap, the DC for the bitmap, and the DC for the window. I have tried all sorts of values for the value and length.
I'm not sure if this is the right thing to use though.
I don't have to use a bitmap, that's just the only thing I know to do. Actually it would be awesome to find a way to directly change the main window's DC.
I do want to avoid libraries though. I am doing this purely for learning C++.
This took QUITE a bit of research so I'll post exactly how it is done for anyone else who may be looking.
This colors every pixel red.
hDC = BeginPaint(hWnd, &Ps);
const int
width = 400,
height = 400,
size = width * height * 3;
byte * data;
data = new byte[size];
for (int i = 0; i < size; i += 3)
{
data[i] = 0;
data[i + 1] = 0;
data[i + 2] = 255;
}
BITMAPINFOHEADER bmih;
bmih.biBitCount = 24;
bmih.biClrImportant = 0;
bmih.biClrUsed = 0;
bmih.biCompression = BI_RGB;
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biPlanes = 1;
bmih.biSize = 40;
bmih.biSizeImage = size;
BITMAPINFO bmpi;
bmpi.bmiHeader = bmih;
SetDIBitsToDevice(hDC, 0, 0, width, height, 0, 0, 0, height, data, &bmpi, DIB_RGB_COLORS);
delete[] data;
memset can be used on the actually RGB information array (but you need to also know the format of the bitmap, if a pixel has 32 or 24 bits ).
From a bit of research on msdn, it seems that what you want to get is the BITMAP structure :
http://msdn.microsoft.com/en-us/library/k1sf4cx2.aspx
There you have the bmBits on which you can memset.
How to get there from your function ?
Well, CreateCompatibleBitmap returns a HBITMAP structure and it seems you can get BITMAP from HBITMAP with the following code :
BITMAP bmp;
GetObject(hBmp, sizeof(BITMAP), &bmp);
This however seems to get a you copy of the existing bitmap info, which only solves your memset problem (you can now set the bitmap information with memset, eventhou I don't see any other use for memeset besides making the bmp all white or black).
There should be a function that allows you to set the DC bites to a bitmap thou, so you should be able to use the new bitmap as a parameter.

c++ read pixels with GetDIBits()

I'm trying to create a function which is equivalent to the windows API GetPixel() function, but I want to create a bitmap of my screen and then read that buffer.
This is what I've got (Mostly copy pasted from google searches), when I run it it only prints out 0's. I think I've got most of it right, and that my issue is that I don't know how to read the BYTE variable.
So my question is, what do I need to do in order to get it to print out some random colors (R,G or B) with my for loop?
#include <Windows.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
HDC hdc,hdcMem;
hdc = GetDC(NULL);
hdcMem = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, 1680, 1050);
BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdcMem, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error" << endl;
}
// create the bitmap buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
MyBMInfo.bmiHeader.biBitCount = 32;
MyBMInfo.bmiHeader.biCompression = BI_RGB;
MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);
// get the actual bitmap buffer
if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error2" << endl;
}
for(int i = 0; i < 100; i++) {
cout << (int)lpPixels[i] << endl;
}
return 0;
}
Windows 7
C::B 13.12 (Console Application)
Compiler: mingw32-gcc
Library gdi32 linked
As agreed, I'm adding a new answer with the working code snippet (I added the missing cleanup of lpPixels). See the discussions in my previous answer and the one made by #enhzflep.
#include <Windows.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
HBITMAP GetScreenBmp( HDC hdc) {
// Get screen dimensions
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
// Create compatible DC, create a compatible bitmap and copy the screen using BitBlt()
HDC hCaptureDC = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight);
HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
BOOL bOK = BitBlt(hCaptureDC,0,0,nScreenWidth, nScreenHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);
SelectObject(hCaptureDC, hOld); // always select the previously selected object once done
DeleteDC(hCaptureDC);
return hBitmap;
}
int main() {
HDC hdc = GetDC(0);
HBITMAP hBitmap = GetScreenBmp(hdc);
BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdc, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error" << endl;
}
// create the bitmap buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
// Better do this here - the original bitmap might have BI_BITFILEDS, which makes it
// necessary to read the color table - you might not want this.
MyBMInfo.bmiHeader.biCompression = BI_RGB;
// get the actual bitmap buffer
if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error2" << endl;
}
for(int i = 0; i < 100; i++) {
cout << (int)lpPixels[i];
}
DeleteObject(hBitmap);
ReleaseDC(NULL, hdc);
delete[] lpPixels;
return 0;
}
Basically, you need to have drawn some pixels in order to get back a result other than 0.
At present, the 4th line of code in your main creates an empty (blank, 0-initialized) image. You then get information about the size of this image with your first call to GetDIBits. You then get the actual (blank) pixels with your second call to GetDIBits.
To fix, just load a bitmap file from disk into your hBitmap and select this bitmap into your hdcMem.
I.e, change
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, 1680, 1050);
to something like this.
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "xpButton.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
HBITMAP old = (HBITMAP) SelectObject(hdcMem, hBitmap);
(make sure you use a valid bmp file name. Mine exists in the same folder as the .cpp file, since this is the 'current' directory when you run via the IDE. If you wish to run via explorer, place another copy of the bmp in the same folder as your exe)
Here's the bmp I've used (which has been converted to a png after upload to SO):
And here's the first 10 iterations through the loop.
255
5
253
0
255
5
253
0
255
5
Note that the pixel at 0,0 has the colour of: rgb(253,5,255) and it's an 8bit image, so there's no alpha channel, hence it has the value 0. The pixels are stored as [BGRA], [BGRA], [BGRA], etc, etc.
I'll leave it to you to fix the (non-existant) clean-up section of your program. Windows will de-allocate the memory you've used here, but you absolutely should not get into the habit of not freeing any memory you've allocated. :)
Your code seems a bit confused. Too many snippets I guess :).
Still, you're quite close:
The first GetDIBits() call is in order to get the properties of the bitmap filled in, as the comment in your code suggests.
You are using an unnecessary MemDC for this - which is probably from a snippet that wants to do a BitBlt with the screen.
You then can use the filled in structure to get the actual bitmap pixels with the second GetDIBits() call, but what you're doing is replacing the properties with hard coded values again, making the first GetDIBits() call useless.
So: Drop the MemDC - you don't need it - and replace hdcMem with hdc in the first call to GetDIBits(), then remove all the statements that overwrite bmiHeader members after the first GetDIBits call and you should get your pixels.
Oh, and of course don't forget to call ReleaseDC()/DeleteObject() on the dc and bitmap and delete[] the buffer :)

CreateDIBSection in MFC and rendering using picture control

I'm creating a DIB Section in MFC using the call CreateDIBSection. I get a HBITMAP from the call which I pass onto another dialog in my MFC Project. In the other dialog I'm using CStatic::SetBitmap(HBITMAP) call to render the bitmap. But for some reason I'm not able to see anything. This works perfectly fine if this is done in same dialog, but I want to create bitmap in one dialog and display in another.
The code for creating the DIBSection is
//-----------------BEGINNING OF FIRST DIALOG--------------------------------------------------
LPVOID pViewBitmapBits = NULL;
BITMAPINFOHEADER BMHeaderInfo;
memset(&BMHeaderInfo, 0, sizeof(BITMAPINFOHEADER));
BMHeaderInfo.biSize = sizeof(BITMAPINFOHEADER);
BMHeaderInfo.biWidth = 800;
BMHeaderInfo.biHeight = 400;
BMHeaderInfo.biPlanes = 1;
BMHeaderInfo.biBitCount = 8;
BMHeaderInfo.biCompression = BI_RGB;
BMHeaderInfo.biSizeImage = 0;
BMHeaderInfo.biClrUsed = 0;
BMHeaderInfo.biClrImportant= 0;
BITMAPINFO BMInfo;
memset(&BMInfo, 0, sizeof(BMInfo));
BMInfo.bmiHeader = BMHeaderInfo;
BMInfo.bmiColors[0].rgbBlue=255;
HBITMAP hGlobalBitMap = CreateDIBSection(m_pParentSheet->test.m_hDC, &BMInfo, DIB_RGB_COLORS, &pViewBitmapBits, NULL, NULL);
SelectObject(m_pParentSheet->test.m_hDC, hGlobalBitMap);
//--------------------------END OF FIRST DIALOG----------------------------------
//-----------------------------BEGINNING OF SECOND DIALOG----------------------------------------
void CreateViewDlg::OnBnClickedButton2()
{
m_pic.SetBitmap(hGlobalBitMap );
}
//------------------------------------END OF SECOND DIALOG---------------------------------
Please help me with this. Is there any limitation to the usage of HBITMAP Handles?
HBITMAP hGlobalBitMap = ...
Looks like local variable hiding the global variable.

Caching a bitmap

I'm writing a Win32 application using C++.
In this application I'm handling the WM_PAINT message:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GdiplusStartup(&gdiplusToken, &gdiPlusStartup, 0);
DrawM(ps.hdc, hWnd);
EndPaint(hWnd, &ps);
break;
And in the DrawM function I'm having something like this:
void DrawMap(HDC hdc, HWND hWnd)
{
if(!isDrawn)
{
// (some calculations)
Graphics g(hdc);
Bitmap img(max_x, max_y, &g);
int zoom_factor = 50;
for(int i = 0; i< segments.size(); i++)
{
// (some math)
for(int j = 0; j < segments.at(i).point_count; j++)
// (another dose of math)
g.DrawLines(&pen, segmentPoints, segments.at(i).point_count);
delete [] segmentPoints;
}
g.Save();
isDrawn = true;
}
else
{
// here is the problem
}
}
In the code above, what I wanted to do, is to render the image once, and later on when the window is resized, moved or anything happens to it that requires repainting will not render the Bitmap from scratch, instead it should use a cached one.
The problem is that Bitmap does not allow copying (the copy constructor denies it).
Another problem is that, when I'm trying to save the image to a file or a stream I receive an "Invalid parameter" error (i.e the return code is 2):
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
img.Save(_T("m.png"), &Gdiplus::ImageFormatPNG, NULL);
->clone() also seems that it is not working, because when I define a pointer to a Bitmap, clone the bitmap to it and in the "else" statement I use:
Graphics g(hdc);
g.DrawImage(bmpClone, 50, 50);
Nothing is rendered.
Any ideas on how to cache the Bitmap?
Clone() should work, but without seeing your code (which uses it) it's hard to know what's going on. As an alternative, another (more circuitous) approach would be to call GetHBITMAP() on the original Bitmap, store the GDI bitmap handle and then construct the new Bitmap with the Bitmap(HBITMAP, HPALETTE) constructor in future repaints.
Instead of declaring img as a local object, make it a static or a member of a class. Then it will be available at the next WM_PAINT without needing to be copied.