Over a million developers have joined DZone.

Modifying the MSDN WIC Sample for Windows 8 To Add FillOpacityMask Support

DZone's Guide to

Modifying the MSDN WIC Sample for Windows 8 To Add FillOpacityMask Support

Free Resource

As a Windows 8 developer, you probably already know that it does not support OpacityMask in XAML applications. This leaves you with the option of using Direct2D. A while ago, Microsoft released a sample that demonstrated the opacity mask capabilities, available here.

This sample compiles and works well on Windows 8 (with Visual Studio 2012) as a Win32 application.

The problem, however, lies in the fact that some of the memory manipulations that are being done in a Win32 application, cannot be done in a Windows Store application, including but not limited to SafeRelease. The Direct2D core, however, remains practically untouched. To demonstrate this, I downloaded the stock WIC sample, available in the MSDN Code Gallery.

I began by refactoring the code in WicImage.cpp. Specifically, I separated the converter (IWICFormatConverter) initialization code in a separate function, also separating the converter reference itself:

void WicImage::InitializeConverter()

			bitmap_.Get(),					 // Input bitmap to convert
			GUID_WICPixelFormat32bppPBGRA,   // Destination pixel format
			WICBitmapDitherTypeNone,         // Specified dither pattern
			NULL,                            // Specify a particular palette 
			0.f,                             // Alpha threshold
			WICBitmapPaletteTypeCustom       // Palette translation type
In the header file for WicImage, I added:
ComPtr<IWICFormatConverter> converter_;
Now, in the GetD2DCompatibleBitmap function, I can include a call to InitializeConverter instead of having the snippet directly integrated in that function. The reason for this is simple - I will need to ultimately export the reference to the converter, that will be ready-to-go instead of relying on repeated reinitialization.

I added a new internal function that will return the converter:

Microsoft::WRL::ComPtr<IWICFormatConverter> WicImage::GetFormatConverter()
	return converter_;
Once this is complete, I can proceed to operate on my opacity mask. In the ImageEditor class, I create a new function called ApplyTestMask:
void ImageEditor::ApplyTestMask()
	D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 155, 155);


	ComPtr<ID2D1Bitmap> bitmap;
	ID2D1BitmapBrush *m_pFernBitmapBrush;


	D2D1_BITMAP_BRUSH_PROPERTIES propertiesXClampYClamp = 




	renderTarget_->DrawRectangle(&rcBrushRect, m_pBlackBrush, 1.f);
Pay attention to the CreateBitmapFromWicBitmap call. A mistake here would be attempting to directly retrieve the IWICBitmap from the WicImage class. This, however, will result in an exception notifying you that an existing read or write lock already exists. Instead, pass the converter, that will return a valid image that is already pre-formatted, after which you will be able to create a bitmap brush and call FillOpacityMask.

The approach is very similar for gradient brushes, which do not require explicit access to the WicImage content.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}