用Delphi编写CGI程序返回图象_Delphi

  • 内容
  • 评论
  • 相关

Delphi 对Internet编程提供了强大的支持,Delphi 编写CGI非常容易,Dlphi 本身提供了例程。但是笔者在实践中遇到需要CGI程序根据数据库动态 返回图象的情况,很多文章推进使用Cgi-Expert或有关的OCX控件,使用控件不仅 带来额外的开销,且缺乏灵活性,那么能否不使用这些控件自己编写代码实现,现 推荐一种方法,代码如下:  

unit ChartMod;
  
interface
  
uses
  Windows, Messages, SysUtils, Classes,
HTTPApp, Db, DBTables,
  DbChart, Series, Jpeg, ExtCtrls;

http://www.gaodaima.com/65628.html用Delphi编写CGI程序返回图象_Delphi

  
type
  TwebModule1 = class(TWebModule)
    Table1: TTable;
procedure WebModule1ActionAreaAction
(Sender: TObject;
      Request: TWebRequest; Response:  
TWebResponse; var Handled:
Boolean);
procedure WebModule1ActionPopulationAction
(Sender: TObject;
      Request: TWebRequest; Response:  
TWebResponse; var Handled:
Boolean);
    procedure WebModule1Create(Sender: TObject);
    procedure WebModule1Destroy(Sender: TObject);
    procedure WebModule1AfterDispatch(Sender: TObject;
      Request: TWebRequest; Response:  
TWebResponse; var Handled: Boolean);
  private
    Chart: TDBChart;
    Series: TPieSeries;
    Image: TImage;
  public
    { Public declarations }
  end;
  
var
  WebModule1: TWebModule1;
  
implementation
  
{$R *.DFM}
  
procedure TWebModule1.WebModule1ActionAreaAction(
Sender: TObject;
  Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean);

begin
  // set specific values
  Chart.Title.Text.Clear;
  Chart.Title.Text.Add ('Area of Countries');
  Chart.LeftAxis.Title.Caption := 'Area';
  Series.Title := 'Area';
  Series.PieValues.ValueSource := 'Area';
end;
  
procedure TWebModule1.WebModule1ActionPopulationAction(
Sender: TObject;
  Request: TWebRequest; Response: TWebResponse;  
var Handled: Boolean);

begin
  // set specific values
  Chart.Title.Text.Clear;
  Chart.Title.Text.Add ('Population of Countries');
  Chart.LeftAxis.Title.Caption := 'Population';
  Series.Title := 'Population';
  Series.PieValues.ValueSource := 'Population';
end;
  
procedure TWebModule1.WebModule1Create(Sender: TObject);
begin
  // open the database table
  Table1.Open;
  // create the chart
  Chart := TDBChart.Create (nil);
  Chart.Width := 600;
  Chart.Height := 400;
  Chart.AxisVisible := False;
  Chart.Legend.Visible := False;
  Chart.BottomAxis.Title.Caption := 'Name';
  // create the pie series
  Series := TPieSeries.Create (Chart);
  Series.ParentChart := Chart;
  Series.DataSource := Table1;
  Series.XLabelsSource := 'Name';
  Series.OtherSlice.Style := poBelowPercent;
  Series.OtherSlice.Text := 'Others';
  Series.OtherSlice.Value := 2;
  Chart.AddSeries (Series);
  // create the memory bitmap
  Image := TImage.Create (nil);
  Image.Width := Chart.Width;
  Image.Height := Chart.Height;
end;
  
procedure TWebModule1.WebModule1Destroy(
Sender: TObject);

begin
  Chart.Free;
  Series.Free;
  Image.Free;
end;
  
procedure TWebModule1.WebModule1AfterDispatch(
Sender: TObject;
  Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean);

var
  Jpeg: TJpegImage;
  MemStr: TMemoryStream;
begin
  // paint the chart on the memory bitmap
  Chart.Draw (Image.Canvas, Image.BoundsRect);
  // create the jpeg and copy the iamge to it
  Jpeg := TJpegImage.Create;
  try
    Jpeg.Assign (Image.Picture.Bitmap);
    MemStr := TMemoryStream.Create;
    try
      // save to a stream and return it
      Jpeg.SaveToStream (MemStr);
      MemStr.Position := 0;
      Response.ContentType := 'image/jpeg';
      Response.ContentStream := MemStr;
      Response.SendResponse;
    finally
      MemStr.Free;
    end;
  finally
    Jpeg.Free;
  end;
end;
  
end.

欢迎大家阅读《用Delphi编写CGI程序返回图象_Delphi》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

本文链接地址: 用Delphi编写CGI程序返回图象_Delphi

微信支付二维码

微信 赏一包辣条吧~

支付宝支付二维码

支付宝 赏一听可乐吧~

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注