?
Solved

Show jpg, gif, bmp file from internet

Posted on 2004-11-05
5
Medium Priority
?
371 Views
Last Modified: 2013-11-19
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;
0
Comment
Question by:Fixzy
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Slick812
ID: 12513889
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
 

Author Comment

by:Fixzy
ID: 12513954
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
 
LVL 35

Accepted Solution

by:
Slick812 earned 500 total points
ID: 12515489
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
 

Author Comment

by:Fixzy
ID: 12517040
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
 

Author Comment

by:Fixzy
ID: 12518051
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

580 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