delphi 获取网络图片

转自: http://www.delphitop.com/html/wangluo/43.html

代码文件:
--------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
IdHTTP1: TIdHTTP;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses jpeg;

procedure TForm1.Button1Click(Sender: TObject);
const
url = 'http://ptlogin2.qq.com/getimage?aid=15000701&0.9129723031485226';
var
ms: TMemoryStream;
jpg: TJPEGImage;
begin
ms := TMemoryStream.Create;
jpg := TJPEGImage.Create;

IdHTTP1.Get(url, ms);
ms.Position := 0;

jpg.LoadFromStream(ms);
Canvas.Draw(10, 10, jpg);

jpg.Free;
ms.Free;
end;

【补充】 
提示:如出现“socket error # 10054”错误,可以参考  《idhttp的socketerror # 10054 错误的处理办法》 
在 Get 方法先做一次初始化,如下:

IdHTTP1.Disconnect;
IdHTTP1.Free;
IdHTTP1:=TIdHTTP.Create(nil);


end. 

你可能感兴趣的:(delphi 获取网络图片)