1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
#include <cstdio>
#if defined(DEBUG) || defined(_DEBUG)
#define LOG(fmt, ...) fprintf(stderr,fmt, __VA_ARGS__);
#else
#define LOG(fmt, ...)
#endif
class GDI_Plus
{
public:
GDI_Plus()
{
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, nullptr);
}
~GDI_Plus()
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
}
private:
ULONG_PTR m_gdiplusToken = 0;
Gdiplus::GdiplusStartupInput gdiplusStartupInput = {0};
};
struct Pixel
{
BYTE red,
green,
blue;
};
int main()
{
GDI_Plus _ ;
HWND consoleWindow = GetConsoleWindow();
HDC consoleDC = GetDC(consoleWindow);
Gdiplus::Graphics graphics(consoleDC);
Gdiplus::Bitmap *bmp = Gdiplus::Bitmap::FromFile(L"D:\\Temp\\Demo.jpg");
LOG("Bitmap Width: %d\nBitmap Height: %d\n", bmp->GetWidth(), bmp->GetHeight());
graphics.DrawImage(bmp, 100, 200, bmp->GetWidth(), bmp->GetHeight());
Gdiplus::Rect rect(0,0, bmp->GetWidth(), bmp->GetHeight());
Gdiplus::BitmapData data;
auto status = bmp->LockBits(&rect, Gdiplus::ImageLockModeWrite, PixelFormat24bppRGB, &data); // 24 bit jpeg
Pixel *pixel = static_cast<Pixel*>(data.Scan0);
int numPixels = bmp->GetWidth() * bmp->GetHeight();
for (int i = 0; i < numPixels; ++i)
{
pixel->blue = pixel->red = pixel->green = 125;
++pixel;
}
bmp->UnlockBits(&data);
graphics.DrawImage(bmp, 400, 200, bmp->GetWidth(), bmp->GetHeight());
ReleaseDC(consoleWindow, consoleDC);
}
|