What am I doing wrong? Program crashes. [GDI+, WINAPI, C++] - c++

I wrote a program in C++ which has to print an image on the screen and exit after 2 seconds. Everything was ok until the program has to exit. When the program is on return 0; instruction, it crashes. I think it's because of wrong dealocation, but code seems to be correct.
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
using namespace std;
using namespace Gdiplus;
HDC hdc = GetDC(NULL);
int main() {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Graphics graphics(hdc);
Rect dst(500, 500, 17, 17);
Image* image = Image::FromFile(L"image.png", false);
cout<<"Image size: "<<image->GetWidth()<<", "<<image->GetHeight()<<endl;
graphics.DrawImage(image, dst);
delete image;
GdiplusShutdown(gdiplusToken);
Sleep(2000);
return 0;
}
I've got the newest compilator and I compile by:
g++ -c main.cpp -std=g++11
g++ -o main main.o -lgdiplus

graphics destructor is executed after GdiplusShutdown. you need enclose block with
Graphics graphics(hoc);
in { }

Related

bmp.Save returns FileNotFound

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.

Using the Gdiplus outside of the main [duplicate]

I'm using the GDI+ API just to display an image (bmp) on the screen. Here is the code of the function doing the task:
void drawImg_ArmorInfo_PupupWnd(HDC hdc) {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Image* image = new Image(L"C:/Users/Darek/F2 stuff/art/intrface/armor_info_1.bmp");
int a1 = image->GetWidth();
int a2 = image->GetHeight();
Graphics graphics(hdc);
graphics.DrawImage(image, 0, 0);
delete image;
GdiplusShutdown(gdiplusToken);
}
The problem is that I'm getting an exception: Access violation writing location 0xXXXXXXXX after calling the GdiplusShutdown function. What's interesting when I comment out the
Graphics graphics(hdc);
graphics.DrawImage(image, 0, 0);
part the program runs without any problems but, of course, the image isn't drawn. When I comment out the call to GdiplusShutdown function - no problems again.
What's wrong with the code and what can be the reason of the problem here?
graphics falls out of the scope after the GdiplusShutdown so it cannot destruct correctly.
Try this:
Graphics * graphics = new Graphics(hdc);
graphics->DrawImage(image, 0, 0);
delete graphics;

GDI+ GdipCreateBitmapFromHBITMAP in C++

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

tcc: error: undefined symbol '_GetConsoleWindow#0'

I'm making a program in C/C++ which must run hidden using this code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
int main(){
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
. . .
}
I really want to use tinyc to compile it because it's much better than gcc (almost, the final executable is much tiny than gcc).
The point is that when I try to compile it using:
tcc PROGRAM.c -luser32
It makes an error which says:
tcc: error: undefined symbol '_GetConsoleWindow#0'
But when I use gcc it works! I think I have a missed library but I don't know which one.
Please, some help :)
According to MSDN, GetConsoleWindow is located in Kernel32.dll
Try:
tcc PROGRAM.c -luser32 -lkernel32
EDIT:
tcc's kernel32.def is missing the export for GetConsoleWindow.
Append the string GetConsoleWindow at the end of the def file located in the lib directory inside tcc's installation folder.
remove gdi32.def kernel32.def msvcrt.def user32.def files from C:\tcc\lib; remove all .def files from C:\tcc\lib
#include <windows.h>
#include <wincon.h>
#include <stdio.h>
#include <conio.h>
void main()
{
HWND hwnd;
hwnd = GetConsoleWindow();
HDC hdc;
hdc = GetWindowDC(hwnd);
printf("console hwnd: %p\n", hwnd);
printf("console hdc: %p\n", hdc);
HPEN hPenNull, hPenBlack, hPenRed, hPenGreen, hPenBlue;
hPenNull=GetStockObject(NULL_PEN);
hPenBlack=CreatePen(PS_SOLID, 2, RGB(0,0,0));
hPenRed=CreatePen(PS_SOLID, 2, RGB(255,0,0));
hPenGreen=CreatePen(PS_SOLID, 2, RGB(0,255,0));
hPenBlue=CreatePen(PS_SOLID, 2, RGB(0,0,255));
HBRUSH hBrushNull, hBrushBlack, hBrushRed, hBrushGreen, hBrushBlue, hBrushYellow;
hBrushNull=GetStockObject(NULL_BRUSH);
hBrushBlack=CreateSolidBrush(RGB(0,0,0));
hBrushRed=CreateSolidBrush(RGB(255,0,0));
hBrushYellow=CreateSolidBrush(RGB(255,255,0));
hBrushGreen=CreateSolidBrush(RGB(0,255,0));
hBrushBlue=CreateSolidBrush(RGB(0,0,255));
SelectObject(hdc, hPenRed);
SelectObject(hdc, hBrushYellow);
Ellipse(hdc, 200,50,260,150);
SelectObject(hdc, hPenNull);
SelectObject(hdc, hBrushRed);
Ellipse(hdc, 140, 80, 180, 120);
SelectObject(hdc, hPenBlue);
SelectObject(hdc, hBrushNull);
Ellipse(hdc, 280, 50, 340, 150);
getch();
}
-L"C:\tcc\lib" -lkernel32 -luser32 -lgdi32 -Wl,-subsystem=console

How to draw in C++ with Mouse Down Event

I wanna draw some pixels with mouse and i wanna do it in C++ just like in Paint but so far i couldnt get it working.
What i have done:
// NewPaint.cpp : main project file.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <dos.h>
#include <Winuser.h>
int main()
{
SetConsoleTitle(L"PaintER"); // Set text of the console so you can find the window
HWND hWnd = GetConsoleWindow(); // Get the HWND
HDC hdc = GetDC(hWnd); // Get the DC from that HWND
textbackgroundcolor(WHITE); // Background color
textcolor(BLACK); // Text color
POINT p; // Pointer
while(1) // Smt like mouse button 1
{
if (ScreenToClient(hWnd, &p)) // Mouse position
{
SetPixel(hdc, p.x, p.y, RGB(0,0,0)); // Draw black pixels
}
}
ReleaseDC(hWnd, hdc); // Release the DC
DeleteDC(hdc); // Delete the DC
system("pause");
return 0;
}
I am using Visual Studio 2010 for this and using Windows 7. I heard these could be inportant infos..
I dont have i only got GDI+ references so if you know a way to use it for this please let me know.
I need coding examples and explanations with it as i've given in my code.