저장은 되는데 읽기가 안됩니다.
읽기에서 'Bitmap Image is not Valid' 라는 에러가 납니다.
원인을 모르게어요
MSSQL에서 테이불은
열이름 데이터형식 길이
EmpCode varchar 10
Picture image 16
간단히 이렇게 디자인되었습니다.
Delphi 7을 사용하고 있습니다.
////////////////////////////////////////////////////////////////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DB, ADODB, ExtDlgs, Jpeg, Grids, DBGrids,
DBCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
QQ: TADOQuery;
Button1: TButton;
OPD: TOpenPictureDialog;
btnSave: TButton;
btnRead: TButton;
DataSource1: TDataSource;
DBImage1: TDBImage;
Panel1: TPanel;
IMG: TImage;
Panel2: TPanel;
IMG1: TImage;
procedure Button1Click(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure btnReadClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//그림찾기
procedure TForm1.Button1Click(Sender: TObject);
begin
if OPD.Execute then
IMG.Picture.LoadFromFile(OPD.FileName);
end;
//저장
procedure TForm1.btnSaveClick(Sender: TObject);
var BitImage : TBitmap;
JpegImage : TJPEGImage;
MemStream : TMemoryStream;
begin
QQ.Close;
QQ.SQL.Clear;
QQ.SQL.Add('INSERT INTO TMP_PICTURE (EmpCode, Picture )');
QQ.SQL.Add('VALUES (''2000'', :Picture)');
if IMG.Picture.Graphic is TBitmap then begin
BitImage := TBitmap.Create;
MemStream := TMemoryStream.Create;
MemStream.Position := 0;
if IMG.Picture.Graphic = nil then begin
QQ.Parameters.ParamByName( 'PICTURE' ).Assign( IMG.Picture.Bitmap );
end
else begin
BitImage.Assign( IMG.Picture.Bitmap );
BitImage.SaveToStream( MemStream );
QQ.Parameters.ParamByName( 'PICTURE' ).LoadFromStream( MemStream, ftBlob );
end;
QQ.ExecSQL;
BitImage.Destroy;
MemStream.Destroy;
end else
if IMG.Picture.Graphic is TJPEGImage then begin
JpegImage := TJPEGImage.Create;
MemStream := TMemoryStream.Create;
MemStream.Position := 0;
if IMG.Picture.Graphic = nil then begin
QQ.Parameters.ParamByName( 'PICTURE' ).Assign( IMG.Picture.Bitmap );
end
else begin
JPEGImage.Assign( IMG.Picture.Graphic );
JPEGImage.SaveToStream( MemStream );
QQ.Parameters.ParamByName( 'PICTURE' ).LoadFromStream( MemStream, ftBlob );
end;
QQ.ExecSQL;
JPEGImage.Destroy;
MemStream.Destroy;
end;
end;
//읽기
procedure TForm1.btnReadClick(Sender: TObject);
begin
QQ.Close;
QQ.SQL.Clear;
QQ.SQL.Add('select PICTURE from TMP_PICTURE');
QQ.SQL.Add('where EMpCode = ''2000''');
QQ.Open;
if QQ.FieldByName('PICTURE').IsBlob then
IMG1.Picture.Assign(QQ.FieldByName('PICTURE')); //여기서 에러 'Bitmap Image is not Valid'
end;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
샘풀 프로그램을 올립니다. 고견부탁합니다
비슷하게 만들어서 테스트 해봤는데 잘 처리됩니다. ^^;
아래 코드대신....
저장할때 Image1.Picture.Graphic.SaveToStream(memStream);
이렇게 저장했습니다.