delphi 三层 dataclient 存储图片
最近开发用到了D7 三层,涉及到图片问题。存储图片的时候无法使用clientDataset, LoadFromStream来保存图片。在网上找了些发现很多人有类似问题。用params来存储文字也发现有截断问题,最好的方式就是‘’‘+str+‘’‘的方式来存储。思路就是把图片转换成字符串来存储。
//数据库中建表:
create table ImgCreate (No int primary key identity(1,1),img varchar(max))
//加载图片:
if OpenPictureDialog1.Execute then
begin
if OpenPictureDialog1.FileName<>'' then
begin
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
imgName := JpegToTex(OpenPictureDialog1.FileName);//图片转换成字符串函数
end;
end;
//JpegToTex 函数实现
function JpegToTex(FileName: string): WideString;
var
FileStream : TFileStream;
StringStream : TStringStream;
FSize : integer;
FileData : string;
i : integer;
DataStr : WideString;
begin
try
FileStream := TFileStream.Create (FileName, fmOpenRead);
StringStream := TStringStream.Create('');
FSize := FileStream.Size;
StringStream.CopyFrom(FileStream, FSize);
FileData := StringStream.DataString;
DataStr := '';
for i := 1 to Length(FileData) do
begin
DataStr := DataStr + InttoHex(Ord(FileData[i]), 2);
end;
Result := DataStr;
finally
FreeAndNil(FileStream);
FreeAndNil (StringStream);
end;
end;
//保存到数据库:
with clientdataset1 do
begin
close;
commandText ;= 'Insert Into ImgCreate(img) Values(imgName)' ;
execute;
end
//从数据库读取
// var imgStr:widestring;
//ds:tdatasource;
imgStr := ds.DataSet.FieldByName(img).Value;
if imgStr<>'' then
begin
Image1.Picture.Graphic := TextToJpeg(imgStr);
end else
begin
Image1.Picture.Graphic := nil;
end;
//TextToJpeg函数实现()
function TextToJpeg(str: WideString): TJPEGImage;
var
buf : array of Byte;
DataLength : Integer;
i : Integer;
DataStr : WideString;
ByteFile : file of byte;
jpg : TJPEGImage;
tmpFileName: string;
begin
DataStr := str;
DataLength := Length(DataStr) div 2-1;
SetLength(buf, DataLength);
for i := 1 to DataLength do
begin
buf[i - 1] := StrToInt('$' + copy(DataStr, (i - 1) * 2 + 1, 2));
end;
tmpFileName := ExtractFilePath(ParamStr(0))+'tmp.jpg';
AssignFile(ByteFile, tmpFileName);
Rewrite(ByteFile);
for i := 0 to DataLength - 1 do
begin
write(ByteFile, buf[i]);
end;
CloseFile(ByteFile);
Result := TJPEGImage.Create;
Result.LoadFromFile(tmpFileName);
DeleteFile(tmpFileName);
end;
- 点赞
- 收藏
- 分享
- 文章举报
- DELPHI DATASNAP 2010 入门操作(2)不写一行代码,绿色三层我也行
- Oracle.DataAccess.Client.OracleException ORA-00600: 内部错误代码,通过C#执行上面存储过程报错,而通过PLSQL执行不报错。谁知道原因。
- delphi三层中怎么保存图片到sqlserver数据库
- Swift Core Data 图片存储与读取Demo
- 跨服务器存储图片使用FTPClient 类
- DELPHI ClientData使用详解
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 简单搞一下 System.Data.OracleClient调用带blob、clob等大字段类型参数的存储过程!
- 如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中
- Delphi2010中采用DataSnap的三层网络架构服务器获取客户端ip,端口信息
- android 存储图片到data目录和读取data目录下的图片
- android 存储图片到data目录和读取data目录下的图片
- Swift Core Data 图片存储与读取Demo
- delphi存储图片路径
- DELPHI DATASNAP 2010 入门操作(2)不写一行代码,绿色三层我也行
- Delphi:利用TreeView 的data属性存储相关数据
- 如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中
- Swift Core Data 图片存储与读取Demo
- VS通过Oracle驱动连接数据库,对图片进行存取操作(Oracle.DataAccess.Client.dll)
- delphi中三层结构怎样设计?一个管理软件的应用服务器端需要建多少个Remote Data Module?