0

Possible Duplicate:
How to get images from URL in Delphi

I'm using Delphi 2010,with some functions i can download and show JPG or PNG pictures in cximage or image object in a form, but i'm trying to do without download the picture to hard drive,i was wondering is that possible ? I searched via google & stackoverflow but couldn't find, all samples are about the download the file.

For example,i added an cximage1 to my form and want to show an picture from an url like "http://www.someaddress.com/pictures/001.JPG".

Thanks for all helps,and sorry for bad English :)

Community
  • 1
  • 1
Sheshman
  • 88
  • 3
  • 10
  • Standard "Delphi" (e.g. Delphi 2010, or RAD Developer XE) is one thing - it comes with a very rich set of libraries and controls. The ["cximage"](http://www.xdp.it/cximage/) C++ library is something different. And the DevExpress TcxImage control is yet a third thing. Q: What are you referring to? Q: Have you looked at the documention? – paulsm4 Jun 16 '12 at 23:43

1 Answers1

3

You can use this function (compiled and tested using Delphi 2010) to download a file over HTTP and store it in a TBitMap. It'll read all file types that have a registered support in Delphi's TGraphic hiearachy, and will auto-detect BMP, GIF, JPG and PNG file formats:

USES Graphics, IdHTTP, PngImage, jpeg, GIFImg;

FUNCTION DownloadImage(CONST URL : STRING ; ImageType : TGraphicClass = NIL) : TBitMap;
  VAR
    HTTP        : TIdHttp;
    S           : TStream;
    IMG         : TGraphic;
    STR         : AnsiString;

  BEGIN
    HTTP:=TIdHttp.Create(NIL);
    TRY
      S:=TMemoryStream.Create;
      TRY
        HTTP.Get(URL,S);
        IF NOT Assigned(ImageType) THEN BEGIN
          S.Position:=0;
          SetLength(STR,5);
          S.Read(STR[1],LENGTH(STR));
          IF COPY(STR,1,2)='BM' THEN
            ImageType:=TBitMap
          ELSE IF COPY(STR,1,3)='GIF' THEN
            ImageType:=TGIFImage
          ELSE IF COPY(STR,2,3)='PNG' THEN
            ImageType:=TPngImage
          ELSE IF (ORD(STR[1])=$FF) AND (ORD(STR[2])=$D8) THEN
            ImageType:=TJPEGImage
        END;
        IF NOT Assigned(ImageType) THEN RAISE EInvalidImage.Create('Unrecognized file format!');
        IMG:=ImageType.Create;
        TRY
          S.Position:=0;
          IMG.LoadFromStream(S);
          Result:=TBitMap.Create;
          TRY
            Result.Assign(IMG)
          EXCEPT
            Result.Free;
            RAISE
          END
        FINALLY
          IMG.Free
        END
      FINALLY
        S.Free
      END
    FINALLY
      HTTP.Free
    END
  END;

If you already know the file type, you can specify it as the 2nd parameter as either TGifImage, TPngImage, TJPegImage or TBitMap. If not, the routine will try to auto-detect it among these four types. If you use a custom-graphic type, you'll need to specify it as the 2nd parameter or update the auto-detection logic to detect it.

So, if you want to use it to display a downloaded image in a TImage without saving it to disc, you can use it thus:

  VAR
    BMP : TBitMap;

  BEGIN
    BMP:=DownloadImage('http://domain.com/image.jpg');
    TRY
      Image1.Picture.Assign(BMP)
    FINALLY
      BMP.Free
    END
  END;
HeartWare
  • 7,464
  • 2
  • 26
  • 30
  • 3
    Why does DownloadImage return a TBitmap? Why not skip that step and return the TGraphic IMG. That conversion step seems rather needless at least for this question. – David Heffernan Jun 17 '12 at 07:58
  • Thank you for the function i'll try and will let you know ASAP,so i'm trying to avoid save file to disc because most of the testers using windows 7 and unfortunatelly UAC restricted to write to most of folders,that's why i'm trying to show pictures from internet without download to disc. I'm also tryed the UAC manifest and res files it's working for "Run as administrator" but somehow i still can't write to program's own folder. – Sheshman Jun 17 '12 at 10:29
  • thanks for all help,i tryed the function and it works perfectly, i would like to thank you again for help. – Sheshman Jun 17 '12 at 10:44
  • 2
    +1 The capitals give some sort of nostalgic turbo pascal feel to the code. :) – Wouter van Nifterick Jun 17 '12 at 10:46
  • 1
    @Wouter Surely you mean Modula-2 or similar. TP was insensitive to case. – David Heffernan Jun 17 '12 at 14:56
  • Remember to accept the answer if the code solves your problem. – HeartWare Jun 17 '12 at 15:03
  • @DavidHeffernan: No particular reason. I started by defining the interface (to return a TBitMap), and could have changed this to TGraphic instead. I'll leave that exercise up to the reader :). – HeartWare Jun 17 '12 at 15:05
  • @David, no i actually meant TP. I started programming with Turbo Pascal 5.5, and I remember that it didn't have syntax highlighting, and I used capitals as a poor-man's syntax highlighting. – Wouter van Nifterick Jun 18 '12 at 01:45