看下 我的JPG图片怎么加载不进去?
#include "stdafx.h"#include "resource.h"
#include <atlbase.h>
HWND hWndMain;
long WINAPI wndproc(HWND hWnd,UINT iMessage ,UINT wParam ,LONG lParam);
BOOL initwindowsclass(HINSTANCE hInstance);
BOOL initwindows(HINSTANCE hInstance,int nCmdShow);
void DisplayImage(HDC hdc, LPCTSTR szImagePath) ;
POINT pt;
HDC hdc;
/*HDC hdcmem;
HBITMAP hbm;//位图句柄
BITMAP bm;//位图结构
RECT rt;
int x,y;
POINT Mt,Md,Ml,Mr;
COLORREF color;
int r,g,b;
int k[4],K;//用来存储上下左右四个像素点的r*/
int WINAPI WinMain(HINSTANCE hInstance ,HINSTANCE hPrevInstance ,LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if(!initwindowsclass(hInstance))
return FALSE;
if(!initwindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL initwindows(HINSTANCE hInstance ,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("w",
"test",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd) return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL initwindowsclass(HINSTANCE hInstance)
{
HBRUSH hb1;
hb1=CreateSolidBrush(RGB(0,0,0));
// hb2=CreateSolidBrush(RGB(0,100,0));
// hb3=CreateSolidBrush(RGB(0,0,100));
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=hb1;
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,(LPCTSTR)IDI_ICON2 );
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=wndproc;
WndClass.lpszClassName="W",
WndClass.lpszMenuName=NULL,
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}
long WINAPI wndproc(HWND hWnd,UINT iMessage ,UINT wParam ,LONG lParam)
{
HDC hdc;
HPEN hp;
HBRUSH hb;
PAINTSTRUCT ps;
switch(iMessage)
{
case WM_CREATE:
hdc=GetDC(hWnd);
// hdcmem=CreateCompatibleDC(hdc);
//ReleaseDC(hWnd,hdc);
break;
case WM_MOUSEMOVE:
// InvalidateRect(hWnd,NULL,1);
break;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);
break;
//WINGDIAPI COLORREF WINAPI GetPixel(HDC, int, int);
//case WM_MOUSEMOVE:
// break;
case WM_TIMER:
// InvalidateRect(hWnd,NULL,2);
break;
case WM_DESTROY:
// DeleteObject(hbm);
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
void DisplayImage(HDC hdc, LPCTSTR szImagePath)
{
szImagePath="E:\\照片\\作业.jpg";
HANDLE hFile=CreateFile(szImagePath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
/*HANDLE CreateFile(
LPCTSTR lpFileName, //指向文件名的指针
DWORD dwDesiredAccess, //访问模式(写/读)
DWORD dwShareMode, //共享模式
LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针
DWORD dwCreationDisposition, //如何创建
DWORD dwFlagsAndAttributes, //文件属性
HANDLE hTemplateFile //用于复制文件句柄 ); */
//szImagePath="E:\\照片\\作业.jpg";
DWORD dwFileSize=GetFileSize(hFile, NULL); //获得图片文件的大小,用来分配全局内存
HGLOBAL hImageMemory=GlobalAlloc(GMEM_MOVEABLE, dwFileSize); //给图片分配全局内存
void *pImageMemory=GlobalLock(hImageMemory); //锁定内存
DWORD dwReadedSize; //保存实际读取的文件大小
ReadFile(hFile, pImageMemory, dwFileSize, &dwReadedSize, NULL); //读取图片到全局内存当中
GlobalUnlock(hImageMemory); //解锁内存
CloseHandle(hFile); //关闭文件句柄
IStream *pIStream;//创建一个IStream接口指针,用来保存图片流
IPicture *pIPicture;//创建一个IPicture接口指针,表示图片对象
CreateStreamOnHGlobal(hImageMemory, false, &pIStream); //用全局内存初使化IStream接口指针
OleLoadPicture(pIStream, 0, false, IID_IPicture, (LPVOID*)&(pIPicture));//用OleLoadPicture获得IPicture接口指针
//得到IPicture COM接口对象后,你就可以进行获得图片信息、显示图片等操作
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pIPicture->get_Width(&hmWidth); //用接口方法获得图片的宽和高
pIPicture->Render(hdc,0,0,100,100,0,hmHeight,hmWidth,-hmHeight,NULL); //在指定的DC上绘出图片
GlobalFree(hImageMemory); //释放全局内存
pIStream->Release(); //释放pIStream
pIPicture->Release(); //释放pIPicture
}