0

I found this example and tried to load an image from my localhost:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TMemoryStream *strm = new TMemoryStream;
    IdHTTP1->Get(L"http://192.168.15.10/server/ima.png", strm);
    strm->Position = 0;
    Image1->Bitmap->LoadFromStream(strm);
}

But I´m getting this error:

Project Project1.exe raised exception class EBitmapLoadingFailed with message 'Loading bitmap failed.'

The image (ima.png) is in the correct place, and it opens perfectly when I load it manually in a TImage in the MultiResBitmap options. And it opens perfectly when using Image1->Bitmap->LoadFromFile("c:\\server\\ima.png");

I tried to get other images from the Internet, but get the same error. In fact, I need to load PNG and JPG files from my webserver (online) at runtime using Firemonkey on Android and iOS.

I'm using C++Builder 10.2.1 Tokyo, and would like a C++Builder example. No Delphi please!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • png is not bmp ... you need a png loader then convert it to bitmap ... [here](https://stackoverflow.com/a/37340970/2521214) is how my picture loaders usually look like – Spektre Nov 21 '17 at 15:56
  • Hi @Spektre. Thanks for your reply. I saw the function from your link. I have a question. Does it work in FMX (C++Builder 10.2)? I tried to use it and got the mesage error `E2209 Unable to open include file 'graphics.h'` and `E2316 'HandleType' is not a member of 'TBitmap'`. – Leo Priori Nov 21 '17 at 17:01
  • I do not use FMX as I am stuck with BDS2006 VCL ... there the `Graphics::TBitmap` is part of VCL you should not include `graphics.h` for it at all it should be already available (it is the same as your `Image1->Bitmap` I guess). The `TPNGObject` is in external library and you need to [download](http://proger.i-forge.net/Компьютер/Delphi/[20120225]%20Useful%20Delphi%20packages/) it first , the `TJPEGImage` is in `jpeg.hpp`. See [Display an array of color in C](https://stackoverflow.com/a/21699076/2521214) on how to use the bitmap from VCL properly – Spektre Nov 21 '17 at 19:11
  • @Spektre: what you say applies to VCL only. The question is about FireMonkey instead. FMX's TBitmap can handle multiple image formats, including JPG and PNG. See [the documentation](http://docwiki.embarcadero.com/Libraries/en/FMX.Graphics.TBitmapCodecManager) for the supported formats on each platform. – Remy Lebeau Nov 21 '17 at 21:20
  • @LeoPriori: in FMX, `TBitmap.LoadFromFile()` for a PNG file, and `TBitmap.LoadFromStream()` for a PNG stream, should both call into the same `TCustomBitmapCodec`-derived class, so the fact that `LoadFromFile("ima.png")` works but `LoadFromStream(strm)` fails for the (supposedly) same data is suspicious. Have you tried saving the `TMemoryStream` to a temp file to make sure it is actually a valid PNG and matches `ima.png` exactly? What kind of webserver are using to send the PNG to `TIdHTTP`? What does that code look like? – Remy Lebeau Nov 21 '17 at 21:30
  • @RemyLebeau good to know ... it was the first thing that come into my mind – Spektre Nov 21 '17 at 21:37
  • @LeoPriori: the code you have shown is fine, so I have to suspect the `TMemoryStream` data is not what you are expecting. – Remy Lebeau Nov 21 '17 at 21:40
  • Hi Lord Lebeau! You guided me to the solution. I made a boo-boo!!! My WAMPSERVER has a DocumentRoot property (file: httpd-vhosts.conf) that was set the folder (/server) already. So there is no need to include the folder in path. So the correct way is: `IdHTTP1->Get(L"http://192.168.15.10/ima.png", strm);` Thankyou Spektre and @RemyLebeau. Should I delete or edit this question? `(Leo Priori: "Oh, gosh, golly, I made a boo-boo and I gave it to Remy Lebeau." -- Remy Lebeau: "Great, another fainter".)` – Leo Priori Nov 22 '17 at 03:15
  • @LeoPriori: In that case, your webserver has a bug. If you request a URL to a file that doesn't exist (because the path was wrong, etc), the webserver should have reported an HTTP 404 error, which would have caused `TIdHTTP` to throw an exception. Also, consider setting `IdHTTP1->Request->Accept = "image/jpeg, image/png";` to let the webserver know that those are the only media types you are willing to accept in a response. If the webserver tries to send anything you don't accept, it should reply with an HTTP 406 error, which would also cause `TIdHTTP` to throw an exception. – Remy Lebeau Nov 22 '17 at 03:41

1 Answers1

0

Add this :

#include <FMX.Platform.hpp>
#include <FMX.MediaLibrary.hpp>
#include <System.Messaging.hpp>