Show jpg, gif, bmp file from internet

Hi.

I´m using the following code to get jpegs from the internet....I want to be able to get gif, bmp and so on also. A bonus would be if I can get the icon of a file that is not a image also.

Anyone who can help??



function GetInetFile(const aHURL, AAppName: String): String;
const
  BufferSize = 1024;
var
  Buffer: String;
  BufferLen: DWORD;
  hSession, hURL: HInternet;
  bytesRead: DWORD;
  s: String;
begin
  Result:='';
  hSession := InternetOpen(PChar(AAppName),
                           INTERNET_OPEN_TYPE_PRECONFIG,
                           nil, nil, 0);
  try
    hURL := InternetOpenURL(hSession, PChar(aHURL), nil,0,0,0);
    try
      s := '';
      repeat
        Buffer := '';
        SetLength(Buffer, BufferSize);
        InternetReadFile(hURL, PChar(Buffer), BufferSize, BufferLen);
        SetLength(Buffer, BufferLen);
        if (BufferLen <> 0) then
          s := s + Buffer;
      until (BufferLen = 0);
      Result := s;
    finally
      InternetCloseHandle(hURL)
    end;
  finally
      InternetCloseHandle(hSession)
  end;
end;

procedure TForm1.GetJpeg( aImage: TImage; Url: String );
var
  Stream: TStringStream;
  Image: TJpegImage;
  s: string;
  i: Integer;
begin
  s := GetInetFile( Url, ExtractFileName(Application.ExeName) );
  Stream := TStringStream.Create(s);
  Image := TJpegImage.create;
  Image.LoadFromStream(Stream);
  aImage.picture.Assign(Image);
  Image.Free;
  Stream.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetJpeg(Image1, 'http://www.yadayada.com/image.jpg');
end;
FixzyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
hello  Fixzy , , you can already do bitmaps, since TBitmap is in the Graphics unit,
I am wondering how to help you, there are several delphi components that would allow you handle a .GIF file, have you looked at Torry's or other component download sites?, What kind of help are you needing here, a list of components for different image file types (gif, png) or other kind of help?

I guess you want some code for this -
"if I can get the icon of a file that is not a image"
do you mean a program file (.EXE) ? But I do not see how this relates to the GIF image download, or is this a separate thing?
0
FixzyAuthor Commented:
Hi.

If I want to get bmp files or others...what do I have to change in this code (I cant figure it out)?

GetJpeg(Image1, 'http://www.yadayada.com/image.jpg');

I would like to write something like this....If it is not a picture that can be shown It puts the icon of the file into the TImage.

GetJpeg(Image1, 'http://www.yadayada.com/'+listbox1.items[i]);

Hope u understand what I mean.
/Fixzy
0
Slick812Commented:
here is some code from my head, it is untested, but it should give you some Idea of how to test and then load a different type of image, according to the file extention of the file name


procedure Form1.GetJpeg(aImage: TImage; const Url: String);
const
aryFileExt: Array[0..3] of String = ('.BMP','.JPG','.JPEG','.GIF');

var
  Stream: TStringStream;
  Bmp1: TBitmap;
  Jpg1: TJpegImage;
  Gif1: TGifImage;
  s, Ext1: string;
  i: Integer;
begin
  if Length(Url) < 6 then
    begin
    ShowMessage('ERROR - Url is incorrect');
    Exit;
    end;

  s := GetInetFile( Url, ExtractFileName(Application.ExeName) );
  if Length(s) < 10 then Exit;

  Stream := TStringStream.Create(s);
 
  Ext1 := UpperCase(ExtractFileExt(Url));
  for i := 0 to High(aryFileExt) do
    if Ext1 = aryFileExt[i] then
      begin
      case i of
        0: begin
          Bmp1 := TBitmap.Create;
          Bmp1.LoadFromStream(Stream);
          aImage.picture.Assign(Bmp1);
          FreeAndNil(Bmp1);
          end;
        1..2: begin
          Jpg1 := TJpegImage.Create;
          Jpg1.LoadFromStream(Stream);
          aImage.picture.Assign(Jpg1);
          FreeAndNil(Jpg1);
          end;
        3: begin
          Gif1 := TGifImage.Create;
          Gif1.LoadFromStream(Stream);
          aImage.picture.Assign(Gif1);
          FreeAndNil(Gif1);
          end;
        end;
      end;

  FreeAndNil(Stream);
end;

 - - - - - - - -  -- - - - -  - - - - -  - - - - - -  - - -
I am still not sure about the Icon question, you may should just display a small  "ERROR" bitmap or Icon instead of an Icon, that I guess is from the current system, but if it is an Unknown file image type, there may not be an Icon
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FixzyAuthor Commented:
I´ll try the code and let u know how I´m doing.

Regarding the icons....I would like to be able to use the icons that windows uses for eg. txt, pdf, asp and so on when the file is not a Gif, Jpeg, Bmp....but it seems hard to do that when it comes to files on the internet...(I know how to do this when I am working with files on the computer though)

Of course I could make a default "ERROR" icon or something if the extension is not GIF, JPEG, BMP and so on...

Any clearer?

/Fixzy
0
FixzyAuthor Commented:
Your code worked Slick812....thanx.

I crcked the thinggy with the icons from internet....it was pretty easy when I put my mind to it.

/Fixzy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.