Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get JPG from url

Posted on 2008-11-03
3
Medium Priority
?
311 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:geocoins-software
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

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

thanks
0
 
LVL 13

Accepted Solution

by:
ThievingSix earned 2000 total points
ID: 22870935
The code below should work.

Here is an example of how to use it:

procedure TForm1.Button1Click(Sender: TObject);
var
  JPEG : TJPEGImage;
  Data : TMemoryStream;
begin
  Data := TMemoryStream.Create;
  Try
    JPEG := TJPEGImage.Create;
    Try
      If Not(GetInetFile('http://www.test.com/phoenix/images/5_top3pic1.jpg',Data)) Then Exit;
      Data.Position := 0;
      JPEG.LoadFromStream(Data);
      Image1.Picture.Graphic := JPEG;
    Finally
      JPEG.Free;
    end;
  Finally
    Data.Free;
  end;
end;

Uses WinInet;
 
function GetInetFile(const FileURL: String; OutStream: TMemoryStream): Boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: Array[1..BufferSize] Of Byte;
  BufferLen: 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,0,0);
    Try
      OutStream.Position := 0;
      Repeat
        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

0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 22871116
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

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question