首页 > 解决方案 > Direct2d ID2D1GradientStopCollection1 - 如何创建

问题描述

我正在尝试使用 Direct2D 1.1 创建渐变。

具体来说,我正在尝试创建一个 ID2D1GradientStopCollection1。

我的代码:

ID2D1GradientStopCollection1* native = nullptr;

hr = context2_->CreateGradientStopCollection(
    (D2D1_GRADIENT_STOP*)gradientStops,
    gradientStopsCount,
    D2D1_COLOR_SPACE_SRGB,
    D2D1_COLOR_SPACE_SRGB,
    D2D1_BUFFER_PRECISION_UNKNOWN,
    D2D1_EXTEND_MODE_CLAMP,
    D2D1_COLOR_INTERPOLATION_MODE_STRAIGHT,
    &native
);

// hr returns 0x8899000a : A call to this method is invalid.

注意:context2_ 的类型:ID2D1DeviceContext*

执行此语句失败。hr中返回的错误码是0x8899000a(调用此方法无效。)

任何帮助使它工作表示赞赏。

标签: c++windowsdirect2d

解决方案


此方法需要特定的缓冲区精度。

hr = context2_->CreateGradientStopCollection(
    (D2D1_GRADIENT_STOP*)gradientStops,
    gradientStopsCount,
    D2D1_COLOR_SPACE_SRGB,
    D2D1_COLOR_SPACE_SRGB,
    D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB, // Buffer precision
    D2D1_EXTEND_MODE_CLAMP,
    D2D1_COLOR_INTERPOLATION_MODE_STRAIGHT,
    &native2);

推荐阅读