uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormPaint(Sender: TObject);
const
ImgPath = 'c:/temp/test.bmp';
var
img: TGPImage;
bit: TBitmap;
g: TGPGraphics;
begin
bit := TBitmap.Create;
bit.LoadFromFile(ImgPath); {因为 TBitmap 操作流特别方便, 这里就没有演示}
img := TGPBitmap.Create(bit.Handle, bit.Palette);
g := TGPGraphics.Create(Canvas.Handle);
g.DrawImage(img, 0, 0);
g.Free;
bit.Free;
img.Free;
end;
方法二:
TGPImage 需要的是 IStream 而非 TStream, 直接给它一个 IStream 参数不就完了?
那怎么实现 IStream 接口呢? Classes 单元下有一个 TStreamAdapter 类就是专门实现 IStream 接口的.
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormPaint(Sender: TObject);
const
ImgPath = 'c:/temp/test.bmp';
var
img: TGPImage;
MemStream: TMemoryStream;
MyIStream: TStreamAdapter;
g: TGPGraphics;
begin
MemStream := TMemoryStream.Create;
MemStream.LoadFromFile(ImgPath);
MyIStream := TStreamAdapter.Create(MemStream);
img := TGPImage.Create(MyIStream);
g := TGPGraphics.Create(Canvas.Handle);
g.DrawImage(img, 0, 0);
g.Free;
img.Free;
MemStream.Free;
MyIStream := nil; {注意这里}
end;