《DELPHI使用IDHTTP显示网络图片.doc》由会员分享,可在线阅读,更多相关《DELPHI使用IDHTTP显示网络图片.doc(4页珍藏版)》请在金锄头文库上搜索。
1、DELPHI使用IDHTTP显示网络图片:程序代码:unit Unit1;interfaceuses 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; Ima
2、ge1: TImage; IdHTTP1: TIdHTTP; Label1: TLabel; procedure Button1Click(Sender: TObject); private Private declarations public Public declarations end;var Form1: TForm1;implementation$R *.dfmprocedure TForm1.Button1Click(Sender: TObject);varimagestream:TMemoryStream;Buffer:Word;jpg:TjpegImage;gif:TgifI
3、mage;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.HTTPOption
4、s:=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); image
5、stream.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.Creat
6、e; 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.运行效果: