Thread: JPEG from resource with GDI+

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    29

    JPEG from resource with GDI+

    I am trying to show a jpeg image from a resource with GDI+.

    I've made a JPEG resource called IDR_JPEG1 in VC++ 7.1 with the type "JPEG".

    I've used this code:
    Code:
    // Graphics declaration above...
    LPSTREAM stream; 
    HGLOBAL imageMem = LoadResource(GetModuleHandle(0), FindResource(GetModuleHandle(0), MAKEINTRESOURCE(IDR_JPEG1), "JPEG"));
    CreateStreamOnHGlobal(imageMem, false, &stream);
    
    Image image(stream);
    graphics.DrawImage(&image, 0, 0, 50, 50);
    It compiles but no image is shown.
    But when i do
    Code:
    Image image(L"res/generals.jpg");
    graphics.DrawImage(&image, 0, 0, 50, 50);
    the image is shown...

    What is wrong ?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I don't know what class Image is, but obviously it has a constructor that takes a filename (string) but no constructor that takes an LPSTREAM (prolly an implicit conversion takes place). Successful compilation does not mean invincibility, you should check for error messages.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    29
    typedef IStream *LPSTREAM;
    Image::Image(IStream*,BOOL);

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    1
    I' having roughly the same problem. I investigated some further and noticed that DrawImage returns status 3 (OutOfMemory), but this is not the case.

    Mazdak, did you check the last error code after doing: "Image image(stream);" to see whether the stream was correctly loaded?

    Anyone some ideas?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As stated in the documentation LoadResource does not return an actual HGLOBAL. Therefore, its return value can not be used with CreateStreamOnHGlobal.

    See CodeProject: Loading JPG & PNG resources using GDI+ for sample code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 07:12 PM
  2. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 01:07 AM
  3. Generic Resource_Manager WIP with lots TODO
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2006, 01:55 AM
  4. JPEG Resource
    By dragunsflame in forum Windows Programming
    Replies: 2
    Last Post: 04-08-2003, 10:07 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM