新增圖片至資料庫 |
尚未結案
|
2b22
一般會員 ![]() ![]() 發表:1 回覆:2 積分:0 註冊:2004-04-23 發送簡訊給我 |
|
yachanga
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:24 回覆:335 積分:296 註冊:2003-09-27 發送簡訊給我 |
Hi 2b22:
試試看~
var fs: TFileStream; bs: TBlobStream; sFileName: String; begin sFileName:='C:\Test.bmp';//for test ,insert into Query1 PICTURE column try Image1.Picture.LoadFromFile(sFileName); DATABASE1.StartTransaction; With Query1 Do Begin Edit; fs := TFileStream.Create(sFileName, fmOpenRead); bs := TBlobStream(CreateBlobStream(FieldByName('PICTURE'), bmWrite)); bs.CopyFrom(fs, 0); bs.Free; fs.Free; Post; end; DATABASE1.Commit; Except DATABASE1.Rollback; End; end; |
change.jian
版主 ![]() ![]() ![]() ![]() ![]() 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
您好,這是我之前的案子,把圖檔內容存入資料庫的程式碼,如下:
function TPICTUREObj.UpdateImage(sPrjID, sPictID: String): String; var FupDate:TQuery; Jpg:TJPEGImage; Stream:TMemoryStream; begin FupDate:=TQuery.Create(FOwner); Jpg:=TJPEGImage.Create; Stream:=TMemoryStream.Create; try FupDate.DatabaseName:=FDatabase.DatabaseName; //把圖檔存入 //PICT:原掃描的圖檔,為BMP格式 try //FUpDate.Edit; FssSQL.Clear; FssSQL.Add('UPDATE PICTURE '); FssSQL.Add(' SET PICT_IMAGE=:B_PICT_IMAGE'); FssSQL.Add(' WHERE SYS_PRJ_ID = ' sPrjID); FssSQL.Add(' AND PICT_ID = ' sPictID); FupDate.SQL.Text:=FssSQL.Text; //存入資料庫一律為JPG格式 if (FImageFormat = 'BMP') then begin JPG.Assign(FImage.Picture.Graphic); JPG.CompressionQuality:=70; JPG.Compress; JPG.SaveToStream(Stream); end else if (FImageFormat = 'JPG') then begin JPG.Assign(FImage.Picture); JPG.SaveToStream(Stream); end; FupDate.ParamByName('B_PICT_IMAGE').SetBlobData(Stream.Memory,Stream.Size); if FDatabase.InTransaction then FDatabase.Rollback; FDatabase.StartTransaction; FUpDate.Prepare; FupDate.ExecSQL; Result:='圖檔寫入成功'; FDatabase.Commit; except on E:Exception do begin FDatabase.Rollback; raise Exception.Create('圖檔寫入失敗,錯誤訊息如下:'#13 e.Message); end; end; finally FUpDate.Active:=False; FupDate.Free; Stream.Free; end; end; |
terrychen
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:90 回覆:794 積分:501 註冊:2003-05-01 發送簡訊給我 |
|
timhuang
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
|
2b22
一般會員 ![]() ![]() 發表:1 回覆:2 積分:0 註冊:2004-04-23 發送簡訊給我 |
|
timhuang
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
|
2b22
一般會員 ![]() ![]() 發表:1 回覆:2 積分:0 註冊:2004-04-23 發送簡訊給我 |
|
change.jian
版主 ![]() ![]() ![]() ![]() ![]() 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
引言: 我的情況是這樣的: 我要寫一個資料庫存取的程式 因此要有一個範例資料庫來測試程 所以我想找一些像access的程式 可以在建立資料庫之後快速加入資料到資料庫 現在我參加過各位的範例後 已可以加入圖檔到資料庫 但另一個問題又出現: 如何辨別在資料庫中儲存的是jpg還是bmp??? 希望各位賜教!我之前也為這個問題傷腦筋很久,後來是乾脆在資料庫另加一個欄位儲存這個blob的內容是什麼.如果一般的檔案的話,可以直接以檔案名稱的副檔名來判斷,但如果是存在資料庫裡的話,我也在找答案 |
hagar
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
參考 How to determine the graphic format of a file:
http://www.lmc-mediaagentur.de/dpool/tips/1319.htm
function PhysicalResolveFileType(AStream: TStream): Integer; var p: PChar; begin Result := 0; if not Assigned(AStream) then Exit; GetMem(p, 10); try AStream.Position := 0; AStream.Read(p[0], 10); {bitmap format} if (p[0] = #66) and (p[1] = #77) then Result := 1; {tiff format} if ((p[0] = #73) and (p[1] = #73) and (p[2] = #42) and (p[3] = #0)) or (( p[0] = #77) and (p[1] = #77) and (p[2] = #42) and ( p[3] = #0)) then Result := 2; {jpg format} if (p[6] = #74) and (p[7] = #70) and (p[8] = #73) and (p[9] = #70) then Result := 3; {png format} if (p[0] = #137) and (p[1] = #80) and (p[2] = #78) and (p[3] = #71) and (p[4] = #13) and (p[5] = #10) and (p[6] = #26) and (p[7] = #10) then Result := 4; {dcx format} if (p[0] = #177) and (p[1] = #104) and (p[2] = #222) and (p[3] = #58) then Result := 5; {pcx format} if p[0] = #10 then Result := 6; {emf format} if (p[0] = #215) and (p[1] = #205) and (p[2] = #198) and (p[3] = #154) then Result := 7; {emf format} if (p[0] = #1) and (p[1] = #0) and (p[2] = #0) and (p[3] = #0) then Result := 7; finally Freemem( p ); end; end; |
bigcome2004
一般會員 ![]() ![]() 發表:28 回覆:19 積分:9 註冊:2004-02-17 發送簡訊給我 |
******引文********
試試看~ var fs: TFileStream;
bs: TBlobStream;
sFileName: String;
begin
sFileName:='C:\Test.bmp';//for test ,insert into Query1 PICTURE column
try
Image1.Picture.LoadFromFile(sFileName);
DATABASE1.StartTransaction;
With Query1 Do
Begin
Edit;
fs := TFileStream.Create(sFileName, fmOpenRead);
bs := TBlobStream(CreateBlobStream(FieldByName('PICTURE'), bmWrite));
bs.CopyFrom(fs, 0);
bs.Free;
fs.Free;
Post;
end;
DATABASE1.Commit;
Except
DATABASE1.Rollback;
End;
end; **********************************
不知可否請這位高手...上傳借我研究看看...
我有試過....但有些東西、元件我不太懂....
麻煩一下~~~感謝你!!
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |