⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 olepic.c

📁 convert a small image to a text image ascii art
💻 C
字号:

#include <windows.h>
#include <olectl.h>
#include <stdio.h>
#include <shlwapi.h>

#pragma comment(lib,"shlwapi.lib") 

extern const IID IID_IPicture;
#define HIMETRIC_INCH	2540


////////////////////////////////////////////////////////////////////////


short OleGetPicType(LPPICTURE pIPicture)
{

	short type;


	pIPicture->lpVtbl->get_Type(pIPicture, &type);
/*
	switch(type){
	case PICTYPE_UNINITIALIZED:

		break;
	case PICTYPE_NONE:

		break;
	case PICTYPE_BITMAP:

		break;
	case PICTYPE_METAFILE:

		break;
	case PICTYPE_ICON:

		break;
	case PICTYPE_ENHMETAFILE:

		break;

	default:

	}
*/
	return type;
}

int OleGetPicWidth(LPPICTURE pIPicture)
{
	int Width;
	pIPicture->lpVtbl->get_Width(pIPicture, &Width);
	return Width;
}

int OleGetPicHeight(LPPICTURE pIPicture)
{
	int Height;
	pIPicture->lpVtbl->get_Height(pIPicture, &Height);
	return Height;
}

void OlePicRender(LPPICTURE pIPicture, PAINTSTRUCT *ps)
{
	long hmWidth = OleGetPicWidth(pIPicture, ps->hdc);
	long hmHeight = OleGetPicHeight(pIPicture, ps->hdc);

	// convert himetric to pixels
	int nWidth	= MulDiv(hmWidth, GetDeviceCaps(ps->hdc, LOGPIXELSX), HIMETRIC_INCH);
	int nHeight	= MulDiv(hmHeight, GetDeviceCaps(ps->hdc, LOGPIXELSY), HIMETRIC_INCH);

	pIPicture->lpVtbl->Render(pIPicture, ps->hdc, 0, 0, nWidth, nHeight, 0, hmHeight,
   					 hmWidth, -hmHeight, &ps->rcPaint);
}



void OlePicRenderFit(LPPICTURE pIPicture, PAINTSTRUCT *ps,HWND HW)
{RECT rcDlg;

	long hmWidth = OleGetPicWidth(pIPicture, ps->hdc);
	long hmHeight = OleGetPicHeight(pIPicture, ps->hdc);

	// convert himetric to pixels
	int nWidth	= MulDiv(hmWidth, GetDeviceCaps(ps->hdc, LOGPIXELSX), HIMETRIC_INCH);
	int nHeight	= MulDiv(hmHeight, GetDeviceCaps(ps->hdc, LOGPIXELSY), HIMETRIC_INCH);

		
	GetWindowRect(HW, &rcDlg);
	

	pIPicture->lpVtbl->Render(pIPicture, ps->hdc, 
		             0, 0,rcDlg.right, rcDlg.bottom,   //fit ecran
		             0, hmHeight, hmWidth, -hmHeight,  //image size
					 &ps->rcPaint);
}

HANDLE OleGetPicHandle(LPPICTURE pIPicture)
{
	HANDLE Handle;
	pIPicture->lpVtbl->get_Handle(pIPicture, &Handle);
	return Handle;
}


BOOL Ole2bmp(IPicture* pPicture,LPCTSTR szPath)
{
    IStream* pStream = NULL;
    HGLOBAL hgl = NULL;
    void* lpVoid = NULL; 
    long nSize = 0;
    HRESULT hr = S_OK;
    HANDLE handle = NULL;
    LONG res = 0;
    DWORD err = 0;

	 //////////////
	 //ZeroMemory( &hgl, sizeof(hgl) );
	 //ZeroMemory( &pStream, sizeof(pStream) );
	 //////////////

    

    if(pPicture == NULL)  return NULL;
    
    do
    {
        hgl = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,8192);
		//hgl  =GlobalAlloc(GMEM_FIXED, dwFileSize);
        if(hgl == NULL)    return NULL;
        CreateStreamOnHGlobal(hgl,FALSE,&pStream);
        if(pStream == NULL)  return NULL;

		////////////////////////
        pPicture->lpVtbl->put_KeepOriginalFormat(pPicture,TRUE);
        hr = pPicture->lpVtbl->SaveAsFile(pPicture,pStream,TRUE,&nSize);
		////////////////////////////

        if(FAILED(hr))   return NULL;

        handle = CreateFile(szPath, GENERIC_WRITE,   FILE_SHARE_WRITE,NULL,
            CREATE_ALWAYS,   FILE_ATTRIBUTE_NORMAL,        NULL);
        lpVoid = GlobalLock(hgl);
        WriteFile(handle,lpVoid,nSize,&res,NULL);
    }
    while(FALSE);


    pStream->lpVtbl->Release(pStream);   
    GlobalUnlock(hgl);
    GlobalFree(hgl);
    CloseHandle(handle);


    return (res != 0) && (res == nSize);
}





HBITMAP Ole2BITMAP(char *szFile, LPPICTURE *pIPicture)
{

HBITMAP hBitmap;
HBITMAP bmp;
DWORD dwFileSize;
DWORD dwBytesRead;
HANDLE hFile;
LPPICTURE gpPicture;
LPVOID lpPicData;
LPVOID pvData;
LPSTREAM pstm;
LPVOID hrlp;
HGLOBAL hGlobal;
BITMAP bm ;

 /*
	 //////////////
	 ZeroMemory( &bm, sizeof(bm) );
     ZeroMemory( &hBitmap, sizeof(hBitmap) );
	 ZeroMemory( &gpPicture, sizeof(gpPicture) );
	 ZeroMemory( &lpPicData, sizeof(lpPicData) );
	 ZeroMemory( &pvData, sizeof(pvData) );
	 ZeroMemory( &pstm, sizeof(pstm) );
	 //////////////
	 */

hFile=CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
dwFileSize=GetFileSize(hFile,NULL);
if(dwFileSize==-1) return NULL;

pvData=NULL;
hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
//hGlobal =GlobalAlloc(GMEM_FIXED, dwFileSize);
pvData=GlobalLock(hGlobal);
ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);


CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
//OleLoadPicture(pstm,0,FALSE,&IID_IPicture,(LPVOID*)&gpPicture);
OleLoadPicture(pstm,0,TRUE,&IID_IPicture,(LPVOID*)&gpPicture);
pstm->lpVtbl->Release(pstm);
gpPicture->lpVtbl->get_Handle(gpPicture,(OLE_HANDLE*)&hBitmap);

GetObject (hBitmap, sizeof (BITMAP), (PSTR) &bm) ;



bmp = CopyImage(hBitmap,IMAGE_BITMAP,bm.bmWidth, bm.bmHeight,LR_COPYRETURNORG);

 
GlobalFree(hGlobal);
DeleteObject(hBitmap);
 

return bmp;

}






DWORD OleLoadPicFile(char *szFile, LPPICTURE *pIPicture)
{
	LPPICTURE ppIPicture = *pIPicture;
	BOOL bRead ;
	DWORD dwBytesRead = 0;
	HRESULT hr;
	LPSTREAM pstm = NULL;
	HANDLE hFile;
	HGLOBAL hGlobal;
	LPVOID pvData = NULL;
	DWORD dwFileSize;

		 //////////////
	 ZeroMemory( &pIPicture, sizeof(pIPicture) );
     ZeroMemory( &pstm, sizeof(pstm) );
 	 ZeroMemory( &pvData, sizeof(pvData) );
 	 //////////////

	// open file
	hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

	// get file size
	dwFileSize = GetFileSize(hFile, NULL);

	
	// alloc memory based on file size
	hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);

	pvData = GlobalLock(hGlobal);

	
	// read file and store in global memory
	bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
	GlobalUnlock(hGlobal);
	CloseHandle(hFile);

	
	// create IStream* from global memory
	hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);

	// Destroy last picture if exists
	if (ppIPicture)
		ppIPicture->lpVtbl->Release(ppIPicture);

	hr = OleLoadPicture(pstm, dwFileSize, FALSE, &IID_IPicture, (LPVOID)pIPicture);
	pstm->lpVtbl->Release(pstm);

	return hr;
}



// This function loads a file into an IStream.
DWORD OleLoadPicRes(HINSTANCE hInst, int resID, LPPICTURE *pIPicture)
{
	char name[100];
	DWORD dwResSize;
	HGLOBAL hNewMem;
	HRSRC hRes 	;
	HRESULT hr;
	HGLOBAL hResGlobal;
	LPVOID lpResMem ;
	LPVOID lpNewMem;
	LPSTREAM pstm = NULL;
	LPPICTURE ppIPicture;


	

	 //////////////
	 ZeroMemory( &pIPicture, sizeof(pIPicture) );
     ZeroMemory( &pstm, sizeof(pstm) );
	 ZeroMemory( &ppIPicture, sizeof(ppIPicture) );
 	 ZeroMemory( &lpResMem, sizeof(lpResMem) );
	 ZeroMemory( &lpNewMem, sizeof(lpNewMem) );
 	 //////////////

	//sprintf(name, "#%d", resID);

	ppIPicture = *pIPicture;
	// Make resource ident string e.g. "#307"

	// Load resource
	 hRes 			= FindResource(hInst, MAKEINTRESOURCE(resID), RT_RCDATA);
	//hRes 			= FindResource(hInst,   name , RT_RCDATA);

	dwResSize	= SizeofResource(hInst, hRes);
	hResGlobal 	= LoadResource(hInst, hRes);
	lpResMem 	= LockResource(hResGlobal);

	// The
	// Make a new global memory block with GMEM_MOVEABLE
	hNewMem = GlobalAlloc(GMEM_MOVEABLE, dwResSize);
	lpNewMem = GlobalLock(hNewMem);

	// Copy old to new
	memcpy(lpNewMem, lpResMem, dwResSize);

	// Unlock both memory's
	GlobalUnlock(hNewMem);
	GlobalUnlock(hResGlobal);

	// Pass new memory to CreateStreamOnHGlobal and create IStream* from global memory
	// The memory must be MOVEABLE for this to work. I tried making the return
	// from LoadResource() MOVEABLE with GlobalReAlloc() but it would not work.
	
	hr = CreateStreamOnHGlobal(hNewMem, TRUE, &pstm);

	

	// Create IPicture from image file
	hr = OleLoadPicture(pstm, dwResSize, FALSE, &IID_IPicture, (LPVOID)pIPicture);
	pstm->lpVtbl->Release(pstm);


	// Destroy last picture if exists
	if (ppIPicture)	ppIPicture->lpVtbl->Release(ppIPicture);

	return hr;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -