DELPHI使用IDHTTP显示网络图片

DELPHI使用IDHTTP显示网络图片:

 


程序代码:

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls, StdCtrls, IdBaseComponent, IdComponent,

  GifImage, Jpeg, IdTCPConnection, IdTCPClient, IdHTTP;

 

type

  TForm1 = class(TForm)

    Edit1: TEdit;

    Button1: TButton;

    Image1: TImage;

    IdHTTP1: TIdHTTP;

    Label1: TLabel;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);

var

imagestream:TMemoryStream;

Buffer:Word;

jpg:TjpegImage;

gif:TgifImage;

begin

    image1.Picture.Graphic:=nil  ;

    imagestream := TMemoryStream.Create();

    try

      IdHTTP1.Request.Accept :=  '*/*';

      IdHTTP1.Request.AcceptLanguage := 'zh-cn';

      IdHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';

      IdHTTP1.Request.Connection := 'Keep-Alive';

      IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];

      IdHTTP1.ProtocolVersion:=pv1_1;

 

       try

         idhttp1.Get(Edit1.Text,imagestream);

       except

         showmessage('连接失败!');

         exit;

       end;

 

       imagestream.Position:=0;

       if imagestream.Size = 0 then

        begin

           imagestream.Free;

           ShowMessage('错误!');

           exit;

        end;

 

       imagestream.ReadBuffer(Buffer,2);

       imagestream.Position:=0;

 

         if Buffer=$4D42 then //bmp

         begin

           image1.Picture.Bitmap.LoadFromStream(imagestream);

         end

         else if Buffer=$D8FF then //jpg

         begin

           jpg:=TjpegImage.Create;

           jpg.LoadFromStream(imagestream);

           image1.Picture.Assign(jpg);

           jpg.Free;

         end

         else if Buffer=$4947 then  //gif

         begin

           gif:=TGifImage.Create;

           gif.LoadFromStream(imagestream);

           image1.Picture.Assign(gif);

           gif.Free;

         end

         else if Buffer=$050A then

         begin

           ShowMessage('PCX');

         end

         else if Buffer=$5089 then

         begin

           ShowMessage('PNG');

         end

         else if Buffer=$4238 then

         begin

            ShowMessage('PSD');

         end

         else if Buffer=$A659 then

         begin

           ShowMessage('RAS');

         end

         else if Buffer=$DA01 then

         begin

           ShowMessage('SGI');

         end

         else if Buffer=$4949 then

         begin

           ShowMessage('TIFF');

         end

         else  

         begin

           ShowMessage('ERROR');

         end;   

 

    finally

       imagestream.Free;

    end;

 

 

end;

 

end.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值