Blob field (images) and ZLib
Posted: 25.09.2005, 23:42
Hello,
I use the following codes to read and write blob field with ZeOS, but are not working (PostreSQL 8.0.3 with libpq74.dll, FireBird 1.5.x) with the latest CVS version. It was working in 6.1.5 and 6.5.1-alpha (2004-11-14).
I tested it with ADO connection(MS SQL szerver 7), it was working with all version of ZeOS!!! :-(
The problem is: if the image is big (= not a small logo), the content of the image is wrong (mixed pixels), or if I use Zlib compression an error raise when I read it back!
[syntax="delphi"]
// (De-)Compress functions
procedure CompressStream(aSource,aTarget:TStream);
var
compStream:TCompressionStream;
begin
compStream:=TCompressionStream.Create(clFastest,aTarget);
try
aSource.Position:=0;
compStream.CopyFrom(aSource,aSource.Size);
compStream.CompressionRate;
finally
compStream.Free;
end;
end;
procedure DecompressStream(aSorce,aTarget:TStream);
var
decompStream:TDecompressionStream;
nRead:integer;
Buffer:array[0..1023] of Char;
Count:integer;
begin
decompStream:=TDecompressionStream.Create(aSorce);
try
if aSorce.Size > 0 then begin
decompStream.Position:=0;
Count:=1024;
repeat
try
nRead:=decompStream.Read(Buffer,Count);
except
on E:Exception do begin
raise Exception.Create('Decompression error when write it to stream!');
end;
end;
try
aTarget.WriteBuffer(Buffer,nRead);
except
on E:Exception do begin
raise Exception.Create('Decompression error when write it to stream!');
end;
end;
until nRead=0;
end;
finally
decompStream.Free;
end;
end;
(****************************************************)
procedure ReadBlobField(ImageField:TBlobField;Image:TImage;DeCompress:boolean);
var
memoStream:TMemoryStream;
compStream:TMemoryStream;
begin
if ImageField.IsNull then begin
memoStream:=TMemoryStream.Create;
Image.AutoSize:=true;
Image.Picture.Bitmap.LoadFromStream(memoStream);
Image.AutoSize:=false;
exit;
end;
memoStream:=TMemoryStream.Create;
if DeCompress then compStream:=TMemoryStream.Create else compStream:=nil;
try
// blob reading
try
if DeCompress then begin
ImageField.SaveToStream(compStream);
compStream.Position:=0;
DecompressStream(compStream,memoStream);
end else begin
ImageField.SaveToStream(memoStream);
end;
except
on E:Exception do begin
ShowPSError('Blob field reading error: '+#13+E.Message);
memoStream.Size:=0;
if DeCompress then compStream.Size:=0;
end;
end;
try
Image.AutoSize:=true;
memoStream.Position:=0;
Image.Picture.Bitmap.FreeImage;
Image.Picture.Bitmap.Dormant;
if memoStream.Size>0 then begin
memoStream.Position:=0;
Image.Picture.Bitmap.LoadFromStream(memoStream);
end;
Image.AutoSize:=false;
except
on E:Exception do ShowPSError('Image creating error: '+#13+E.Message);
end;
finally
memoStream.Free;
if DeCompress then compStream.Free;
end;
end;
procedure WriteBlobParam(ImageParam:TParam;Image:TImage;Compress:boolean);
var
memoStream:TMemoryStream;
compStream:TMemoryStream;
begin
if not Image.Visible then exit;
memoStream:=TMemoryStream.Create;
if Compress then compStream:=TMemoryStream.Create else compStream:=nil;
try
Image.Picture.Bitmap.SaveToStream(memoStream);
if memoStream.Size=0 then begin
ImageParam.Value:=null;
ImageParam.LoadFromStream(memoStream,ImageParam.DataType);
end else begin
// memoStream.Size;
memoStream.Position:=0;
if Compress then begin
CompressStream(memoStream,compStream);
compStream.Position:=0;
ImageParam.LoadFromStream(compStream,ImageParam.DataType);
end else begin
memoStream.Position:=0;
ImageParam.LoadFromStream(memoStream,ImageParam.DataType);
end;
memoStream.Position:=0;
end;
finally
memoStream.Free;
if Compress then compStream.Free
end;
end;
[/syntax]
Can somebody help? At the moment I am planing to upgarde from D7+ZeOS 6.5.1-alpha-2004-11-14 to D2005-ZeOS-CVS-2005-09-23.
THANX
Attila
I use the following codes to read and write blob field with ZeOS, but are not working (PostreSQL 8.0.3 with libpq74.dll, FireBird 1.5.x) with the latest CVS version. It was working in 6.1.5 and 6.5.1-alpha (2004-11-14).
I tested it with ADO connection(MS SQL szerver 7), it was working with all version of ZeOS!!! :-(
The problem is: if the image is big (= not a small logo), the content of the image is wrong (mixed pixels), or if I use Zlib compression an error raise when I read it back!
[syntax="delphi"]
// (De-)Compress functions
procedure CompressStream(aSource,aTarget:TStream);
var
compStream:TCompressionStream;
begin
compStream:=TCompressionStream.Create(clFastest,aTarget);
try
aSource.Position:=0;
compStream.CopyFrom(aSource,aSource.Size);
compStream.CompressionRate;
finally
compStream.Free;
end;
end;
procedure DecompressStream(aSorce,aTarget:TStream);
var
decompStream:TDecompressionStream;
nRead:integer;
Buffer:array[0..1023] of Char;
Count:integer;
begin
decompStream:=TDecompressionStream.Create(aSorce);
try
if aSorce.Size > 0 then begin
decompStream.Position:=0;
Count:=1024;
repeat
try
nRead:=decompStream.Read(Buffer,Count);
except
on E:Exception do begin
raise Exception.Create('Decompression error when write it to stream!');
end;
end;
try
aTarget.WriteBuffer(Buffer,nRead);
except
on E:Exception do begin
raise Exception.Create('Decompression error when write it to stream!');
end;
end;
until nRead=0;
end;
finally
decompStream.Free;
end;
end;
(****************************************************)
procedure ReadBlobField(ImageField:TBlobField;Image:TImage;DeCompress:boolean);
var
memoStream:TMemoryStream;
compStream:TMemoryStream;
begin
if ImageField.IsNull then begin
memoStream:=TMemoryStream.Create;
Image.AutoSize:=true;
Image.Picture.Bitmap.LoadFromStream(memoStream);
Image.AutoSize:=false;
exit;
end;
memoStream:=TMemoryStream.Create;
if DeCompress then compStream:=TMemoryStream.Create else compStream:=nil;
try
// blob reading
try
if DeCompress then begin
ImageField.SaveToStream(compStream);
compStream.Position:=0;
DecompressStream(compStream,memoStream);
end else begin
ImageField.SaveToStream(memoStream);
end;
except
on E:Exception do begin
ShowPSError('Blob field reading error: '+#13+E.Message);
memoStream.Size:=0;
if DeCompress then compStream.Size:=0;
end;
end;
try
Image.AutoSize:=true;
memoStream.Position:=0;
Image.Picture.Bitmap.FreeImage;
Image.Picture.Bitmap.Dormant;
if memoStream.Size>0 then begin
memoStream.Position:=0;
Image.Picture.Bitmap.LoadFromStream(memoStream);
end;
Image.AutoSize:=false;
except
on E:Exception do ShowPSError('Image creating error: '+#13+E.Message);
end;
finally
memoStream.Free;
if DeCompress then compStream.Free;
end;
end;
procedure WriteBlobParam(ImageParam:TParam;Image:TImage;Compress:boolean);
var
memoStream:TMemoryStream;
compStream:TMemoryStream;
begin
if not Image.Visible then exit;
memoStream:=TMemoryStream.Create;
if Compress then compStream:=TMemoryStream.Create else compStream:=nil;
try
Image.Picture.Bitmap.SaveToStream(memoStream);
if memoStream.Size=0 then begin
ImageParam.Value:=null;
ImageParam.LoadFromStream(memoStream,ImageParam.DataType);
end else begin
// memoStream.Size;
memoStream.Position:=0;
if Compress then begin
CompressStream(memoStream,compStream);
compStream.Position:=0;
ImageParam.LoadFromStream(compStream,ImageParam.DataType);
end else begin
memoStream.Position:=0;
ImageParam.LoadFromStream(memoStream,ImageParam.DataType);
end;
memoStream.Position:=0;
end;
finally
memoStream.Free;
if Compress then compStream.Free
end;
end;
[/syntax]
Can somebody help? At the moment I am planing to upgarde from D7+ZeOS 6.5.1-alpha-2004-11-14 to D2005-ZeOS-CVS-2005-09-23.
THANX
Attila