CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Unhappy Convert this function to load from Resource rather than File

    Hello gurus,

    You can see following function (I got from codeproject as far I remember), loads from an external file. What I want is have same fucntion load from resources, I pass it resource ID as DWORD and it simply loads from resources not an external file. something like
    HBITMAP LoadImageResource(DWORD resID);


    I am sure for winapi gurus it is task of a few minutes. Any help is apprecited.

    Please, it is urgent:

    Code:
    HBITMAP LoadImageFile(char *FileName)  {
       // Use IPicture stuff to use JPG / GIF / BMP files
       IPicture* p;
       IStream* s;
       IPersistStream* ps;
       HGLOBAL hG;
       void* pp;
       FILE* fp;
    
    
       // Read file in memory
       fp = fopen(FileName,"rb");
       if (!fp)
    	  return NULL;
    	
    
       fseek(fp,0,SEEK_END);
       
       int fs = ftell(fp); //get filesize
       fseek(fp,0,SEEK_SET);
       hG = GlobalAlloc(GPTR,fs);
       if (!hG)  {
          fclose(fp);
          return NULL;
       }
    
    
          
       pp = (void*)hG;
       fread(pp,1,fs,fp);
       fclose(fp);
    	
    
       // Create an IStream so IPicture can
        CreateStreamOnHGlobal(hG,false,&s);
       if (!s) {
          GlobalFree(hG);
          return NULL;
       }
    
    
       OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
    
       if (!p) {
          s->Release();
          GlobalFree(hG);
          return NULL;
       }
       s->Release();
       GlobalFree(hG);
    
       HBITMAP hB = 0;
       p->get_Handle((unsigned int*)&hB);
    
       // Copy the image. Necessary, because upon p's release,
       // the handle is destroyed.
       HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG);
    
       p->Release();
       return hBB;
    }

    I do not want to use LoadBitmap or LoadImage in this regard, please. If possible, I want exactly same function but reading resources not external file.

    waiting for kind quick reply.


    regards
    Last edited by Ali Imran; January 31st, 2007 at 11:46 AM. Reason: fix typos
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Convert this function to load from Resource rather than File

    Quote Originally Posted by Ali Imran
    I do not want to use LoadBitmap or LoadImage in this regard, please.
    I do not quite understand why those won't work for you when they are written for exactly the purpose you have in mind.

  3. #3
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: Convert this function to load from Resource rather than File

    I do not quite understand why those won't work for you when they are written for exactly the purpose you have in mind.
    becuase none of these functions load JPEG or GIF.

    Anyway ? I can get this function done please

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  4. #4
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: Convert this function to load from Resource rather than File


  5. #5
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: Convert this function to load from Resource rather than File

    1. it reports error as unknown identifier 'hBmp' on following line:

    HBITMAP bitmap = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);

    2. Would this function work if I discard the second argument, and pass RT_RCDATA as resource type?

    3. Am I allowed to use it anyway I want ?

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  6. #6
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: Convert this function to load from Resource rather than File

    Sorry for the late answer.

    1) Yes, i just corrected the code.

    2) the second argument depends on how you include the file in the .rc file. FindResource requires the data type - after all, even LoadImage (which IMHO uses FindResource) requires the image type (IMAGE_ICON, IMAGE_BITMAP or IMAGE_CURSOR). So, if you use JPEG or GIF or PNG or whatever as type when you declare in the resource, that's what you should use when loading the image. For example:
    Code:
    //in the .rc file
    ID_JPEG_IMAGE JPEG "image.jpg" //this will require _T("JPEG") as type
    ID_JPEG_IMAGE GIF "image.gif" //this will require _T("GIF") as type
    You can still try to use RT_RCDATA. This function was coded long ago, and i don't remember why i asked to pass the resource type..

    3) Well, where do you want to use it?

  7. #7
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: Convert this function to load from Resource rather than File

    1. thanks

    2. tried RT_RCDATA but does not work

    3. For free GUI library RAD C++ 1.2.

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  8. #8
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: Convert this function to load from Resource rather than File

    Quote Originally Posted by Ali Imran
    3. For free GUI library RAD C++ 1.2.
    No problem. If you want, put a link to the page in the "credits".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured