在IExtractImage中创建HBitmap

时间:2017-06-08 11:46:15

标签: winapi gdi

我正在开发缩略图创建者作为shell扩展。

为此,我选择实施接口IExtractImage

我的dll已加载并正确调用,但缩略图始终为黑色,而不是红色。

我错过了什么?

class MyShellPreview : public IExtractImage, IPersistFile

    // set by IExtractImage::GetLocation
    SIZE m_size;


    IFACEMETHODIMP Extract(HBITMAP *phBmpImage)
    {
        size_t size = m_size.cx * m_size.cy * 3;

        // alloc buffer
        BYTE *buffer = (BYTE*)malloc(size);

        // fill buffer
        for (k = i = 0; i < m_size.cx; ++i)
        {
            for (j = 0; j < m_size.cy; ++j, ++k)
            {
                buffer[k] = 128;
                buffer[k+1] = 0;
                buffer[k+2] = 0;
            }
        }

        *phBmpImage = CreateBitmap(m_size.cx, m_size.cy, 3, 8, buffer);

        free(buffer);

        return S_OK;
    }        
};

我知道出于性能原因,我应该使用CreateCompatibleBitmapSetDIBits,但我不确定应该从哪里获得HDC

1 个答案:

答案 0 :(得分:0)

正如评论所指出的,解决方案是使用:

  • 32位图像
  • 1个色彩平面


class MyShellPreview : public IExtractImage, IPersistFile

    // set by IExtractImage::GetLocation
    SIZE m_size;

    IFACEMETHODIMP Extract(HBITMAP *phBmpImage)
    {
        size_t size = m_size.cx * m_size.cy * 4;

        // alloc buffer
        BYTE *buffer = (BYTE*)calloc(size, 1);

        // fill buffer
        for (k = i = 0; i < m_size.cx; ++i)
        {
            for (j = 0; j < m_size.cy; ++j, k+=4)
            {
                buffer[k] = 128;                    
            }
        }

        *phBmpImage = CreateBitmap(m_size.cx, m_size.cy, 1, 24, buffer);

        free(buffer);

        return S_OK;
    }        
};