用dephi先写个dll
代码如下
function CompressionBmpToJpeg(loadFilePath: pchar; saveFilePath: pchar; CompressionQuality: dword): Boolean; stdcall;
var
Bmp: TBitmap;
Jpg: TJpegImage;
MyImage: TMemoryStream; //内存流对象
Buffer: Word;
begin
if not FileExists(loadFilePath) then
begin
Result := False;
Exit;
end;
Bmp := TBitmap.Create;
Jpg := TJpegImage.Create;
MyImage := TMemoryStream.Create; //建立内存流对象
try
MyImage.Position := 0; //移动指针到最开头的位置
MyImage.LoadFromFile(loadFilePath);
MyImage.ReadBuffer(Buffer,2); //读取文件的前2个字节,放到Buffer里面
if Buffer = $4D42 then //如果前两个字节是以4D42[低位到高位] bmp
begin
Bmp.LoadFromFile(loadFilePath);
Jpg.Assign(Bmp);
Jpg.CompressionQuality := CompressionQuality;
Jpg.SaveToFile(saveFilePath);
DeleteFile(loadFilePath);
Result := True;
end
else
begin
Result := False;
end;
finally
Bmp.Free;
Jpg.Free;
MyImage.Free;
end;
end;
pb 的代码:
ls_savefilename=wf_convertBmpToJpg(ls_fileName,ls_jpgFile)
wf_convertBmpToJpg函数:
time ld_begin
long ll_file
if fileLength(as_bmp) > 0 then
ld_begin=now()
if CompressionBmpToJpeg(as_bmp,as_jpg,80) then
do while true
ll_file = fileOpen(as_jpg,streamMode!,read!,LockReadWrite!)
if ll_file>0 then
fileClose(ll_file)
return as_jpg
end if
if now()>RelativeTime(ld_begin,6) then return as_bmp
loop
else
return as_bmp
end if
else
return as_bmp
end if
2)、cb_save的clicked事件:(本文只处理了新增记录,有兴趣的朋友可以完善他)
long ll_no
string ls_name
string ls_photofile
integer li_filenum
integer li_loops
integer li_counter
long ll_filelen
blob lb_read
blob lb_picture
If sle_no.text = '' or sle_name.text = '' or sle_photo.text = '' then return
ll_no = long(sle_no.text) //号码
ls_name = sle_name.text //姓名
ls_photofile = sle_photo.text //照片文件
//插入记录,先写no,name字段
Insert into blobtest (no,name)
values (:ll_no, :ls_name);
If sqlca.sqlcode = -1 then
Rollback;
Messagebox('插入记录出错',Sqlca.Sqlerrtext)
Return
Else
Commit;
End if
// 获 取 文 件 的 大 小
ll_filelen = FileLength(ls_photofile)
//打开文件
li_filenum = FileOpen(ls_photofile,STREAMMODE!, READ!,LOCKREAD!)
/*FileRead() 函 数 不 支 持 读 取 大 于32K 的 文 本,
计 算 将 使 用 FileRead 函 数 的 次 数*/
IF ll_filelen > 32765 THEN
li_loops = ( (ll_filelen - 1) / 32765 ) + 1
ELSE
li_loops = 1
END IF
//读文件
FOR li_counter = 1 to li_loops
FileRead(li_filenum, lb_read )
lb_picture = lb_picture + lb_read
NEXT
FileClose(li_filenum)
//显示图片
p_1.SetPicture(lb_picture)
//写入数据库
UPDATEBLOB blobtest
SET photo = :lb_picture
WHERE no = :ll_no;
IF Sqlca.SQLNRows > 0 THEN
COMMIT;
Messagebox('提示信息','记录保存成功!')
Else
Rollback;
Messagebox('提示信息','记录保存成功,照片文件保存失败!' + sqlca.sqlerrtext)
END IF
3)、cb_exit的clicked事件:
close(parent)
4)、cb_query的clicked事件:
blob lb_picture
long ll_No
if sle_no.text = '' then return
ll_no = long(sle_no.text) //号码
SelectBlob photo
Into :lb_picture
From blobtest
where no = :ll_no;
//显示图片
p_1.SetPicture(lb_picture)