Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading an image from a .rc file  (Read 14736 times)

0 Members and 2 Guests are viewing this topic.

Prominence256

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • http://GameMakerStation.110mb.com/
Loading an image from a .rc file
« on: January 27, 2011, 03:26:45 am »
I'm trying to upgrade my screensaver from using Shapes to using actual images. I have it working and everything, but I was wondering if there was a way to set an sf::Image from an image saved in a .rc file, that way my screensaver's just one file.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loading an image from a .rc file
« Reply #1 on: January 27, 2011, 07:38:10 am »
Get a pointer to the contents of your resource and use LoadFromMemory.
Laurent Gomila - SFML developer

Prominence256

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • http://GameMakerStation.110mb.com/
Loading an image from a .rc file
« Reply #2 on: January 27, 2011, 11:03:30 pm »
Do you have an example of this? This is what I tried and it didn't work:
Code: [Select]
Image zero;
HBITMAP img = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP0));
zero.LoadFromMemory((const char*) img, sizeof(img));

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loading an image from a .rc file
« Reply #3 on: January 27, 2011, 11:16:17 pm »
I don't know how to do, but it's a little bit more complicated than this. Read carefully the MSDN doc and tutorials.
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Loading an image from a .rc file
« Reply #4 on: January 27, 2011, 11:49:31 pm »
my guess is that the "sizeof" is wrong. The BITMAP variable is more or less a pointer/reference to the data. Just like if you would use sizeof(sf::Image) you would not get the amount of bytes the image data consist of but you would get the byte size of the instance sf::Image.

You'll have to acquire the size somehow else.

Never used BITMAP myself before, just an educated guess ^^
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Loading an image from a .rc file
« Reply #5 on: January 28, 2011, 01:59:54 am »
Bitmaps are stored in memory like this under Windows.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Loading an image from a .rc file
« Reply #6 on: January 29, 2011, 11:16:02 pm »
Acquiring a pointer to resource data is a multi-step process.

1)  FindResource to find it
2)  LoadReourse to get a handle to it
3)  LockResource to get the data pointer


After that I'm not sure how you free the resource as there doesn't seem to be an UnlockResource function.  You can search MSDN for details on all these functions.

Prominence256

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • http://GameMakerStation.110mb.com/
Loading an image from a .rc file
« Reply #7 on: January 30, 2011, 02:10:36 am »
Thanks, and according to MSDN LockResource doens't actually lock anything as of XP.

But now my issue is what to put for the SizeInBytes in LoadFromMemory.

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Loading an image from a .rc file
« Reply #8 on: January 30, 2011, 02:31:32 am »
Quote from: "Prominence256"
Thanks, and according to MSDN LockResource doens't actually lock anything as of XP.

But now my issue is what to put for the SizeInBytes in LoadFromMemory.

Width * Height *4

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Loading an image from a .rc file
« Reply #9 on: January 30, 2011, 03:27:16 am »
Quote from: "JAssange"

Width * Height *4


No, he needs the size of the bitmap/whatever file.

To get the size of a resource,use SizeofResource:

http://msdn.microsoft.com/en-us/library/ms648048%28v=vs.85%29.aspx

Prominence256

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • http://GameMakerStation.110mb.com/
Loading an image from a .rc file
« Reply #10 on: January 30, 2011, 03:56:19 am »
I tried all of this together and the image showed up as a little white dot on the screen, also SFML printed this to the console:
Quote from: "SFML"
Failed to load image from memory. Reason : Image not of any known type, or corrupt

This was my code:
Code: [Select]
Image zeroimg;
HRSRC hresource = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1), RT_BITMAP);
HGLOBAL hglobal = LoadResource(GetModuleHandle(NULL), hresource);
LPVOID lpvoid = LockResource(hglobal);
zeroimg.LoadFromMemory((const char*) lpvoid, SizeofResource(GetModuleHandle(NULL), hresource));

Here's a link to the picture, I couldn't find an image host that doesn't convert the image to a png.

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Loading an image from a .rc file
« Reply #11 on: January 30, 2011, 04:05:30 am »
Quote from: "Disch"
Quote from: "JAssange"

Width * Height *4


No, he needs the size of the bitmap/whatever file.

To get the size of a resource,use SizeofResource:

http://msdn.microsoft.com/en-us/library/ms648048%28v=vs.85%29.aspx

But he said LoadFromMemory so I assume he means the SFML function, which only takes the pixels in ARGB format, which would be w*h*4.

Prominence256

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • http://GameMakerStation.110mb.com/
Loading an image from a .rc file
« Reply #12 on: January 30, 2011, 04:12:11 am »
I did mean the SFML function, but I get the same result with both width*height*4 and SizeofResource().

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Loading an image from a .rc file
« Reply #13 on: January 30, 2011, 05:25:27 am »
Quote from: "Prominence256"
I did mean the SFML function, but I get the same result with both width*height*4 and SizeofResource().

Then the pointer isn't to ARGB formatted pixel data. You can't just take the handle of the Bitmap resource, it has a DIB header and a color table, its a completely different format. You need to parse it into an array of ARGB bytes.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loading an image from a .rc file
« Reply #14 on: January 30, 2011, 10:16:56 am »
LoadFromMemory takes a pointer to a bitmap file in memory (the same thing that you would have on your hard drive).

LoadFromPixels takes a pointer to an array of RGBA pixels.

Use the one that suits what your resource is ;)
Laurent Gomila - SFML developer