从SQL Server 读取图片

写代码养猫 2009-02-19 01:24:50
加精

unit clsHRMRegister;
interface
uses
DBConnector, clsBaseDataProcess, Graphics, Classes, jpeg, DB;
type
THRMRegister = class(TBaseDataProcess)
private
//省略
protected
procedure InitData;override;
procedure RecordToClass;override;
procedure ClassToRecord;override;
public
//***********************省略属性
destructor Destroy; override;
end;

implementation

procedure THRMRegister.InitData;
begin
inherited;
Photo := TStream.Create;
with Query do
begin
Connection := TDBConnector.Instance.Connection;
SQL.Text := 'Select * From TB_Employee';
Open;
end;
DataSource.DataSet := Query;
end;

procedure THRMRegister.RecordToClass;
begin
with Query do
begin
TBlobField(FieldByName('Photo')).SaveToStream(Photo);
end;
end;

procedure THRMRegister.ClassToRecord;
begin
with Query do
begin
TBlobField(FieldByName('Photo')).LoadFromStream(Photo);
end;
end;

destructor THRMRegister.Destroy;
begin
Photo.Free;
inherited;
end;

end.



unit HRMRegisterForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, TemplateDataOperateForm, ActnList, StdCtrls, Buttons, ExtCtrls,
GridsEh, DBGridEh, ComCtrls, ModuleManager, Menus, clsCommon;

type
TfrmHRMRegister = class(TfrmTemplateDataOperate)
//省略
private
{ Private declarations }
protected
procedure InitForm;override;
procedure ClassToEdit;override;
procedure EditToClass;override;
function CheckInput:Boolean;override;
public
{ Public declarations }
end;

var
frmHRMRegister: TfrmHRMRegister;

implementation
uses
clsHRMRegister, ContractForm;
{$R *.dfm}

procedure TfrmHRMRegister.InitForm;
begin
//省略
end;

procedure TfrmHRMRegister.ClassToEdit;
begin
with THRMRegister(DataObj) do
begin
img1.Picture.Graphic.LoadFromStream(Photo);//这里出错,

end;
end;

procedure TfrmHRMRegister.EditToClass;
begin
with THRMRegister(DataObj) do
begin
img1.Picture.Graphic.SaveToStream(Photo); //这里不出错
end;
end;

function TfrmHRMRegister.CheckInput:Boolean;
begin
Result := False;
//省略
Result := True;
end;

procedure TfrmHRMRegister.actPhotoExecute(Sender: TObject);
var
sFileName:string;
begin
inherited;
if dlgOpen1.Execute then
begin
sFileName := dlgOpen1.FileName;
img1.Picture.LoadFromFile(sFileName);
end;
end;

initialization
TModuleManager.Instance.RegisterModule('人事档案登记',TfrmHRMRegister);

end.


谁帮忙分析下原因
...全文
1715 64 打赏 收藏 转发到动态 举报
写回复
用AI写文章
64 条回复
切换为时间正序
请发表友善的回复…
发表回复
邪魅 2011-10-19
  • 打赏
  • 举报
回复
把图片保存到数据库?
这种方式不好吧
山东蓝鸟贵薪 2011-10-19
  • 打赏
  • 举报
回复
以内存方式读取,再用控件显示出来即可
yingsuixindong 2011-06-18
  • 打赏
  • 举报
回复
学习中~~~~~~~~~~~~~~
auqfiudh 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 59 楼 pop1030123 的回复:]
是Delphi/Pascal吗?
好像很局限啊…………
[/Quote]
呵呵 好像什么都 能做.什么都不好做..
lee_lp 2009-06-03
  • 打赏
  • 举报
回复
学习中~~~~~~~~~~~~~~
pop1030123 2009-02-28
  • 打赏
  • 举报
回复
是Delphi/Pascal吗?
好像很局限啊…………
feifeiyiwen 2009-02-25
  • 打赏
  • 举报
回复
up
pilicat 2009-02-25
  • 打赏
  • 举报
回复
嗯,路过,收藏.
ldyqz2003 2009-02-24
  • 打赏
  • 举报
回复
UP
violet0909 2009-02-23
  • 打赏
  • 举报
回复
沙发~~~~受益匪浅呀
iM臭皮匠 2009-02-23
  • 打赏
  • 举报
回复
之前我也遇到过相同的问题~~~~~又一次的遇见,温习了
yinyuanfei 2009-02-22
  • 打赏
  • 举报
回复
呵呵,深奥呀,学习学习。
xiaodahero 2009-02-21
  • 打赏
  • 举报
回复
Image1.Picture.Bitmap.LoadFromStream试
freedomwood 2009-02-21
  • 打赏
  • 举报
回复
太深奥了,呵呵,路过学习了
calvinp 2009-02-20
  • 打赏
  • 举报
回复
学习下先。新来滴
小曦子 2009-02-20
  • 打赏
  • 举报
回复
学习..
nettman 2009-02-20
  • 打赏
  • 举报
回复
Mark!
starluck 2009-02-20
  • 打赏
  • 举报
回复
{
这里2次判断不一样,第一个if 进去 CheckPicFormat 返回的值是BMP , else if 的时候返回的时 JPEG,
所以每次都是到错误信息那,为什么呢?
}


國為 checkPicFormat 我只判斷了二個,當你將流的 position 移動了位置後,肯定不對的。


// checkpicoformat 前你檢查下 Photo.size 是多大? PHOTO是 Tmemostream;
写代码养猫 2009-02-20
  • 打赏
  • 举报
回复

procedure TfrmHRMRegister.ClassToEdit;
var
M_JPG:TJPEGImage;
M_BMP:TBitmap;
begin
// checkpicoformat 前你檢查下 Photo.size 是多大? PHOTO是 Tmemostream;
with THRMRegister(DataObj) do
begin
M_JPG := TJPEGImage.Create;
M_BMP := TBitmap.Create;
Photo.Position:=0;
if UpperCase(CheckPicFormat(Photo)) = 'JPEG' then
begin
Photo.Position:=0;
M_JPG.LoadFromStream(Photo);
img1.Picture.Graphic := M_JPG;
end
{
这里2次判断不一样,第一个if 进去 CheckPicFormat 返回的值是BMP , else if 的时候返回的时 JPEG,
所以每次都是到错误信息那,为什么呢?
}
else if UpperCase(CheckPicFormat(Photo)) = 'BMP' then
begin
Photo.Position:=0;
M_BMP.LoadFromStream(Photo);
img1.Picture.Graphic := M_BMp;
end else
begin
TMyCommon.MsgError('读取照片出错!',Handle);
Exit;
end;

try
finally
FreeAndNil(M_JPG);
FreeAndNil(M_BMP);
end;
end;
end;


写代码养猫 2009-02-20
  • 打赏
  • 举报
回复
2359350
加载更多回复(43)

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧