如何把JPG图象缩小后存人SQL的IMAGE字段 ( 积分: 200 )

  • 主题发起人 主题发起人 fxb
  • 开始时间 开始时间
F

fxb

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把JPG图象缩小后存人SQL的IMAGE字段,最好能提供原码
 
implementation
uses JPEG;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
jpg: TJPEGImage;
BMP: TBitmap;
begin
bmp:= TBitmap.Create;
jpg:= TJPEGImage.Create;
try
bmp.LoadFromFile('c:/test.bmp');
jpg.Assign(bmp);
jpg.CompressionQuality:= 50; //1..100
jpg.Compress;
//jpg.SaveToFile('c:/out.jpg');
DBImage1.DataField:= 'IMage';
DBImage1.Picture.Bitmap.Assign(jpg); //may be raise error
Table1.Edit;
Table1.Post; //save to db
finally
bmp.Free;
jpg.Free;
end;
end;
 
能指定分辩率为400*300吗?
 
用流的形式存储吧!
 
procedure TForm5.Button1Click(Sender: TObject);
var
Jpg: TJPEGImage;
Bmp: TBitmap;
Stream: TMemoryStream;
begin
Bmp := TBitmap.Create;
Jpg := TJPEGImage.Create;
Stream := TMemoryStream.Create;
try
Bmp.LoadFromFile('c:/test.bmp');
Jpg.Width := 400;
Jpg.Height := 300;
Jpg.Assign(Bmp);
Jpg.CompressionQuality := 50;
Jpg.Compress;
Jpg.SaveToStream(Stream);
YourDataset.Edit;
TBlobField(YourDataset.FieldByName('Image')).LoadFromStream(Stream);
YourDataset.Post;
finally
if Bmp <> nil then FreeAndNil(Bmp);
if Jpg <> nil then FreeAndNil(Jpg);
if Stream <> nil then FreeAndNil(Stream);
end;
end;
 
后退
顶部