[[환경]] Delphi 5.x, Oracle 8i
Query로 Blob Field에 있는 Image를 불러 오는 것은 성공을
했습니다.(PHP3로 누가 DB에 저장한 것인데...)
- 호출 Procedure
Procedure Blob_JPG(image_name: TImage; image_name2: TDBImage; Blob_Source: TStream);
Var
PIC_Stream: TStream;
PIC_JPG : TJPEGImage;
Begin
Try
PIC_Stream := Blob_Source;
PIC_JPG := TJPEGImage.Create;
try
PIC_JPG.LoadFromStream(PIC_Stream);
image_name.Picture.Assign(PIC_JPG);
image_name2.Picture.Assign(PIC_JPG);
finally
PIC_JPG.Free;
end;
Finally
PIC_Stream.Free;
End;
End;
- Call Routine
if Query1.FieldByName('V_IMAGE_TYPE').AsString = 'JPEG' Then
Blob_JPG(image1, DBImage1, Query1.CreateBlobStream(Query1.FieldByName('B_IMAGE_DATA'),bmRead))
Else Blob_GIF(image1, DBImage1, Query1.CreateBlobStream(Query1.FieldByName('B_IMAGE_DATA'),bmRead));
[[문제점]]
역으로 TImage에(혹은 TDBImage) 있는 그림을 Query로 Inert하는 방법을 모르겠습니다.
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('insert into tmp_image');
Query1.SQL.Add('(V_IMAGE_ID,N_IMAGE_NO,N_IMAGE_SEQ, V_IMAGE_NAME, V_IMAGE_TYPE, B_IMAGE_DATA)');
Query1.SQL.Add('Values');
Query1.SQL.Add('(''TEST'',1, 1, ''TEST.JPG'', ''JPEG'',:C0)');
Query1.Params[0]. --> ???
Stream, loadfromfile, asblob 등 모든것으로 해봐도 Error만 신나게 떨어지네요.
혹시 해결을 하신분이 있으면 도움 부탁드립니다.
desc tmp_image
V_IMAGE_ID NOT NULL VARCHAR2(255)
N_IMAGE_NO NOT NULL NUMBER(10)
N_IMAGE_SEQ NOT NULL NUMBER(10)
V_IMAGE_NAME NOT NULL VARCHAR2(255)
V_IMAGE_TYPE NOT NULL VARCHAR2(4)
B_IMAGE_DATA NOT NULL BLOB
혹시나 저같이 몇일동안 고통을 받으실 분들을 위해...
좀 지저분한 Code라도 이해하고 봐주세요.
환 경: Win98, Delphi5, Oracle 8i(Release 8.1.5.0.0)
목 적: JPEG및 GIF Format의 Blob data를 호출/저장
Talbe 구조
SQL>desc tmp_image
V_IMAGE_ID NOT NULL VARCHAR2(255)
N_IMAGE_NO NOT NULL NUMBER(10)
N_IMAGE_SEQ NOT NULL NUMBER(10)
V_IMAGE_NAME NOT NULL VARCHAR2(255)
V_IMAGE_TYPE NOT NULL VARCHAR2(4)
B_IMAGE_DATA NOT NULL BLOB
uses JPEG, GIFImage
cf> GIFImage : http://www.melander.dk/delphi/gifimage/index.html(gifimage.pas)
Procedure Blob_GIF(image_name: TImage; Blob_Source: TStream);
Query로 받은 GIF Stream Data를 TImage에 뿌려준다.
Procedure Blob_JPG(image_name: TImage; Blob_Source: TStream);
Query로 받은 JPG Stream Data를 TImage에 뿌려준다.
procedure TForm1.DBNavigatorClick(Sender: TObject; Button: TNavigateBtn);
Insert및 Edit 시 Procedure ImageToDB(Sender)를 호출한다.
Procedure TForm1.ImageToDB(Sender: TObject);
DB에 Image Blob Data를 저장하는 Routine
procedure TForm1.Button1Click(Sender: TObject);
Table1.Open Routine
procedure TForm1.Button2Click(Sender: TObject);
Query로 Blob Data를 가져와 화면에 Display 한다.(Blob_GIF, Blob_JPG 호출)
처음엔 그냥 Table에 있는 Blob Data를 사용을 하였는데 Program이
JPG와 GIF를 제대로 읽지를 못해서 Blob(Image)만 Query로 호출하여 사용한다.
혹시 Table로 Open을 해서 이상이 없으면 연락 주세요.
-------------------- Sourece 시작 --------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, JPEG, GIFImage,
ExtCtrls, Db, DBTables, DBCtrls, StdCtrls, Mask, ExtDlgs;
type
TForm1 = class(TForm)
Panel1: TPanel;
DBNavigator: TDBNavigator;
ScrollBox: TScrollBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
edtV_image_id: TDBEdit;
edtN_image_no: TDBEdit;
edtN_image_seq: TDBEdit;
edtV_image_name: TDBEdit;
edtV_image_type: TDBEdit;
Table1: TTable;
Table1V_IMAGE_ID: TStringField;
Table1N_IMAGE_NO: TFloatField;
Table1N_IMAGE_SEQ: TFloatField;
Table1V_IMAGE_NAME: TStringField;
Table1V_IMAGE_TYPE: TStringField;
Table1B_IMAGE_DATA: TBlobField;
DataSource1: TDataSource;
image: TImage;
OpenPictureDialog1: TOpenPictureDialog;
qrTmp: TQuery;
Button1: TButton;
Button2: TButton;
procedure DBNavigatorClick(Sender: TObject; Button: TNavigateBtn);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
private
{ Private declarations }
Procedure ImageToDB(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
FileName : string;
implementation
{$R *.DFM}
Procedure Blob_GIF(image_name: TImage; Blob_Source: TStream);
Var
PIC_Stream: TStream;
PIC_Gif : TGIFImage;
Begin
Try
PIC_Stream := Blob_Source;
PIC_Gif := TGIFImage.Create;
try
PIC_Gif.LoadFromStream(PIC_Stream);
image_name.Picture.Assign(PIC_Gif);
finally
PIC_Gif.Free;
end;
Finally
PIC_Stream.Free;
End;
End;
Procedure Blob_JPG(image_name: TImage; Blob_Source: TStream);
Var
PIC_Stream: TStream;
PIC_JPG : TJPEGImage;
Begin
Try
PIC_Stream := Blob_Source;
PIC_JPG := TJPEGImage.Create;
try
PIC_JPG.LoadFromStream(PIC_Stream);
image_name.Picture.Assign(PIC_JPG);
finally
PIC_JPG.Free;
end;
Finally
PIC_Stream.Free;
End;
End;
procedure TForm1.DBNavigatorClick(Sender: TObject; Button: TNavigateBtn);
begin
case Button of
nbFirst : ;
nbPrior : ;
nbNext : ;
nbLast : ;
nbInsert : ImageToDB(Sender);
nbDelete : ;
nbEdit : ImageToDB(Sender);
nbPost : ;
nbCancel : ;
nbRefresh : ;
end;
end;
Procedure TForm1.ImageToDB(Sender: TObject);
Var
FileStream: TFileStream;
BlobStream: TBlobStream;
Begin
Application.ProcessMessages;
If OpenPictureDialog1.Execute Then
Begin
FileStream := TFileStream.Create(OpenPictureDialog1.FileName, fmOpenRead);
BlobStream := TBlobStream.Create(Table1B_IMAGE_DATA, bmReadWrite);{Seek to the Beginning of the stream}
BlobStream.Seek(0, soFromBeginning);{Delete any data that may be there}
BlobStream.Truncate;{Copy from the FileStream to the BlobStream}
BlobStream.CopyFrom(FileStream, FileStream.Size);{Free the streams}
FileStream.Free;
BlobStream.Free;{Post the record}
// Table1.Post;{Enable the buttons}
End;
End;
procedure TForm1.Button1Click(Sender: TObject);
begin
Table1.Open;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
qrTmp.Close;
qrTmp.SQL.Clear;
qrTmp.SQL.Add('select V_IMAGE_ID,N_IMAGE_NO,N_IMAGE_SEQ, V_IMAGE_NAME, V_IMAGE_TYPE,B_IMAGE_DATA');
qrTmp.SQL.Add('from tmp_image');
qrTmp.SQL.Add('where v_image_id = :C0');
qrTmp.SQL.Add('and n_image_no = :C1');
qrTmp.SQL.Add('and n_image_seq = :C2');
qrTmp.Params[0].Value := edtV_image_id.Text;
qrTmp.Params[1].Value := strtoint(edtN_image_no.Text);
qrTmp.Params[2].Value := strtoint(edtN_image_seq.Text);
qrTmp.Open;
if qrTmp.FieldByName('V_IMAGE_TYPE').AsString = 'JPEG' Then
Blob_JPG(Image, qrTmp.CreateBlobStream(qrTmp.FieldByName('B_IMAGE_DATA'),bmRead))
Else If Table1.FieldByName('V_IMAGE_TYPE').AsString = 'GIF' Then
Blob_GIF(Image, qrTmp.CreateBlobStream(qrTmp.FieldByName('B_IMAGE_DATA'),bmRead));
end;
procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
If DataSource1.DataSet.State = dsEdit Then Exit;
If DataSource1.DataSet.State = dsInsert Then Exit;
Button2Click(Sender);
end;
end.
-------------------- Sourece 끝 --------------------