-
January 31st, 2007, 11:42 AM
#1
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
-
January 31st, 2007, 12:20 PM
#2
Re: Convert this function to load from Resource rather than File
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.
-
January 31st, 2007, 12:52 PM
#3
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
-
January 31st, 2007, 04:05 PM
#4
Re: Convert this function to load from Resource rather than File
-
February 1st, 2007, 04:16 AM
#5
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
-
February 2nd, 2007, 02:39 PM
#6
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?
-
February 2nd, 2007, 04:06 PM
#7
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
-
February 3rd, 2007, 05:35 AM
#8
Re: Convert this function to load from Resource rather than File
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|