// This example creates a LEAD Bitmap Tiff encoder, and then gets and displays all the dithering types.
// Refer to the Example_Requirements for help in running this sample.
HRESULT IWICLeadBitmapEncoder_GetDitherTypes_Example(HWND hWnd)
{
   HRESULT hr = S_OK;
   IWICImagingFactory *piImagingFactory = NULL;
   IWICBitmapEncoder *piBitmapEncoder = NULL;
   IWICLeadBitmapEncoder *piLeadBitmapEncoder = NULL;
   UINT uDitherTypes = 0;
   WICLeadDitherType *pDitherTypes = NULL;
   CString csExampleName = L"IWICLeadBitmapEncoder_GetDitherTypes_Example";
   CString csMsg = csExampleName + "\n\n";
   // Create a LEAD TIFF Bitmap Encoder
   IFS(CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*) &piImagingFactory));
   IFS(piImagingFactory->CreateEncoder(GUID_ContainerFormatLeadTiff, NULL, &piBitmapEncoder));
   // QueryInterface on the encoder to get the IWICLeadBitmapEncoder interface
   IFS(piBitmapEncoder->QueryInterface(IID_WICLeadBitmapEncoder, reinterpret_cast<void**>(&piLeadBitmapEncoder)));
   // Choose a BitsPerPixel that requires dithering (less than 24)
   // Call GetDitherTypes twice.  First to get the required size of the array, then to fill the array
   IFS(piLeadBitmapEncoder->GetDitherTypes(GUID_WICPixelFormat8bppIndexed, 0, NULL, &uDitherTypes));
   if (SUCCEEDED(hr))
   {
      pDitherTypes = new WICLeadDitherType[uDitherTypes];
      if (pDitherTypes)
      {
         IFS(piLeadBitmapEncoder->GetDitherTypes(GUID_WICPixelFormat8bppIndexed, uDitherTypes, pDitherTypes, &uDitherTypes));
      }
   }
   // Get and display the WICLeadDitherType friendly name
   if (SUCCEEDED(hr))
   {
      UINT uActual = 0;
      WCHAR *pwzFriendlyName = NULL;
      for (UINT i = 0; i < uDitherTypes; i++)
      {
         // Call once to get length of friendly name
         IFS(piLeadBitmapEncoder->GetDitherTypeFriendlyName(pDitherTypes[i], uActual, NULL, &uActual));
         if (SUCCEEDED(hr))
         {
            pwzFriendlyName = new WCHAR[uActual];
            // Call again to fill the friendly name buffer
            IFS(piLeadBitmapEncoder->GetDitherTypeFriendlyName(pDitherTypes[i], uActual, pwzFriendlyName, &uActual));
            csMsg = csMsg + CString(pwzFriendlyName) + "\n";
            DELETE_POINTER(pwzFriendlyName);
         }
      }
   }
   RELEASE_INTERFACE(piLeadBitmapEncoder);
   RELEASE_INTERFACE(piBitmapEncoder);
   RELEASE_INTERFACE(piImagingFactory);
   DELETE_POINTER(pDitherTypes);
   MessageBox(hWnd, csMsg, csExampleName, MB_OK);
   return hr;
}