我已经解决问题,程序如下:
use jpeg
var
tp_stream: tmemorystream;
begin
tp_stream.create;
query1.close;
.........
image.picture.savetostream(tp_stream);
query1.parambyname(':变量名').loadstream:=tp_stream;
query1.sql.execsql;
query1.close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.Open;
end;
procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
var
Stream : TStream;
Size : integer;
Header : TGraphicHeader;
Signature : pointer;
i : integer;
Image : TGraphic;
Name : string;
begin
if ((Field <> nil) and (Field <> Table1Graphic)) or
(Table1Graphic.DataSet.State <> dsBrowse) then
exit;
Image := nil;
// Database cursor has moved - Update image
Stream := Table1Graphic.DataSet.CreateBlobStream(Table1Graphic, bmRead);
try
// Skip paradox image header
Size := Stream.Size;
if (Size >= SizeOf(TGraphicHeader)) then
begin
Stream.Read(Header, SizeOf(Header));
if (Header.Count <> 1) or (Header.HType <> $0100) or
(Header.Size <> Size - SizeOf(Header)) then
Stream.Seek(-SizeOf(Header), soFromCurrent);
end;
try
// Determine image type from file signature
for i := low(Signatures) to high(Signatures) do
begin
if (length(Signatures[i].Signature) > Size) then
continue;
// Read signature
GetMem(Signature, length(Signatures[i].Signature));
try
Stream.Read(Signature^, length(Signatures[i].Signature));
// Reposition stream cursor
Stream.Seek(-length(Signatures[i].Signature), soFromCurrent);
// Compare signatures
if (CompareMem(Signature, @Signatures[i].Signature[1], length(Signatures[i].Signature))) then
begin
Image := TGraphicHackClass(Signatures[i].ImageType).Create;
Name := Signatures[i].Name;
break;
end;
finally
Freemem(Signature);
end;
end;
// Force TImage to desired graphic type
ImageFish.Picture.Graphic := Image;
// Load image from stream
if (ImageFish.Picture.Graphic <> nil) then
begin
ImageFish.Picture.Graphic.LoadFromStream(Stream);
Caption := format('FISH FACTS - %s image, %d bytes', [Name, Size]);
end else
Caption := 'FISH FACTS';
// Write the specified image to the BLOB field
procedure TForm1.UpdateField(Graphic: TGraphic);
var
Stream : TStream;
Header : TGraphicHeader;
begin
Table1Graphic.DataSet.Edit;
Stream := Table1Graphic.DataSet.CreateBlobStream(Table1Graphic, bmWrite);
try
// TBitmaps are saved in a special format which is compatible with
// the TDBImage component...
if (Graphic is TBitmap) and
((Table1Graphic.DataType = ftGraphic) or (Table1Graphic.DataType = ftTypedBinary)) then
begin
Stream.Write(Header, SizeOf(Header));
Graphic.SaveToStream(Stream);
Header.Count := 1;
Header.HType := $0100;
Header.Size := Stream.Position - SizeOf(Header);
Stream.Position := 0;
Stream.Write(Header, SizeOf(Header));
end else
// ...all other image formats are just saved to the BLOB stream
Graphic.SaveToStream(Stream);
finally
Stream.Free;
end;
Table1Graphic.DataSet.Refresh;
end;
procedure TForm1.MenuImageConvertBitmapClick(Sender: TObject);
var
Bitmap : TBitmap;
begin
Bitmap := TBitmap.Create;
try
Bitmap.Assign(ImageFish.Picture.Graphic);
UpdateField(Bitmap);
finally
Bitmap.Free;
end;
end;
procedure TForm1.MenuImageConvertJPEGClick(Sender: TObject);
var
JPEG : TJPEGImage;
Bitmap : TBitmap;
begin
JPEG := TJPEGImage.Create;
try
// TJPEGImage can only assign from TBitmap and TJPEGImage, so we
// need to convert the TGIFImage to a TBitmap before it can be
// converted to a TJPEGImage
if (ImageFish.Picture.Graphic is TGIFImage) then
begin
Bitmap := TBitmap.Create;
try
Bitmap.Assign(ImageFish.Picture.Graphic);
JPEG.Assign(Bitmap);
finally
Bitmap.Free;
end;
end else
JPEG.Assign(ImageFish.Picture.Graphic);
JPEG.CompressionQuality := 80;
JPEG.Compress;
UpdateField(JPEG);
finally
JPEG.Free;
end;
end;
procedure TForm1.MenuImageLoadClick(Sender: TObject);
begin
if (OpenPictureDialog.Execute) then
begin
ImageFish.Picture.LoadFromFile(OpenPictureDialog.FileName);
UpdateField(ImageFish.Picture.Graphic);
end;
end;