here is my function im trying to create bitmap from hbitmap here. But im getting identifier not found error.
__declspec(dllexport) void __stdcall testFunction()
{
int W = 860, H = 720;
int iLeft = 0, iRight = 860;
int iTop = 0, iBottom = 720;
int iW = iRight - iLeft, iH = iBottom - iTop;
HWND bluestacks = FindWindow(L"BlueStacksApp", L"BlueStacks App Player");
HDC bHDC = GetWindowDC(bluestacks);
HDC cHDC = CreateCompatibleDC(bHDC);
HBITMAP bmp = CreateCompatibleBitmap( bHDC, W, H);
HGDIOBJ sO = SelectObject( cHDC, bmp);
BOOL pW =PrintWindow( bluestacks, cHDC, 0);
SelectObject(cHDC, bmp);
BitBlt(cHDC, 0, 0, iW, iH, bHDC, iLeft, iTop, 0x00CC0020);
Bitmap newBitmap = GdipCreateBitmapFromHBITMAP(bmp);
}
Error code:
identifier "GdipCreateBitmapFromHBITMAP" is undefined
Note loaded header files:
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <string.h>
#include <strsafe.h>
#include <gdiplus.h>
#include <Gdiplusflat.h> // Gdiplusflat.h
#include <ostream>
The function is defined as follows:
GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm,
HPALETTE hpal,
GpBitmap** bitmap);
if you want to get a Bitmap object from a HBITMAP, maybe you shoud use the constructor
Bitmap(HBITMAP,HPALETTE);
for your code it should be:
Bitmap newBitmap(bmp,NULL);
BTW:before use this, you should initialize GDIPLUS.
works for me, after adding additional option to linker -lgdiplus -mwindows
g++ test.c -lgdiplus -mwindows
Related
I wrote a program to save a BMP of a Greek word.
Here it is:
#include <Windows.h>
#include <gdiplus.h>
#include <iostream>
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;
using namespace std;
int main()
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
// Create a bitmap.
Bitmap bmp(500, 500, PixelFormat24bppRGB);
Graphics graphics(&bmp);
// Set the font.
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 36, FontStyleRegular, UnitPixel);
graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
graphics.SetSmoothingMode(SmoothingModeHighQuality);
// Draw the text.
SolidBrush brush(Color(255, 0, 0, 0));
WCHAR text[] = L"ΔΙΔΑΣΚΑΛΙΑ";
PointF origin(100.0f, 100.0f);
graphics.DrawString(text, -1, &font, origin, &brush);
// Save the bitmap.
Status test = bmp.Save(L"greek_word.bmp", &ImageFormatBMP);
printf("%d", test);
}
// Clean up GDI+.
GdiplusShutdown(gdiplusToken);
return 0;
}
For some reason, it creates the BMP, but it has a file size of 0.
And, for some reason, bmp.Save returns a FileNotFound error.
Anyone know why I am getting this strange behavior?
Thanks.
I am making a clipping software which clips the last 30 seconds of your screen. I am trying to write the results to an AVI file, but I am running into issues. Here is my code:
#include <Windows.h>
#include <Vfw.h>
#include <iostream>
#include <vector>
const int BUFFER_SIZE = 30 * 60; // 30 seconds at 60 fps
int main() {
HMONITOR hMonitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(hMonitor, &info);
int width = info.rcMonitor.right - info.rcMonitor.left;
int height = info.rcMonitor.bottom - info.rcMonitor.top;
HDC hDC = GetDC(NULL);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, width, height);
// Create a device context for the bitmap
HDC hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, hBitmap);
std::vector<HBITMAP> buffer(BUFFER_SIZE);
int index = 0;
while (true) {
BitBlt(hMemDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY);
buffer[index] = hBitmap;
index = (index + 1) % BUFFER_SIZE;
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
break;
}
}
// PROBLEM HERE:
PAVISTREAM pStream;
AVIFileInit();
AVIFileOpenW(&pStream, L"screen_recording.avi", OF_WRITE | OF_CREATE, NULL); // takes PAVIFILE as first parameter
AVIStreamSetFormat(pStream, 0, &hBitmap, sizeof(BITMAPINFOHEADER)); // takes PAVISTREAM as first parameter
// Write the stored frames to the AVISTREAM object
for (int i = 0; i < BUFFER_SIZE; i++) {
AVIStreamWrite(pStream, i, 1, &buffer[i], sizeof(BITMAPINFOHEADER), AVIIF_KEYFRAME, NULL, NULL);
}
AVIStreamClose(pStream);
AVIFileExit();
ReleaseDC(NULL, hDC);
DeleteDC(hMemDC);
DeleteObject(hBitmap);
return 0;
}
Am I doing this right? I am new to C++, so I am not sure if this is what I should be doing.
You can use the function AVIStreamOpenFromFile to open the avi file as stream, that will give the pointer to PAVISTREAM. So, not necessary to call the AVIFileOpen function.
I am unable to manage memory for Bitmap and CLSID objects I have created in a screenshot object class. Both of these are from the GDI+ library. The header lists the following private variables in Screenshot.h
#include <gdiplus.h>
#include <iostream>
#include <fstream>
#include <string>
#include "windows.h"
#pragma once
#pragma comment(lib, "gdiplus.lib")
using namespace std;
using namespace Gdiplus;
class Screenshot
{
private:
HDC dc, memdc, fontdc;
HBITMAP membit;
Bitmap* bmpPtr;
CLSID clsid;
ULONG_PTR gdiplusToken;
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
public:
Screenshot();
~Screenshot();
void TakeScreenshot(string userAction, string winName, long xMousePos, long yMousePos, long long tStamp);
void SaveScreenshot(string filename);
void memoryManagement();
};
Then when my main program takes a screenshot, the values are filled in with TakeScreenshot(), but not yet saved to disk
void Screenshot::TakeScreenshot(//redacted for readibility) {
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
HWND hwnd = GetDesktopWindow();
dc = ::GetDC(0);
int scaleHeight, scaleWidth = 0;
int Height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int Width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
scaleHeight = Height + (0.1 * Height);
memdc = CreateCompatibleDC(dc);
membit = CreateCompatibleBitmap(dc, Width, scaleHeight);
HBITMAP bmpContainer = (HBITMAP)SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, dc, 0, 0, SRCCOPY);
//Other code that adds fonts, etc. Does not invoke bmpPtr
bmpPtr = new Bitmap(membit, NULL);
GetEncoderClsid(L"image/jpeg", &clsid);
If the screenshot is saved, another function SaveScreenshot() uses bmpPtr->Save() and Gdiplus shutdown is called inside of it. However, some of the screenshots get popped off from a queue (STL queue) and out of memory instead of saved, as follows:
void ManageQueue(Screenshot& ssObj)
{
//If queue contains 30 screenshots, pop off first element and push new object
//Else just push new object
if (screenshotQueue.size() == MAX_SCREENSHOTS)
{
screenshotQueue.front().memoryManagement();
screenshotQueue.pop();
screenshotQueue.push(ssObj);
}
else
{
screenshotQueue.push(ssObj);
}
}
I wrote a MemoryManagement() function to perform the necessary releases and deletes before the Screenshot is popped off. This function is not called if the screenshot has been saved:
void Screenshot::memoryManagement()
{
delete bmpPtr;
delete &clsid;
ReleaseDC(NULL, memdc);
DeleteObject(fontdc);
DeleteObject(memdc);
DeleteObject(membit);
}
When either the delete on bmpPtr or clsid is called, whether it is from this function or in the deconstructor, the program is crashing. I am experiencing significant memory leaks with the program now and without running a windows equivalent of Valgrind I'm assuming it's coming from here. How can I successfully delete these objects? I will credit any answer in my source code as a contributing programmer. Please leave any suggestions for improving my question if needed.
scaleHeight = Height + (0.1 * Height);
This seems to be an attempt to fix the problem with DPI scaling. It will work if DPI settings is at 10%, but that's usually not the case. You have to make your program DPI aware through the manifest file. Use SetProcessDPIAware for a quick fix.
Don't declare dc, memdc, etc. as class members. These are GDI handles (not GDI+) which you can hold for a short time, usually during the duration of the function. You have to release them as soon as possible.
Also other variables like clsid don't need to be declared as class members. You can declare them as class member if you wish, but there is nothing to gain.
If you have a multi-monitor setup you also need SM_XVIRTUALSCREEN/Y to get the top-left corner of the monitor setup.
//call this once on start up
SetProcessDPIAware();
HDC dc = ::GetDC(0);
int x = GetSystemMetrics(SM_XVIRTUALSCREEN);
int y = GetSystemMetrics(SM_YVIRTUALSCREEN);
int Height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int Width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
HDC memdc = CreateCompatibleDC(dc);
HBITMAP membit = CreateCompatibleBitmap(dc, Width, Height);
HBITMAP bmpContainer = (HBITMAP)SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, dc, x, y, SRCCOPY);
Bitmap* bmpPtr = new Bitmap(membit, NULL);
// or just Bitmap bmp(membit, NULL);
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
bmpPtr->Save(L"output.jpg", &clsid);
//cleanup:
delete bmpPtr;
SelectObject(memdc, bmpContainer);
DeleteObject(membit);
DeleteDC(memdc);
ReleaseDC(0, dc);
The solution for this problem was to use the namespace delete instead of regular delete. Switching to this prevented the break point trigger during debug and has sealed the memory leak.
void Screenshot::memoryManagement()
{
::delete bmpPtr;
ReleaseDC(NULL, memdc);
DeleteObject(fontdc);
DeleteObject(memdc);
DeleteObject(membit);
GdiplusShutdown(gdiplusToken);
}
I am attempting to capture images from the screen. I currently have code which takes a screenshot and saves it to disk but I would prefer to not save it each time. After several hours of reading over other examples online I still feel I do not understand how this process works.
The two goals are to create the screen in memory to be passed to another function and to be able to capture only selected parts of the screen given (x,y) coordinates.
I am relatively new to coding so if this is a trivial thing it would not surprised but would still greatly appreciate any explanations.
Here is the sample code I found online and have been working with.
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string>
using namespace std;
void ScreenShot()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC, hCaptureBitmap);
BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight,
hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);
//SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code here to save the captured image to disk
ReleaseDC(hDesktopWnd, hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}
I have an dev-c++ Application which makes an Screenshot and writes it into an File. Now I want to write the image into an Variable/Stream. Originally I used three Writefile Functions, which write the header, the info and the hbitmap into an file. Now I want to save the data not to the file, but to an Stream, so I can use it for further processing. The Code I use is this:
/* <Include> */
#include <windows.h>
#include <iostream>
#include <sstream>
/* </Include> */
/* <Const> */
const char *AppName="Yeah";
using namespace std;
/* </Const> */
/* <Function> */
void SaveScreen(HWND pScreen, stringstream Path)
{
int Width = GetSystemMetrics(SM_CXSCREEN);//1280;
int Height = GetSystemMetrics(SM_CYSCREEN);//1024;
HDC hdcScreen;
HBITMAP hbmScreen;
//---------------Bitmap Informationen
BITMAPINFO infobmp;
infobmp.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
infobmp.bmiHeader.biWidth = Width;
infobmp.bmiHeader.biHeight = Height;
infobmp.bmiHeader.biPlanes = 1;
infobmp.bmiHeader.biBitCount = 24;
infobmp.bmiHeader.biCompression = 0;
infobmp.bmiHeader.biSizeImage = 0;
infobmp.bmiHeader.biXPelsPerMeter = 0;
infobmp.bmiHeader.biYPelsPerMeter = 0;
infobmp.bmiHeader.biClrUsed = 0;
infobmp.bmiHeader.biClrImportant = 0;
int* bitmap = new int[Width*Height*3];
BITMAPFILEHEADER bfheader;
bfheader.bfType = 19778;
bfheader.bfSize = sizeof(BITMAPFILEHEADER) + Width*Height*3 + sizeof(BITMAPINFOHEADER);
bfheader.bfReserved1 = 0;
bfheader.bfReserved2 = 0;
bfheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//Bitmap ----------------------- Informationen
hdcScreen = GetWindowDC(pScreen);
hbmScreen = CreateCompatibleBitmap(hdcScreen, Width, Height);
// tempor?rer DC
HDC hdcTemp = CreateCompatibleDC(hdcScreen);
// Bitmap reinselektieren
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcTemp, hbmScreen);
// Inhalt von Desktop ?bertragen
BitBlt(hdcTemp, 0, 0, Width, Height, hdcScreen, 0, 0, SRCCOPY);
int iResult = GetDIBits(hdcTemp, hbmScreen, 0, Height, bitmap, &infobmp, DIB_RGB_COLORS);
// aufr?umen
SelectObject(hdcTemp, hbmOld);
DeleteObject(hbmScreen);
DeleteDC(hdcTemp);
// HANDLE hfile = CreateFile(Path, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
//Datei Schreiben
DWORD word;
WriteFile(Path, &bfheader, 14, &word, NULL);
WriteFile(Path, &infobmp, 40,& word, NULL);
WriteFile(Path, bitmap, Width*Height*3, &word,NULL);
// Path = &bfheader & &infobmp & bitmap;
ReleaseDC(pScreen, hdcScreen);
// CloseHandle(hfile);
delete[] bitmap;
}
/* </Function> */
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd = FindWindow(NULL, AppName);
stringstream ms;
SaveScreen(hWnd, ms);
return 0;
}
Can someone please show me what I am doing wrong?
You can create a structure like this:
struct ScreenShotBuffer
{
BITMAPFILEHEADER bfheader;
BITMAPINFO infobmp;
int* bitmap;
};
Then create a global variable using that structure.
struct ScreenShotBuffer myScreenShot;
Anyway you are allocating the "memory" to hold the data using malloc.
So you can just use these three lines instead of WriteBuffer() :
myScreenShot.bfheader=bfheader;
myScreenShot.infobmp=infobmp;
myScreenShot.bitmap=bitmap;