Recent

Author Topic: How to load a picture from the Internet?  (Read 15060 times)

Vlad1997

  • New Member
  • *
  • Posts: 16
    • My animations and programs (Russian)
How to load a picture from the Internet?
« on: January 07, 2010, 07:36:18 pm »
How to load a picture from the Internet?
Formats: BMP, JPEG and GIF.
OS: Ubuntu

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: How to load a picture from the Internet?
« Reply #1 on: January 07, 2010, 08:30:33 pm »
Please, review examples and documentation of the following component: http://wiki.freepascal.org/lNet
« Last Edit: January 07, 2010, 08:34:17 pm by skalogryyz »

Vlad1997

  • New Member
  • *
  • Posts: 16
    • My animations and programs (Russian)
Re: How to load a picture from the Internet?
« Reply #2 on: January 08, 2010, 12:25:31 pm »
And how to use it?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: How to load a picture from the Internet?
« Reply #3 on: January 08, 2010, 02:53:50 pm »

Vlad1997

  • New Member
  • *
  • Posts: 16
    • My animations and programs (Russian)
Re: How to load a picture from the Internet?
« Reply #4 on: January 08, 2010, 05:27:18 pm »
Thank you :)
And how to load just picture?
Code: [Select]
function TMainForm.HTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar;
  ASize: dword): dword;
var
  oldLength: dword;
begin
  oldLength := Length(HTTPBuffer);
  setlength(HTTPBuffer,oldLength + ASize);
  move(ABuffer^,HTTPBuffer[oldLength + 1], ASize);
  MemoHTML.Text := HTTPBuffer;
  MemoHTML.SelStart := Length(HTTPBuffer);
  AppendToMemo(MemoStatus, IntToStr(ASize) + '...');
  Result := aSize; // tell the http buffer we read it all
end;

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: How to load a picture from the Internet?
« Reply #5 on: January 08, 2010, 05:35:37 pm »
Probably easier with Synapse's HttpGetBinary

http://synapse.ararat.cz/doc/help/httpsend.html#HttpGetBinary

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: How to load a picture from the Internet?
« Reply #6 on: January 08, 2010, 08:22:06 pm »
Thank you :)
And how to load just picture?
Provide Lnet component with URL of "just picture", let's say:
http://www.mysite.com/image.png

if you don't know the exact address, you'll need to load an .html file and parse it... (that's how browsers work)

After you're sure that lnet is loading the picture (not .html), you can decide what to do with the data.

The Lnet component writes the data from ABuffer to a stream... let's say TMemoryStream or TFileStream (if you need to store the picture into a file).

If you need to show the picture on the screen you can later load and show it, using Graphics unit.

Code: [Select]
uses
  ...lnet, graphics...
  
var
  pngstream: TStream;
  pngimage : TPortableNetworkGraphics;

function TMainForm.HTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar;
  ASize: dword): dword;
begin
  pngstream.Write(ABuffer^, ASize);
end;

procedure TMainForm.ShowPicture;
begin
  if not Assigned(png) then begin
    pngimage:=TPortableNetworkGraphics.Create;
    pngstream.Position:=0;
    pngimage.LoadFromStream(pngstream);
  end;
  Invalidate;
end;

procedure TMainForm.FormPaint;
begin
  if Assigned(pngimage) then
    Canvas.Draw(0,0,pngimage);
end;

It all depends on what you mean saying "how to load a picture". Lnet will help you to get the picture data, but it's up to you how to use it.

P.S. don't ask for the examples, write your own ;)
« Last Edit: January 08, 2010, 08:24:38 pm by skalogryyz »

 

TinyPortal © 2005-2018