在报表中显示指定URL的照片

本文介绍如何使用ReportMachine在报表中加载指定URL的照片。通过OnBeforePrint事件,从URL下载图片并显示在TRMPictureView控件中。需注意在uses中引入URLMon单元。
在报表中显示指定URL的照片

以下源码演示如何在ReportMachine报表中显示对应记录指定文件名称的照片,并且照片从指定的URL中下载。其原理是,在报表显示前(OnBeforePrint事件),找到要显示照片的TRMPictureView控件,再从揚hoto?#23383;段得到文件,从指定的URL下载该文件名称的照片(jpg文件)到临时文件夹,再将临时文件夹中的照片显示到TRMPictureView控件中。

需要注意的是,首先要在uses 子句中加入URLMon单元的引用。在窗口创建时得到临时文件,当然可以是其它具体的文件,只不过每次报表预览积累下来的照片会占用不少的磁盘空间。

主要代码在RMReport1的OnBeforePrint事件中,首先找到要显示的TRMPictureView控件,这个控件在设计ReportMachine报表时加入,并赋予名称(这里为損icPhoto?#65289;,代码中的名称要与报表中的名称对应,否则永远找不到。在找到控件后,读取保存照片文件名称的字段(这里为揚hoto?#65289;,再将指定的URL(这里定义为常数URL)加上文件名称作为完整的下载URL,必须是有效的URL,可以在服务器中指定对应的虚拟目录,事先在IE中测试是否有效。将文件下载到临时目录下相同名称的文件,再将该文件显示到picPhoto控件中。

该程序在Windows2000 Server + Delphi6 + ReportMachine3.0中测试过。已知的BUG:下载URL无效时响应会变得很慢,且DownLoadFile()函数的返回结果总为True。

unit u_frmURLReport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, Grids, DBGrids, ADODB, ExtCtrls, RM_Dataset,
  RM_Class, RM_Designer, RM_Common, ComCtrls, URLMon;   // Use "URLMon" first!

type
  TfrmURLReport = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ADOQuery1EmpNo: TStringField;
    ADOQuery1EmpName: TStringField;
    ADOQuery1Company: TStringField;
    ADOQuery1ClassName: TStringField;
    ADOQuery1CardNo: TIntegerField;
    ADOQuery1Photo: TStringField;
    Panel1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    RMReport1: TRMReport;
    RMDesigner1: TRMDesigner;
    RMDBDataSet1: TRMDBDataSet;
    StatusBar1: TStatusBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure RMReport1BeforePrint(Memo: TStrings; View: TRMReportView);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    PhotoPath: String;
  public
    { Public declarations }
  end;

var
  frmURLReport: TfrmURLReport;

implementation

uses u_Download;

{$R *.dfm}

{ Get the temporary folder }
function GetTempPathName: String;
var
  Buf: PChar;
begin
  Result:= '';
  GetMem(Buf, 255);
  try
    if GetTempPath(255, Buf) <> 0 then
      Result:= String(Buf);
  finally
    FreeMem(Buf, 255);
  end;
end;

{ Download file from the specified URL }
function DownloadFile(Source, Dest: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest),
      0, nil) = 0;
  except
    Result := False;
  end;
end;

{ Filter the records }
procedure TfrmURLReport.Button1Click(Sender: TObject);
begin
  ADOQuery1.Filtered:= False;
  ADOQuery1.Filter:= Edit1.Text;
  ADOQuery1.Filtered:= True;
end;

{ Preview the report }
procedure TfrmURLReport.Button2Click(Sender: TObject);
begin
  RMReport1.LoadFromFile(ChangeFileExt(Application.ExeName, '.rmf'));
  RMReport1.ShowReport;
end;

{ Design the report }
procedure TfrmURLReport.Button3Click(Sender: TObject);
begin
  RMReport1.LoadFromFile(ChangeFileExt(Application.ExeName, '.rmf'));
  RMReport1.DesignReport;
end;

{ Download the specified photo and show on the report }
procedure TfrmURLReport.RMReport1BeforePrint(Memo: TStrings;
  View: TRMReportView);
const
  URL = 'http://infoserver/gkpic/gkpic/Ext/';
var
  picPhoto: TRMPictureView;
  FileName: String;
begin
  picPhoto:= (RMReport1.FindObject('picPhoto') as TRMPictureView);
  if picPhoto <> nil then
  begin
    FileName:= RMReport1.Dataset.GetFieldValue('Photo');        
    StatusBar1.SimpleText:= 'Downloading photos...';
    StatusBar1.Update;
    DownloadFile(URL + FileName, PhotoPath + FileName);
    if FileExists(PhotoPath + FileName) then
      picPhoto.Picture.LoadFromFile(PhotoPath + FileName)
    else
      picPhoto.Picture.Graphic:= nil;
  end;
end;

{ Get the temporary folder to save the photos first }
procedure TfrmURLReport.FormCreate(Sender: TObject);
begin
  PhotoPath:= GetTempPathName;
  //PhotoPath:= ExtractFilePath(Application.ExeName);
end;

end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值