各位:如何将JPG,JPEG,ICO等图片文件保存到数据库中(SQL,DM2,ORACLE等)。(50分)

  • 主题发起人 主题发起人 ansili
  • 开始时间 开始时间
A

ansili

Unregistered / Unconfirmed
GUEST, unregistred user!
各位:如何将JPG,JPEG,ICO等图片文件保存到数据库中(SQL,DM2,ORACLE等)。
用什么将非二进制文件转换为二进制文件代码如何实现[blue][/blue](不要工具转换)。
 
用Blob类型字段
 
procedure TForm1.Button1Click(Sender: TObject);
var
C: TClipboard;
begin
C := TClipboard.Create;
try
if Clipboard.HasFormat(CF_BITMAP) then

DBImage1.PasteFromClipboard
else
ShowMessage('Clipboard does not contain a bitmap!');
finally
C.Free;
end;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
Table1Bitmap.LoadFromFile(
'c:/delphi/images/splash/16color/construc.bmp');
end;

procedure TForm1.Button3Click(Sender: TObject);
var
B: TBitmap;
begin
B := TBitmap.Create;
try
B.LoadFromFile('c:/delphi/images/splash/16color/athena.bmp');
DBImage1.Picture.Assign(B);
finally
B.Free;
end;
end;
///////////////////////////////////////////////////////////
var
st: TStringStream;
begin
st := tstringstream.create('');
bitmap.savetostream(st);
query1.sql.text := 'insert into table Image_field values :bmp';
query1.parambyname('bmp').asblob := st.datastring;
query1.execsql;
st.free;
end;
////////////////////////////////////////////////////////////////////////
unit Unit1;

interface

{$IFDEF WIN32}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, Db,
DBTables;
{$ELSE}
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, DBTables, DB, Grids, DBGrids, ExtCtrls, StdCtrls;
{$ENDIF}

type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Image1: TImage;
Button1: TButton;
Table1Name: TStringField;
Table1WMF: TBlobField;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
private
{ Private declarations }
FileName : string; {Used to hold a temp file name}
procedure LoadWMFFromDatabase; {loads a WMF from the database}
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
{Used for loading metafiles}
OpenDialog1.Filter := 'Metafiles (*.wmf)|*.wmf';
OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
Image1.Stretch := true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{Erase the temp file if it exists}
if FileName <> '' then
DeleteFile(FileName);
end;

{This function gets a temporary file name form the system}
function GetTemporaryFileName : string;
{$IFNDEF WIN32}
const MAX_PATH = 144;
{$ENDIF}
var
{$IFDEF WIN32}
lpPathBuffer : PChar;
{$ENDIF}
lpbuffer : PChar;
begin
{Get the file name buffer}
GetMem(lpBuffer, MAX_PATH);
{$IFDEF WIN32}
{Get the temp path buffer}
GetMem(lpPathBuffer, MAX_PATH);
{Get the temp path}
GetTempPath(MAX_PATH, lpPathBuffer);
{Get the temp file name}
GetTempFileName(lpPathBuffer,
'tmp',
0,
lpBuffer);
{Free the temp path buffer}
FreeMem(lpPathBuffer, MAX_PATH);
{$ELSE}
{Get the temp file name}
GetTempFileName(GetTempDrive('C'),
'tmp',
0,
lpBuffer);
{$ENDIF}
{Create a pascal string containg}
{the temp file name and return it}
result := StrPas(lpBuffer);
{Free the file name buffer}
FreeMem(lpBuffer, MAX_PATH);
end;

procedure TForm1.LoadWMFFromDatabase;
var
FileStream: TFileStream; {a temp file}
BlobStream: TBlobStream; {the WMF Blob}
begin
Image1.Picture.Metafile.Assign(nil);
{Create a blob stream for the WMF blob}
BlobStream := TBlobStream.Create(Table1WMF, bmRead);
if BlobStream.Size = 0 then begin
BlobStream.Free;
Exit;
end;
{if we have a temp file then erase it}
if FileName <> '' then
DeleteFile(FileName);
{Get a temp file name}
FileName := GetTemporaryFileName;
{Create a temp file stream}
FileStream := TFileStream.Create(FileName,
fmCreate or fmOpenWrite);
{Copy the blob to the temp file}
FileStream.CopyFrom(BlobStream, BlobStream.Size);
{Free the streams}
FileStream.Free;
BlobStream.Free;
{Dispaly the image}
Image1.Picture.Metafile.LoadFromFile(FileName);
end;

{Save a wmf file to the database}
procedure TForm1.Button1Click(Sender: TObject);
var
FileStream: TFileStream; {to load the wmf file}
BlobStream: TBlobStream; {to save to the blob}
begin
{Allow the button to repaint}
Application.ProcessMessages;
if OpenDialog1.Execute then begin
{Turn off the button}
Button1.Enabled := false;
{Assign the avi file name to read}
FileStream := TFileStream.Create(OpenDialog1.FileName,
fmOpenRead);
Table1.Edit;
{Create a BlobStream for the field Table1WMF}
BlobStream := TBlobStream.Create(Table1WMF, bmReadWrite);
{Seek to the Begginning 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;
{Load the metafile in to a TImage}
LoadWMFFromDatabase;
{Enable the button}
Button1.Enabled := true;
end;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
if (Sender as TDataSource).State = dsBrowse then
LoadWMFFromDatabase;
end;

end.
 
能不能谈谈你的思想?
(JPG文件)
 
可以先转换为BMP保存到数据库
如果要输出,再转换为JPEG
TJPEG
 
在Delphi中用流的方式可以把图片读入数据库
若是Jpeg,还可以压缩后在保存,读取的时候再解压,很好用
记住:用Stream,你可以搜索一下
 
用OLE 各种图片文件都能存入数据库
 
怎么用OLE啊,对不起我刚学。
 
怎么转换为BMP啊。谢谢
 
直接改后缀就行了
 
从前写的一段
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
Image1.picture.loadfromfile('d:/work/pic.bmp');
MyJPEG:=TJPEGImage.Create;
Try
MyJPEG.Assign(Image1.Picture.Graphic);
MS:=TMemoryStream.Create;
MyJPEG.SaveToStream(MS);
MS.Position:=0;
ADOQuery1.Append;
ADOQuery1.FieldByName('name').Value:='1';
TBlobField(ADOQuery1.FieldByName('pic')).LoadFromStream(MS);
ADOQuery1.Post;
messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);
MS.Free;
Finally
MyJPEG.Free;
end;
end;
 
努力学习
 
努力学习
 
只用一个函数
loadfromfile();
 
可以采用文件流的方式,对任何文件都有用.
如果想交流,可以联系我.
 
TBlobField(Fieldbyname('YourFiled')).loadformfile('myicon.ico');
 
to tclongbo:
如何把一个图标(用API从某个文件中动态取得)存入数据库,
然后再取出来赋给listview的某个节点?或者加到imagelist里面?
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部