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 192 193 194
| unit GdipApi;
//simplification de unité GdipApi.pas par Zutatous
interface
uses
Windows, SysUtils, Graphics;
procedure LoadImgPlus(const Filename:Widestring;AOut:TBitmap; W,H : Cardinal;
AutoSize,Stretch:boolean);
procedure SaveImgPlus(const Filename:Widestring;AInp:TBitmap;AFormat:string='.bmp');
implementation
type
DebugEventLevel = (
DebugEventLevelFatal,
DebugEventLevelWarning
);
Unit_ = (
UnitWorld,
UnitDisplay,
UnitPixel,
UnitPoint,
UnitInch,
UnitDocument,
UnitMillimeter
);
TUnit = Unit_;
TDebugEventLevel = DebugEventLevel;
DebugEventProc = procedure(level: DebugEventLevel; message: PChar); stdcall;
GdiplusStartupInput = packed record
GdiplusVersion : Cardinal;
DebugEventCallback : DebugEventProc;
SuppressBackgroundThread: BOOL;
SuppressExternalCodecs : BOOL;
end;
TGdiplusStartupInput = GdiplusStartupInput;
PGdiplusStartupInput = ^TGdiplusStartupInput;
GPIMAGE =Pointer;
GPBITMAP =Pointer;
GPGRAPHICS =Pointer;
EncoderParameter = packed record
Guid : TGUID;
NumberOfValues : ULONG;
Type_ : ULONG;
Value : Pointer;
end;
TEncoderParameter = EncoderParameter;
PEncoderParameter = ^TEncoderParameter;
EncoderParameters = packed record
Count : UINT;
Parameter : array[0..0] of TEncoderParameter;
end;
TEncoderParameters = EncoderParameters;
PEncoderParameters = ^TEncoderParameters;
var
StartupInput: TGDIPlusStartupInput;
gdiplusToken: ULONG;
const BmpEncoder :TGuid = '{557CF400-1A04-11D3-9A73-0000F81EF32E}';
const JpegEncoder :TGuid = '{557CF401-1A04-11D3-9A73-0000F81EF32E}';
const GifEncoder :TGuid = '{557CF402-1A04-11D3-9A73-0000F81EF32E}';
const TifEncoder :TGuid = '{557CF405-1A04-11D3-9A73-0000F81EF32E}';
const PngEncoder :TGuid = '{557CF406-1A04-11D3-9A73-0000F81EF32E}';
const QualityParam :TGuid = '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}';
function GdipCreateBitmapFromFile(filename: PWCHAR;
out bitmap: Pointer): byte;stdcall;external 'Gdiplus.dll' ;
function GdipGetImageWidth(image: GPIMAGE;
var width: UINT): byte; stdcall;external 'Gdiplus.dll' ;
function GdipGetImageHeight(image: GPIMAGE;
var height: UINT): byte; stdcall;external 'Gdiplus.dll' ;
function GdipCreateFromHDC(hdc: HDC;
out graphics: GPGRAPHICS): byte; stdcall;external 'Gdiplus.dll' ;
function GdipDrawImage(graphics: GPGRAPHICS; image: GPIMAGE; x: Single;
y: Single): byte; stdcall;external 'Gdiplus.dll' ;
function GdipDeleteGraphics(graphics: GPGRAPHICS): byte; stdcall;
external 'Gdiplus.dll' ;
function GdipDisposeImage(image: GPIMAGE): byte; stdcall;
external 'Gdiplus.dll' ;
function GdipCreateBitmapFromHBITMAP(hbm: HBITMAP; hpal: HPALETTE;
out bitmap: GPBITMAP): byte; stdcall;external 'Gdiplus.dll' ;
function GdipSaveImageToFile(image: GPIMAGE;
filename: PWCHAR;
clsidEncoder: PGUID;
encoderParams: PEncoderParameters): byte; stdcall; external 'Gdiplus.dll' ;
function GdiplusStartup(out token: ULONG; input: PGdiplusStartupInput;
output: Pointer): Byte; stdcall;external 'Gdiplus.dll' ;
procedure GdiplusShutdown(token: ULONG); stdcall;external 'Gdiplus.dll' ;
function GdipDrawImageRectRect(graphics: GPGRAPHICS; image: GPIMAGE;
dstx: Single; dsty: Single; dstwidth: Single; dstheight: Single;
srcx: Single; srcy: Single; srcwidth: Single; srcheight: Single;
srcUnit: TUnit; imageAttributes: pointer;
callback: pointer; callbackData: Pointer): byte; stdcall;external 'Gdiplus.dll' ;
procedure LoadImgPlus(const Filename:Widestring;AOut:TBitmap; W,H : Cardinal;
AutoSize,Stretch:boolean);
var
xH,xW:cardinal;
Pimg,dc:Pointer;
begin
if not Assigned(AOut) then Exit;
if GdipCreateBitmapFromFile(PWidechar(Filename), Pimg)= 0 then
begin
GdipGetImageHeight(Pimg,xH);
GdipGetImageWidth(Pimg,xW);
if AutoSize then
begin
W := xW;
H := xH;
end else if not Stretch then begin
xW :=W;
xH :=H;
end;
AOut.Width :=W;
AOut.Height :=H;
GdipCreateFromHDC(AOut.Canvas.Handle,dc);
GdipDrawImageRectRect(dc,Pimg,0,0,W,H,
0,0,xW,xH,UnitPixel,nil,nil,nil);
GdipDeleteGraphics(dc);
GdipDisposeImage(Pimg);
end;
end;
procedure SaveImgPlus(const Filename:Widestring;AInp:TBitmap;AFormat:string);
var
Pimg : Pointer;
SaveFormat : TGuid;
S : Widestring;
W : EncoderParameters;
CompressRate:integer;
begin
if not Assigned(AInp) then Exit;
AFormat :=LowerCase(AFormat);
if AFormat = '.bmp' then
SaveFormat := BmpEncoder
else if AFormat = '.jpg' then
SaveFormat := JpegEncoder
else if AFormat = '.png' then
SaveFormat := PngEncoder
else if AFormat = '.tif' then
SaveFormat := TifEncoder
else if AFormat = '.gif' then
SaveFormat := GifEncoder
else
raise Exception.Create('Format non valide '+AFormat);
S:= ChangeFileExt(Filename,AFormat);
if GdipCreateBitmapFromHBITMAP(AInp.Handle,AInp.Palette,Pimg )= 0 then
begin
if AFormat = '.jpg' then
with W.Parameter[0] do
begin
W.Count :=1;
CompressRate := 80;
Guid := QualityParam;
NumberOfValues := 1;
Type_ :=4;
Value :=@CompressRate;
GdipSaveImageToFile(Pimg,PWideChar(S),@SaveFormat,@W);
end
else GdipSaveImageToFile(Pimg,PWideChar(S),@SaveFormat,nil);
GdipDisposeImage(Pimg);
end;
end;
initialization
StartupInput.DebugEventCallback := nil;
StartupInput.SuppressBackgroundThread := False;
StartupInput.SuppressExternalCodecs := False;
StartupInput.GdiplusVersion := 1;
GdiplusStartup(gdiplusToken, @StartupInput, nil);
finalization
GdiplusShutdown(gdiplusToken);
end. |
Partager