Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Problem reading image from resource stream


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Apr 28, 2017 2:04 PM Last Post By: Remy Lebeau (Te...
Greg Reese

Posts: 100
Registered: 7/15/05
Problem reading image from resource stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 27, 2017 1:00 PM
I'm trying to read an image from a resource stream and not succeeding. Here's the situation:

I have a FireMonkey program and a file with the image of interest, say my_image.bmp . I go to Project | Resources and Images and add the file with resource type of BITMAP and identifier my_image . I compile and link and then check the executable with Resource Hacker (a resource editor) which shows that the image is indeed stored as a resource in the executable. The code causing the problem is:

TResourceStream* p = new TResourceStream( (NativeUInt)HInstance,
	L"my_image", (wchar_t*)RT_BITMAP );
image->Bitmap->LoadFromStream( p );
delete p;


where "image" is a FireMonkey TImage* .

The TResourceStream appears to construct correctly but when the next line (with LoadFromStream) runs I get the run-time error

EBitmapLoadingFailed with message 'Loading bitmap failed.'

What's happening?

By the way, loading the image directly from the file with
image->Bitmap->LoadFromFile( p )
works fine.

I have Builder 10.2 Tokyo

Thanks.
Greg Reese
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Problem reading image from resource stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2017 10:02 AM   in response to: Greg Reese in response to: Greg Reese
Greg wrote:

TResourceStream* p = new TResourceStream( (NativeUInt)HInstance,
L"my_image", (wchar_t*)RT_BITMAP );

You don't need the type-cast on the last parameter:

TResourceStream* p = new TResourceStream( (NativeUInt)HInstance, L"my_image", RT_BITMAP);


Have you tried saving the TResourceStream to file to make sure the image data is valid?

p->SaveToFile(L"image.bmp");


--
Remy Lebeau (TeamB)
Greg Reese

Posts: 100
Registered: 7/15/05
Re: Problem reading image from resource stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2017 1:29 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Greg wrote:

TResourceStream* p = new TResourceStream( (NativeUInt)HInstance,
L"my_image", (wchar_t*)RT_BITMAP );

You don't need the type-cast on the last parameter:
_TCHAR is set to char in this project, so I do need the cast
</div>
_
<div class="jive-quote">TResourceStream* p = new TResourceStream( (NativeUInt)HInstance, L"my_image", RT_BITMAP);


Have you tried saving the TResourceStream to file to make sure the image data is valid?

p->SaveToFile(L"image.bmp");
No, good idea. I ran SaveToFile immediately after constructing the stream. It wrote out the file but then I couldn't read it with either of two different image-viewing programs!

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Problem reading image from resource stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2017 2:04 PM   in response to: Greg Reese in response to: Greg Reese
Greg wrote:

_TCHAR is set to char in this project, so I do need the cast

Then you are casting it wrong. You can't cast a char* to a wchar_t*.

RT_BITMAP is defined as MAKEINTRESOURCE(2), so you should use MAKEINTRESOURCEW(2)
instead:

TResourceStream* p = new TResourceStream( (NativeUInt)HInstance, L"my_image", 
MAKEINTRESOURCEW(2) );


You might consider creating a #define to make the code clearer:

#define RT_BITMAP_W MAKEINTRESOURCEW(2)
TResourceStream* p = new TResourceStream( (NativeUInt)HInstance, L"my_image", 
RT_BITMAP_W );


No, good idea. I ran SaveToFile immediately after constructing the
stream. It wrote out the file but then I couldn't read it with either
of two different image-viewing programs!

Then it is clearly not accessing the resource data correctly (by chance,
is p->Size returning 0 in this case?). But the fact that you are not getting
an exception is worrisome, too. Either it is accessing the wrong resource
(does SaveToFile() save anything at all?), or it is not finding the resource
and error reporting is not working correctly.

--
Remy Lebeau (TeamB)
Greg Reese

Posts: 100
Registered: 7/15/05
Re: Problem reading image from resource stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2017 2:00 PM   in response to: Greg Reese in response to: Greg Reese
Greg Reese wrote:
TResourceStream* p = new TResourceStream( (NativeUInt)HInstance,
	L"my_image", (wchar_t*)RT_BITMAP );
image->Bitmap->LoadFromStream( p );
delete p;


where "image" is a FireMonkey TImage* .

The TResourceStream appears to construct correctly but when the next line (with LoadFromStream) runs I get the run-time error

EBitmapLoadingFailed with message 'Loading bitmap failed.'

What's happening?

Well, this is interesting. If I store the image as an RCDATA resource and replace RT_BITMAP above with RT_RCDATA, the code works! Hmmm...

Greg Reese
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02