png文件读取问题 ( 积分: 200 )

  • 主题发起人 主题发起人 dobug
  • 开始时间 开始时间
D

dobug

Unregistered / Unconfirmed
GUEST, unregistred user!
用Gdi+的兄弟帮调试下吧,我实在不知道怎么办了,从Stream中创建TGPBitmap对象,如果文件类型是jpg就一点问题没有,但是是png的话马上报错。

下面的代码是写的一个Demo,实际程序要复杂些,不要建议我用Graphicsex,我只要能从一个MemoryStream中创建一个TGpbitmap就可以了。

var
PicStream:TStream;
StreamAdapter:TStreamAdapter;
FBitmap:TGPBitmap;
g:TGPGraphics;
begin
PicStream:=TMemoryStream.Create;
TMemoryStream(PicStream).LoadFromFile('d:/1/1.png');
PicStream.Seek(0,sobeginning);
StreamAdapter:=TStreamAdapter.Create(PicStream,soReference);
FBitmap:=TGPBitmap.Create(StreamAdapter);
g:=TGPGraphics.Create(self.Canvas.Handle);
g.DrawImage(FBitmap,0,0);
g.Free;
PicStream.Free;
FBitmap.Free;
end;
 
问题解决了,来个人领分吧
 
我来领。不好意思。咋解决的,能贴出来吗?
 
是delphi里TStreamAdapter的bug,改写下类,用TMyStreamAdapter就行了

unit MyStreamAdapter;

interface
uses Classes,ActiveX,Windows,SysUtils;

type
TMyStreamAdapter = class(TStreamAdapter)
public
function Stat(out statstg: TStatStg;grfStatFlag: Longint): HResult; override; stdcall;
end;


function DateTimeToFileTime(DateTime: TDateTime): TFileTime;

implementation

function TMyStreamAdapter.Stat(out statstg: TStatStg;
grfStatFlag: Longint): HResult;
{************************************}
Begin
Result := S_OK;
try
if (@statstg <> nil) then
with statstg do
begin
FillChar(statstg, sizeof(statstg), 0);
dwType := STGTY_STREAM;
cbSize := Stream.size;
mTime := DateTimeToFileTime(now);
cTime := DateTimeToFileTime(now);
aTime := DateTimeToFileTime(now);
grfLocksSupported := LOCK_WRITE;
end;
except
Result := E_UNEXPECTED;
end;
end;


function DateTimeToFileTime(DateTime: TDateTime): TFileTime;
// copied from JclDateTime.pas
const
FileTimeBase = -109205.0;
FileTimeStep: Extended = 24.0 * 60.0 * 60.0 * 1000.0 * 1000.0 * 10.0; // 100 nSek per Day
var
E: Extended;
F64: Int64;
begin
E := (DateTime - FileTimeBase) * FileTimeStep;
F64 := Round(E);
Result := TFileTime(F64);
end;

end.
 
后退
顶部