#include <windows.h>
#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>
#pragma comment (lib,"Ole32.lib")
void ScreenSave(const char *filename);//save screen屏
void SaveJPEG(TCHAR *tszInFileName,TCHAR *tszOutFileName);//bmp to jpg
HRESULT CreateStreamOnFile(const TCHAR * tszFilename, IStream ** ppStream);
BOOL GetEnCodecCLSID(IImagingFactory* pImagingFactory,WCHAR * wszMimeType ,CLSID * pclsid );
int WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPWSTR lpCmdLine,int nShowCmd)
{
if(!CreateMutex(NULL,false,L"IImage") || ERROR_ALREADY_EXISTS==GetLastError())
return 0;
/* TCHAR timer[MAX_PATH]={0};
GetModuleFileName(NULL,timer,MAX_PATH);
DWORD dwStart = ::GetTickCount();
MessageBox(NULL,L"running",L"time",MB_OK);*/
ScreenSave("\\Storage Card\\picture.bmp");
SaveJPEG(TEXT("\\Storage card\\picture.bmp"),TEXT("\\Storage card\\picture.jpg"));
// MessageBox(NULL,timer,L"time",MB_OK);
/* DWORD dwEnd = ::GetTickCount();*/
// swprintf(timer,L"%d",dwEnd-dwStart);
return 0;
}
void ScreenSave(const char *filename)
{
HDC hScrDC, hMemDC;
int width, height;
//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL;
//creates a device context for the screen device
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//get the screen point size
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);
//creates a memory device context (DC) compatible with the screen device(hScrDC)
hMemDC = CreateCompatibleDC(hScrDC);
//initialise the struct BITMAPINFO for the bimap infomationㄛ
//in order to use the function CreateDIBSection
//on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
RGB24BitsBITMAPINFO.bmiHeader.biClrUsed = 8;
//use the function CreateDIBSection and SelectObject
//in order to get the bimap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;
//bimap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)
//write into file
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, 3*width*height, 1, mStream);
//close file
fclose(mStream);
}
//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);
}
BOOL GetEnCodecCLSID(IImagingFactory* pImagingFactory,WCHAR * wszMimeType ,CLSID * pclsid )
{
UINT uiCount;
ImageCodecInfo * codecs;
HRESULT hr;
BOOL fRet = FALSE;
hr = pImagingFactory->GetInstalledEncoders(&uiCount, &codecs);
for (UINT i = 0; i < uiCount; i++)
{
if (wszMimeType && !wcscmp(wszMimeType, codecs[i].MimeType))
{
*pclsid = codecs[i].Clsid;
fRet = TRUE;
break;
}
}
CoTaskMemFree(codecs);
return fRet;
}
void SaveJPEG(TCHAR *tszInFileName,TCHAR *tszOutFileName)
{
HRESULT hr;
IImagingFactory * pImagingFactory = NULL ;
IStream *pStream = NULL;
IImageSink *pImageSink = NULL;
IImageDecoder *pImageDecoder = NULL;
IImageEncoder *pImageEncoder = NULL;
CLSID clsidEncoder;
TCHAR /*tszInFileName, *tszOutFileName, */*tszMime;
tszMime = L"image/jpeg";
if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
return;
}
hr = CoCreateInstance(CLSID_ImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void**) &pImagingFactory);
if (FAILED(hr))
{
goto finish;
}
if (FAILED(hr = CreateStreamOnFile(tszInFileName, &pStream)))
{
goto finish;
}
if (!GetEnCodecCLSID(pImagingFactory,tszMime, &clsidEncoder ))
{
goto finish;
}
if (FAILED(hr = pImagingFactory->CreateImageEncoderToFile(&clsidEncoder, tszOutFileName, &pImageEncoder)))
{
goto finish;
}
if (FAILED(hr = pImagingFactory->CreateImageDecoder(pStream, DecoderInitFlagBuiltIn1st, &pImageDecoder)))
{
goto finish;
}
if (FAILED(hr = pImageEncoder->GetEncodeSink(&pImageSink)))
{
goto finish;
}
if (FAILED(hr = pImageDecoder->BeginDecode(pImageSink, NULL)))
{
goto finish;
}
for(;;)
{
hr = pImageDecoder->Decode();
if (E_PENDING == hr)
{
Sleep(500);
}
else if (FAILED(hr))
{
pImageDecoder->EndDecode(hr);
goto finish;
}
else
{
break;
}
}
pImageDecoder->EndDecode(hr);
pImageSink->Release();
pImageSink = NULL;
pImageEncoder->TerminateEncoder();
finish:
if (pStream)
pStream->Release();
if (pImageSink)
pImageSink->Release();
if (pImageDecoder)
pImageDecoder->Release();
if (pImageEncoder)
pImageEncoder->Release();
if (pImagingFactory)
pImagingFactory->Release();
CoUninitialize();
}
HRESULT CreateStreamOnFile(const TCHAR * tszFilename, IStream ** ppStream)
{
HRESULT hrRet = S_FALSE;
HGLOBAL hg = NULL;
HANDLE hFile = NULL;
DWORD dwSize, dwRead;
BYTE* pbLocked = NULL;
hFile = CreateFile(tszFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
goto error;
}
dwSize = GetFileSize(hFile, NULL);
if (0xffffffff == dwSize)
{
goto error;
}
hg = GlobalAlloc(GMEM_MOVEABLE, dwSize);
if (NULL == hg)
{
goto error;
}
pbLocked = (BYTE*) GlobalLock(hg);
if (NULL == pbLocked)
{
goto error;
}
if (!ReadFile(hFile, pbLocked, dwSize, &dwRead, NULL))
{
goto error;
}
GlobalUnlock(hg);
hrRet = CreateStreamOnHGlobal(hg, TRUE, ppStream);
CloseHandle(hFile);
return hrRet;
error:
if (pbLocked)
GlobalUnlock(hg);
if (hg)
GlobalFree(hg);
if (hFile)
CloseHandle(hFile);
return hrRet;
}