将TIMAGE控件中的图片 保存为SQL2K的IMAGE字段,保存很大,怎么能缩小?(100分)

  • 主题发起人 主题发起人 asaili
  • 开始时间 开始时间
A

asaili

Unregistered / Unconfirmed
GUEST, unregistred user!
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into lcdcpk (Chartx) values(:Pic)');
ADOQuery1.Parameters.ParamByName('Pic').Assign(Image1.Picture.Bitmap);
ADOQuery1.ExecSQL;

这样保存后有500多K,我想只要保存为JPG图片大小就可以了,怎么转换一下?
 
这样只能保存bmp文件啊。
要保存为jpg图片,可使用以下代码:(要注意必须加上DB单元,否则会提示找不到TBlobField类型
var
tupian:TMemoryStream;
************************************
if image1.Picture.Graphic<>nil then
begin
tupian:=Tmemorystream.Create;
try
image1.Picture.Graphic.SaveToStream(tupian);
(databox.adoquery1.fieldbyname('samplepicture') as TBlobField).loadfromstream(tupian);
finally
tupian.Free;
end;
end;
 
var tempStream:TMemoryStream;
JpgPic:TJpegImage;
begin
try
JpgPic:=TJpegImage.Create;
tempStream:=TMemoryStream.Create;
tempStream.clear;
adoquery1.Append;
JpgPic.LoadFromFile(openpicturedialog1.filename);
DBImage.Picture.bitmap.assign(JpgPic);//若你想DBIMAGE显示JPG图像就可这样.
JpgPic.SaveToStream(tempStream);
tblobfield(adoquery1.FieldByName('image')).LoadFromStream(tempStream) ;
adoquery1.Post;
finally
JpgPic.Free;
tempStream.Free;
end;
这是保存的方法,读取图片也要转换一下才行.
 
//显示并保存图片 JPG
procedure TMenuForm.FootImageClick(Sender: TObject);
var
tempStream:TMemoryStream;
JpgPic:TJpegImage;
S:String;
begin
if FootImage.Picture.Width=0 then
begin
if OpenDialog.Execute then
begin
S:=OpenDialog.FileName;
S:=UpperCase(Copy(S,Length(S)-2,3));
if S='JPG' then
begin
try
JpgPic:=TJpegImage.Create;
tempStream:=TMemoryStream.Create;
tempStream.Clear;
a.Dataset.Edit;
JpgPic.LoadFromFile(OpenDialog.FileName);
FootImage.Picture.bitmap.assign(JpgPic);
JpgPic.SaveToStream(tempStream);
TBlobField(a.Dataset.FieldByName('图象')).LoadFromStream(tempStream) ;
a.Dataset.Post;
finally
JpgPic.Free;
tempStream.Free;
end;
end;
end;
end;
end;

//读取并显示图片 JPG
procedure TMenuForm.ShowImage;
var
MyJpeg:TJpegImage;
MyStm:TMemoryStream;
begin
a.GetSelRow;

if not a.DataSet.FieldByName('图象').IsNull then
begin
try
MyJpeg:=TJpegImage.Create;
MyStm:=TMemoryStream.Create;
MyStm.Clear;
TBlobField(a.Dataset.FieldByName('图象')).SaveToStream(MyStm);
MyStm.Position:=0;
MyJpeg.LoadFromStream(MyStm);
FootImage.Picture.BitMap.Assign(MyJpeg);
finally
MyJpeg.Free;
MyStm.Free;
end;
end
else
begin
if FootImage.Picture<>Nil then
begin
FootImage.Picture.Assign(Nil);
end;
end;
end;
 
如果你觉得转JPG图象失真,可以用Delphi带的压缩类试一下,效果也不错的
 
多人接受答案了。
 
后退
顶部