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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include <iostream>
#include <string>
#include <string.h>
#include <windows.h>
#include <math.h>
#include <vector>
#include <gdiplus.h>
using namespace Gdiplus;
using namespace std;
std::string getGdiplusStatusMessage(Status status)
{
std::string msg = "";
switch ( status )
{
case Ok:
msg = "Ok: Indicates that the method call was successful.";
break;
case GenericError:
msg = "GenericError: Indicates that there was an error on the method call, which is identified as something other than those defined by the other elements of this enumeration.";
break;
case InvalidParameter:
msg = "InvalidParameter: Indicates that one of the arguments passed to the method was not valid.";
break;
case OutOfMemory:
msg = "OutOfMemory: Indicates that the operating system is out of memory and could not allocate memory to process the method call.";
break;
case ObjectBusy:
msg = "ObjectBusy: Indicates that one of the arguments specified in the API call is already in use in another thread.";
break;
case InsufficientBuffer:
msg = "InsufficientBuffer: Indicates that a buffer specified as an argument in the API call is not large enough to hold the data to be received.";
break;
case NotImplemented:
msg = "NotImplemented: Indicates that the method is not implemented.";
break;
case Win32Error:
msg = "Win32Error: Indicates that the method generated a Win32 error.";
break;
case WrongState:
msg = "WrongState: Indicates that the object is in an invalid state to satisfy the API call. For example, calling Pen::GetColor from a pen that is not a single, solid color results in a WrongState status.";
break;
case Aborted:
msg = "Aborted: Indicates that the method was aborted.";
break;
case FileNotFound:
msg = "FileNotFound: Indicates that the specified image file or metafile cannot be found.";
break;
case ValueOverflow:
msg = "ValueOverflow: Indicates that the method performed an arithmetic operation that produced a numeric overflow.";
break;
case AccessDenied:
msg = "AccessDenied: Indicates that a write operation is not allowed on the specified file.";
break;
case UnknownImageFormat:
msg = "UnknownImageFormat: Indicates that the specified image file format is not known.";
break;
case FontFamilyNotFound:
msg = "FontFamilyNotFound: Indicates that the specified font family cannot be found. Either the font family name is incorrect or the font family is not installed.";
break;
case FontStyleNotFound:
msg = "FontStyleNotFound: Indicates that the specified style is not available for the specified font family.";
break;
case NotTrueTypeFont:
msg = "NotTrueTypeFont: Indicates that the font retrieved from an HDC or LOGFONT is not a TrueType font and cannot be used with GDI+.";
break;
case UnsupportedGdiplusVersion:
msg = "UnsupportedGdiplusVersion: Indicates that the version of GDI+ that is installed on the system is incompatible with the version with which the application was compiled.";
break;
case GdiplusNotInitialized:
msg = "GdiplusNotInitialized: Indicates that the GDI+ API is not in an initialized state. To function, all GDI+ objects require that GDI+ be in an initialized state. Initialize GDI+ by calling GdiplusStartup.";
break;
case PropertyNotFound:
msg = "PropertyNotFound: Indicates that the specified property does not exist in the image.";
break;
case PropertyNotSupported:
msg = "PropertyNotSupported: Indicates that the specified property is not supported by the format of the image and, therefore, cannot be set.";
break;
#if (GDIPVER >= 0x0110)
case ProfileNotFound:
msg = "ProfileNotFound: Indicates that the color profile required to save an image in CMYK format was not found.";
break;
#endif //(GDIPVER >= 0x0110)
default :
msg = "Invalid status: Indicates an unknown status was returned.";
break;
}
return msg;
}
std::wstring towstring(const std::string& v)
{
std::wstring out(v.size()+1,L'\0');
int size = MultiByteToWideChar(CP_UTF8, 0, v.c_str(), -1, &out[0], out.size());
out.resize(size-1);
return out;
}
class image
{
public:
BITMAP bitmap;
HBITMAP hBitmap=NULL;
HDC hdcimage=CreateCompatibleDC(NULL);
HBITMAP oldBitmap=NULL;
ULONG_PTR m_gdiplusToken=NULL;
Gdiplus::GdiplusStartupInput gdiplusStartupInput=nullptr;
Image *img=nullptr;
Gdiplus::Graphics *graphic=nullptr;
public:
operator HDC()
{
return hdcimage;
}
operator HBITMAP()
{
return hBitmap;
}
image()
{
if(hBitmap==NULL)
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
hBitmap=CreateCompatibleBitmap(hdcimage,1,1);
oldBitmap=(HBITMAP)SelectObject(hdcimage, hBitmap);
}
image(int Width, int Height)
{
if(hBitmap==NULL)
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
hBitmap=CreateCompatibleBitmap(hdcimage,Width,Height);
oldBitmap=(HBITMAP)SelectObject(hdcimage, hBitmap);
}
void Dispose()
{
//clear all objects for avoid memory leaks:
Gdiplus::GdiplusShutdown(m_gdiplusToken);
SelectObject(hdcimage, oldBitmap);
DeleteDC(hdcimage);
DeleteObject(hBitmap);
if(img!=nullptr) delete img;
if(graphic!=nullptr) delete graphic;
}
~image()
{
Dispose();
}
void FromFile(string strFile)
{
if(hBitmap==NULL)
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
img=new Image(towstring(strFile).c_str());
if(img->GetLastStatus()>0)
MessageBox( NULL,getGdiplusStatusMessage( img->GetLastStatus() ).c_str(),"error message",MB_OK);
hBitmap=CreateBitmap(img->GetWidth(),img->GetHeight(),1,32,NULL);
GetObject(hBitmap, sizeof(bitmap), &bitmap);
SelectObject(hdcimage, hBitmap);
graphic=new Graphics(hdcimage);
graphic->DrawImage(img, 0, 0, img->GetWidth(), img->GetHeight());
}
void DrawImage(HDC DestinationHDC)
{
//Gdiplus::Graphics graphics2(DestinationHDC);
//graphics2.DrawImage(img, 800, 0, img->GetWidth(), img->GetHeight());
BitBlt(DestinationHDC, 800,0,img->GetWidth(), img->GetHeight(),hdcimage,0,0,SRCCOPY);
}
};
|