| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:看下 我的JPG图片怎么加载不进去?
只看楼主 加入收藏
huazhao
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-10-15
结帖率:0
收藏
 问题点数:0 回复次数:0 
看下 我的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
}
搜索更多相关主题的帖子: 加载 JPG 
2010-11-22 11:05
快速回复:看下 我的JPG图片怎么加载不进去?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016027 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved