如何修改點陣圖來源的圖元
本主題示範如何使用IWICBitmap 和 IWICBitmapLock元件來修改點陣圖來源的圖元。
修改點陣圖來源的圖元
建立 IWICImagingFactory 物件,以在 WIC) 物件 (建立 Windows 映像元件。
// Create WIC factory hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pIWICFactory) );
使用 CreateDecoderFromFilename 方法,從影像檔建立 IWICBitmapDecoder 。
HRESULT hr = S_OK; IWICBitmapDecoder *pIDecoder = NULL; IWICBitmapFrameDecode *pIDecoderFrame = NULL; hr = m_pIWICFactory->CreateDecoderFromFilename( L"turtle.jpg", // Image to be decoded NULL, // Do not prefer a particular vendor GENERIC_READ, // Desired read access to the file WICDecodeMetadataCacheOnDemand, // Cache metadata when needed &pIDecoder // Pointer to the decoder );
取得影像的第一個 IWICBitmapFrameDecode 。
// Retrieve the first bitmap frame. if (SUCCEEDED(hr)) { hr = pIDecoder->GetFrame(0, &pIDecoderFrame); }
JPEG 檔案格式僅支援單一框架。 由於此範例中的檔案是 JPEG 檔案,因此會使用第一個框架 ()
0
。 如需具有多個畫面的影像格式,請參閱 如何擷取影像的框架 ,以存取影像的每個畫面格。從先前取得的影像框架建立 IWICBitmap 。
IWICBitmap *pIBitmap = NULL; IWICBitmapLock *pILock = NULL; UINT uiWidth = 10; UINT uiHeight = 10; WICRect rcLock = { 0, 0, uiWidth, uiHeight }; // Create the bitmap from the image frame. if (SUCCEEDED(hr)) { hr = m_pIWICFactory->CreateBitmapFromSource( pIDecoderFrame, // Create a bitmap from the image frame WICBitmapCacheOnDemand, // Cache bitmap pixels on first access &pIBitmap); // Pointer to the bitmap }
針對IWICBitmap 的指定矩形取得 IWICBitmapLock。
if (SUCCEEDED(hr)) { // Obtain a bitmap lock for exclusive write. // The lock is for a 10x10 rectangle starting at the top left of the // bitmap. hr = pIBitmap->Lock(&rcLock, WICBitmapLockWrite, &pILock);
處理 IWICBitmapLock 物件現在鎖定的圖元資料。
if (SUCCEEDED(hr)) { UINT cbBufferSize = 0; BYTE *pv = NULL; // Retrieve a pointer to the pixel data. if (SUCCEEDED(hr)) { hr = pILock->GetDataPointer(&cbBufferSize, &pv); } // Pixel manipulation using the image data pointer pv. // ... // Release the bitmap lock. SafeRelease(&pILock); } }
若要解除鎖定IWICBitmap,請在與IWICBitmap 相關聯的所有 IWICBitmapLock物件上呼叫IUnknown::Release。
清除已建立的物件。
SafeRelease(&pIBitmap); SafeRelease(&pIDecoder); SafeRelease(&pIDecoderFrame);
另請參閱