如何把JPEG文件导入到数据库中 ( 积分: 100 )

  • 主题发起人 主题发起人 smallfox
  • 开始时间 开始时间
S

smallfox

Unregistered / Unconfirmed
GUEST, unregistred user!
我用下面的指令只能导BMP的,不能导JPG的。
var
Picture1: TPicture;
begin
if OpenPictureDialog1.Execute then
begin
Picture1 := TPicture.Create;
if FileExists(OpenPictureDialog1.FileName) = true then
begin
Picture1.LoadFromFile(OpenPictureDialog1.FileName);
FormDM.IBDSpic.FieldByName('pic').Assign(Picture1);
end;
end;
end;
 
我用下面的指令只能导BMP的,不能导JPG的。
var
Picture1: TPicture;
begin
if OpenPictureDialog1.Execute then
begin
Picture1 := TPicture.Create;
if FileExists(OpenPictureDialog1.FileName) = true then
begin
Picture1.LoadFromFile(OpenPictureDialog1.FileName);
FormDM.IBDSpic.FieldByName('pic').Assign(Picture1);
end;
end;
end;
 
可以把常见的图片格式一律转成JPG流的函数,改进一下就可以达到你的要求,要引用JPEG及tGifImage(网上有很多下载的,如不需要,去除即可)
Procedure AnyToJpg(Const FileName:String; var JpgStream:TMemoryStream; MaxSize:Integer);
var tpPic:TImage;
tpJpg:TJpegImage;
tpGif:TGifImage;
tpBmp:TBitMap;
TpStream:TMemoryStream;
tpRatio:Double;
tpSize,tpWidth,tpHeight:Integer;
begin
//不管什么图片,一律转成JPG保存
if Not FileExists(FileName) then begin
JpgStream:=Nil;
Exit;
end;
try
tpPic:=tImage.Create(nil);
tpJpg:=TJpegImage.create;
tpGif:=TGifImage.Create;
tpBmp:=TBitmap.Create;
if Pos('.GIF',UpperCase(FileName))>0 then
begin
tpGif.LoadFromFile(FileName);
tpBmp.Assign(tpGif.Bitmap);
tpPic.Picture.Assign(tpBmp);
end
else
tpPic.Picture.LoadFromFile(FileName);
if Assigned(tpPic.Picture) then
tpJpg.Assign(tpPic.Picture.Graphic)
else
tpJpg.Assign(nil);
tpStream:=TMemoryStream.Create;
tpJpg.SaveToStream(tpStream);
tpStream.Position:=0;
if MaxSize<=0 then
jpgStream:=tpStream
else
begin
//查看jpg图的尺寸是否超标,如超标,则自动缩成指定大小
tpSize:=tpStream.Size;
if tpSize > MaxSize*1024 then //好像还不行,再说
begin
ShowMessage('Image &quot;'+FileName+'&quot; Size Over than Max Size:'+Trim(IntToStr(MaxSize))+' KB');
JpgStream:=Nil;
Exit;
{ //把JPG自动压缩成指定大小,可惜没做到
tpRatio:= RoundFloat(Sqrt(tpSize / (MaxSize * 1024)),6);
tpWidth:=Round(tpJpg.Width / tpRatio);
tpHeight:=Round(tpJpg.Height / tpRatio);
tpJpg.Performance:=jpBestSpeed;
tpjpg.CompressionQuality:=50;
tpJpg.JPEGNeeded;
tpJpg.Compress;
if not Assigned(JpgStream) then
JpgStream:=TMemoryStream.Create;
ZoomPic(tpStream,tpWidth,tpHeight,jpgStream);
tpJpg.CompressionQuality:=20;
tpJpg.JPEGNeeded;
tpJpg.Compress;
tpJpg.SaveToStream(tpStream);
JpgStream:=tpStream; }
end
else
jpgStream:=tpStream;
end;
JpgStream.Position:=0;
finally
tpPic.Free;
tpJpg.Free;
tpGif.Free;
tpBmp.Free;
end;
end;
 
procedure TFrmUplaodPic.SaveImage(ADS: TADODataSet;PicFieldName:string);
{功能:把图像保存进数据库中
入口:TADODataSet数据源(LockType := ltBatchOptimistic)、数据源图像字段名称
时间:20020625
编者:Tony Liu }
var
MyJPEG: TJPEGImage;
MS: TMemoryStream;
Image: TImage;
begin
Image := TImage.Create(nil);
image.Picture.LoadFromFile('d:/123.jpg');
MyJPEG := TJPEGImage.Create;
try
with MyJPEG do
begin
Assign(Image.Picture.Graphic);
MS := TMemoryStream.create;
SaveToStream(MS);
MS.Position := 0;
ADS.Edit;
//PicFieldName是数据表中的image型字段名称
TBlobField(ADS.FieldbyName(PicFieldName)).LoadFromStream(MS);
image.Picture.CleanupInstance;
end;
finally
MyJPEG.Free;
Image.Free;
Image := nil;
end;
end;
调用函数后需要把TADODataSet进行UpdateBatch才能真正保存数据
 
后退
顶部