Simple way to display JPG and GIF images
King Fan
2002/11/27
I often see someone asking someone asking JPGE and GIF pictures, I just have a code to display JPEG and GIF pictures. Copy it back? Maybe you have a next time.
CShowjpegView is my view class, when you use it, you must make this will be your own class! Not a view class, as long as you can pass a CDC to give this function.
/ *
Function function: Show JPG and GIF images
Parameter Description PDC: Device Environment Object StrPath: Picture Path To display
x: The x position you want to display Y: Y position to display
* /
Bool cshowjpegview :: Showjpgif (CDC * PDC, CString Strpath, Int x, int y)
{
IStream * PSTM;
CFILESTATUS FSTATUS;
CFILE FILE;
Long Cb;
// Open the file and detect the validity of the file
IF (file.open (strpath, cfile :: moderead) &&
File.getStatus (StrPath, FStatus) &&
((cb = fStatus.m_size)! = -1)))
{
Hglobal Hglobal = GlobalAlloc (GMEM_MOVEABLE, CB);
LPVOID PVDATA = NULL;
IF (hglobal! = null)
{
Pvdata = Globalock (Hglobal);
IF (PVData! = NULL)
{
File.readhuge (PVDATA, CB);
GlobalUnlock (Hglobal);
CreatestReamonhglobal (Hglobal, True, & PSTM);
}
}
}
Else
{
Return False;
} // Open the end of the file
/ / Display images in JPEG and GIF formats, GIF can only display one frame, but also no animation,
/ / To display an animation GIF, use the Active control //.
IPicture * PPIC;
// load image from File Stream
IF (Succeeded (PSTM, FSTATUS.M_SIZE, TRUE, IID_IPICTURE, (LPVOID *) & PPIC)))))
{
OLE_XSIZE_HIMETRIC HMWIDTH;
OLE_YSIZE_HIMETRIC HMHEIGHT;
PPIC-> Get_Width (& HMWIDTH);
PPIC-> Get_Height (& hmheight);
Double fx, fy;
// Get Image Height and Width
FX = (double) PDC-> getDeviceCaps (Horzres) * (Double) HMWIDTH /
(Double) PDC-> getDeviceCaps (Horzsize) * 100.0);
Fy = (double) PDC-> getDeviceCaps (Vertres) * (double) hmheight /
((Double) PDC-> getDeviceCaps (VERTSIZE) * 100.0);
// Use render function display image
IF (Failed (PPIC-> Render (* PDC, X, Y, (DWORD) FX, (DWORD) FY, 0,
hmheight, hmwidth, -hmheight, null))))))
{
PPIC-> Release ();
Return False;
}
PPIC-> Release ();
}
Else
{
Return False;
}
Return True;
}
The following three lines of code is a way to call the function:
Cstring spath;
Spath = _t ("c: //windows//system//OOOBE //IMAGES //BGMC.JPG");
Showjpgif (PDC, SPATH, 100, 100);
If you show it in a control, use the CDC in the following way.
// Point PSTATIC to the place to display
CDC * PDC = getdlgitem (IDC_Control) -> getdc ();
The above is a simple way to show JPG and GIF images, and there is also a short while simple. Enlarge reduction function and display animation GIF files are not implemented.