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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
class image
{
public:
ULONG_PTR m_gdiplusToken=NULL;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Graphics *graphic=nullptr;
Gdiplus::Bitmap *img;
int SizeWidth=0;
int SizeHeight=0;
HDC GetHDC()
{
//i'm not getting the right HDC value :(
return graphic->GetHDC();
}
void ReleaseHDC(HDC DestinationHDC)
{
graphic->ReleaseHDC(DestinationHDC);
}
COLORREF GetPixel(int PosX, int PosY)
{
Color clr;
img->GetPixel(PosX, PosY, &clr);
return clr.ToCOLORREF();
}
void SetPixel(int PosX, int PosY, COLORREF color)
{
Color *clr;
clr->SetFromCOLORREF(color);
img->SetPixel(PosX, PosY,clr);
}
image()
{
//nothing:
}
image(int Width, int Height)
{
Dispose();
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
img = new Bitmap( Width, Height);
graphic=new Graphics(img);
}
image(string strFile)
{
Dispose();
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
img=new Bitmap(towstring(strFile).c_str());
graphic=new Graphics(img);
}
void Clear(Color clr=Color::Black)
{
SolidBrush *Fill=new SolidBrush(Color::White);
graphic->FillRectangle(Fill,0,0,SizeWidth,SizeHeight);
}
void NewImage(int Width, int Height)
{
Dispose();
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
img=new Gdiplus::Bitmap(Width, Height);
graphic=new Gdiplus::Graphics(img);
SolidBrush *Fill=new SolidBrush(Color::White);
graphic->FillRectangle(Fill,0,0,Width,Height);
delete Fill;
}
void Dispose()
{
//clear all objects for avoid memory leaks:
if(m_gdiplusToken)
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
}
//by some reason, i can't delete the 'new' values: 'img' and 'graphic'
//the program terminates with an exit error
}
~image()
{
Dispose();
}
void FromFile(string strFile)
{
Dispose();
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
img=new Bitmap(towstring(strFile).c_str());
}
//these 2 functions works and tested:
void DrawImage(HDC DestinationHDC, float PosX, float PosY)
{
if(!img || !m_gdiplusToken) return;
Gdiplus::Graphics graphics2(DestinationHDC);
graphics2.DrawImage(img, PosX, PosY, img->GetWidth(), img->GetHeight());
}
void DrawImage(Graphics *Destination, float PosX, float PosY)
{
//if we don't have an image or the GDIPLUS isn't activated, we exit
if(!img || !m_gdiplusToken) return;
Destination->DrawImage(img, PosX, PosY, img->GetWidth(), img->GetHeight());
}
};
|