1. 程式人生 > >Windows客戶端開發--截圖並儲存為JPG檔案

Windows客戶端開發--截圖並儲存為JPG檔案

JPG和PNG的區別:

PNG is a true color lossless format. In practice it can accomplish a compression on standard photos of a factor of 2-3x with no quality loss with respect to the raw bitmap data.
JPEG is a true color lossy format. Depending on the settings the user can determine the tradeoff between quality and amount of compression, which can usually achieve a compression ratio well above 10x.

何為GetEncoderClsid

CLSID是指windows系統對於不同的應用程式,檔案型別,OLE物件,特殊資料夾以及各種系統元件分配一個唯一表示它的ID程式碼,用於對其身份的標示和與其他物件進行區分。

The function GetEncoderClsid in the following example receives the MIME type of an encoder and returns the class identifier (CLSID) of that encoder. The MIME types of the encoders built into Windows GDI+ are as follows:

image/bmp
image/jpeg
image/gif
image/tiff
image/png

介紹類和一些方法:

ImageCodecInfo類
The ImageCodecInfo class provides the necessary storage members and methods to retrieve all pertinent information about the installed image encoders and decoders (called codecs). Not inheritable.

Clsid方法
Gets or sets a Guid structure that contains a GUID that identifies a specific codec.

GetImageEncodersSize
The GetImageEncodersSize function gets the number of available image encoders and the total size of the array of ImageCodecInfo objects that is returned by the GetImageEncoders function.

GetImageEncoders
The GetImageEncoders function gets an array of ImageCodecInfo objects that contain information about the available image encoders.

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
    UINT num = 0;          // number of image encoders
    UINT size = 0;         // size of the image encoder array in bytes

    ImageCodecInfo* pImageCodecInfo = NULL;

    GetImageEncodersSize(&num, &size);
    if(size == 0)
    {
        return -1;  // Failure
    }

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
    {
        return -1;  // Failure
    }

    GetImageEncoders(num, size, pImageCodecInfo);

    for(UINT j = 0; j < num; ++j)
    {
        if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
        {
            *pClsid = pImageCodecInfo[j].Clsid;
            free(pImageCodecInfo);
            return j;  // Success
        }    
    }

    free(pImageCodecInfo);
    return -1;  // Failure
}

bitmap轉為jpg

先介紹幾個函式:

Bitmap::FromHBITMAP
The Bitmap::FromHBITMAP method creates a Bitmap object based on a handle to a Windows Graphics Device Interface (GDI) bitmap and a handle to a GDI palette.

Bitmap的save方法
Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

void BitmapToJpg(HBITMAP hbmpImage, int width, int height)
{
    Bitmap *p_bmp = Bitmap::FromHBITMAP(hbmpImage, NULL);

    CLSID jpgClsid;
    int result = GetEncoderClsid(L"image/jpeg", &jpgClsid);
    if(result != -1)
        std::cout << "Encoder succeeded" << std::endl;
    else
        std::cout << "Encoder failed" << std::endl;
    p_bmp->Save(L"screen.jpg", &pngClsid, NULL);
    delete p_bmp;
}

ScreenCapture

先介紹幾個函式:

CreateCompatibleDC
The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device.

CreateCompatibleBitmap
The CreateCompatibleBitmap function creates a bitmap compatible with the device that is associated with the specified device context.

BitBlt
The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.

bool ScreenCapture(int x, int y, int width, int height)
{
    HDC hDc = CreateCompatibleDC(0);
    HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);
    SelectObject(hDc, hBmp);
    BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);

    BitmapToJpg(hBmp, width, height);

    DeleteObject(hBmp);
    DeleteDC(hDc);
    return true;
}