Link to home
Start Free TrialLog in
Avatar of geocoins-software
geocoins-software

asked on

Get JPG from url

I need to get a  JPG from url  (Example:  www.test.com/test.jpg) and place it in a TJPeg Object (without first saving it to file). Is this possible?

Is so, can you provide me the code please?

Thanks
Avatar of geocoins-software
geocoins-software

ASKER

NOTE: Must handle JPEGs that do not actually exist.  (possibly returning a nil object - OR - a boolean)

thanks
ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops, this should fix it for non existing images.
function GetInetFile(const FileURL: String; OutStream: TMemoryStream): Boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: Array[1..BufferSize] Of Byte;
  BufferLen: DWORD;
  FileSize: PDWORD;
  Reserved: DWORD;
  sAppName: String;
begin
  Result := false;
  sAppName := ExtractFileName(Application.ExeName);
  hSession := InternetOpen(PChar(sAppName),INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
  Try
    hURL := InternetOpenURL(hSession,PChar(fileURL),nil,0,INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP Or INTERNET_FLAG_NO_AUTO_REDIRECT,0);
    Try
      BufferLen := 4;
      OutStream.Position := 0;
      Repeat
        FillChar(Buffer,SizeOf(Buffer),0);
        InternetReadFile(hURL,@Buffer,SizeOf(Buffer),BufferLen);
        OutStream.Write(Buffer,BufferLen);
      Until BufferLen = 0;
      If OutStream.Size = 0 Then Exit;
      Result := True;
    Finally
      InternetCloseHandle(hURL);
    end
  Finally
    InternetCloseHandle(hSession);
  end
end;

Open in new window