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 181 182 183 184 185 186 187 188 189 190 191
| unit Execute.GDIPBitmap;
{
GDI+ LoadFromStream helper for TBitmap (c)2017 Execute SARL
http://www.execute.fr
}
interface
uses
Windows,
ActiveX,
Classes,
Graphics;
type
{$if Compilerversion >= 20.0} // les class helper existent depuis D2009
TBitmapHelper = class helper for TBitmap
public
function GDIPLoadFromFile(const FileName: string): Boolean;
function GDIPLoadFromStream(Stream: TStream): Boolean; // Loads BMP, JPG, PNG and TIF
end;
{$else}
TBitmap = class(Graphics.TBitmap)
public
function GDIPLoadFromFile(const FileName: string): Boolean;
function GDIPLoadFromStream(Stream: TStream): Boolean; // Loads BMP, JPG, PNG and TIF
end;
{$ifend}
implementation
const
GDIP = 'gdiplus.dll';
type
TGdiplusStartupInput = packed record
GdiplusVersion : Cardinal; // Must be 1
DebugEventCallback : Pointer; // Ignored on free builds
SuppressBackgroundThread: BOOL; // FALSE unless you're prepared to call
// the hook/unhook functions properly
SuppressExternalCodecs : BOOL; // FALSE unless you want GDI+ only to use
end;
TStatus = (
Ok,
GenericError,
InvalidParameter,
OutOfMemory,
ObjectBusy,
InsufficientBuffer,
NotImplemented,
Win32Error,
WrongState,
Aborted,
FileNotFound,
ValueOverflow,
AccessDenied,
UnknownImageFormat,
FontFamilyNotFound,
FontStyleNotFound,
NotTrueTypeFont,
UnsupportedGdiPlusVersion,
GdiplusNotInitialized,
PropertyNotFound,
PropertyNotSupported,
ProfileNotFound
);
ARGB = Cardinal;
{$if Compilerversion >= 20.0}
TGpBitmap = record
private
Handle: Pointer;
public
function CreateFromStream(stream: IStream): TStatus; inline;
function GetHBitmap(var aHandle: HBitmap; Background: ARGB): TStatus; inline;
function Free: TStatus; inline;
end;
{$else}
TGpBitmap = class
private
Handle: Pointer;
public
destructor Destroy; override;
function CreateFromStream(stream: IStream): TStatus;
function GetHBitmap(var aHandle: HBitmap; Background: ARGB): TStatus;
end;
{$ifend}
function GdiplusStartup(out token: ULONG; const input: TGdiplusStartupInput; output: Pointer): TStatus; stdcall; external GDIP;
function GdipCreateBitmapFromStream(stream: IStream; out image: Pointer): TStatus; stdcall; external GDIP;
function GdipCreateHBITMAPFromBitmap(const Bitmap: Pointer; var Handle: HBitmap; Background: ARGB): TStatus; stdcall; external GDIP;
function GdipDisposeImage(const Image: Pointer): TStatus; stdcall; external GDIP;
var
GdiplusToken: ULONG = 0;
function InitGDIPlus: Boolean;
var
StartupInput: TGdiplusStartupInput;
begin
if GdiplusToken = 0 then
begin
FillChar(StartupInput, SizeOf(StartupInput), 0);
StartupInput.GdiplusVersion := 1;
Result := GdiplusStartup(GdiplusToken, StartupInput, nil) = TStatus(Ok);
end else begin
Result := True;
end;
end;
{ TGpBitmap }
function TGpBitmap.CreateFromStream(stream: IStream): TStatus;
begin
Result := GdipCreateBitmapFromStream(stream, Handle);
end;
function TGpBitmap.GetHBitmap(var aHandle: HBitmap; Background: ARGB): TStatus;
begin
Result := GdipCreateHBITMAPFromBitmap(Handle, aHandle, Background);
end;
{$if Compilerversion >= 20.0}
function TGpBitmap.Free: TStatus;
begin
Result := GdipDisposeImage(Handle);
end;
{$else}
destructor TGpBitmap.Destroy;
begin
GdipDisposeImage(Handle);
inherited;
end;
{$ifend}
{ TBitmapHelper }
{$if Compilerversion >= 20.0}
function TBitmapHelper.GDIPLoadFromFile(const FileName: string): Boolean;
{$else}
function TBitmap.GDIPLoadFromFile(const FileName: string): Boolean;
{$ifend}
const
fmOpenRead = 0;
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmOpenRead);
try
Result := GDIPLoadFromStream(FS);
finally
FS.Free;
end;
end;
{$if Compilerversion >= 20.0}
function TBitmapHelper.GDIPLoadFromStream(Stream: TStream): Boolean;
{$else}
function TBitmap.GDIPLoadFromStream(Stream: TStream): Boolean;
{$ifend}
var
Bitmap : TGpBitmap;
HBmp : HBitmap;
begin
Result := False;
if not InitGDIPlus then
Exit;
{$if Compilerversion < 20.0}
Bitmap := TGpBitmap.Create;
{$ifend}
if Bitmap.CreateFromStream(TStreamAdapter.Create(Stream)) = TStatus(Ok) then
begin
if Bitmap.GetHBitmap(HBmp, $FFFFFFFF) = TStatus(Ok) then
begin
Handle := HBmp;
Result := True;
end;
Bitmap.Free;
end;
end;
end. |
Partager