Show photos of a specified URL in a report

Source: Internet
Author: User
Show photos of a specified URL in a report

The following source code shows how to display a photo with the specified file name in the reportmachine report and download the photo from the specified URL. The principle is that, before the report is displayed (onbeforeprint event), find the trmpictureview control to display the photo, and then click Export hoto? #23383; Segment to get the file, download the photo (JPG file) of the file name from the specified URL to the temporary folder, and then display the photo in the Temporary Folder to the trmpictureview control.

Note that you must add a reference to the urlmon unit in the uses clause. When creating a window, you can get a temporary file. Of course, it can be another specific file, but the accumulated photos in each report preview will occupy a lot of disk space.

The main code is in the onbeforeprint event of rmreport1. First, find the trmpictureview control to be displayed. This control is added when designing the reportmachine report and is assigned a name (icphoto here? #65289; the name in the Code must correspond to the name in the report, otherwise it will never be found. After finding the control, read the field that saves the name of the photo file (which is stored hoto? #65289;, and then add the specified URL (defined as a constant URL here) with the file name as the complete download URL, which must be a valid URL, you can specify the corresponding virtual directory on the server and test whether the directory is valid in IE. Download the file to the temporary directory with the same name, and then display the file in the picphoto control.

This program has been tested in Windows2000 Server + DELPHI6 + reportmachine3.0. Known BUG: the response to invalid download URL becomes slow, and the return result of the downloadfile () function is always true.

unit u_frmURLReport;interfaceuses  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;implementationuses 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.