【问题标题】:Direct2d clone bitmapDirect2d 克隆位图
【发布时间】:2015-04-17 07:42:00
【问题描述】:

有没有人知道如何“克隆”位图(ID2D1Bitmap)。

我想克隆位图的一部分,并让它具有原始的所有相同属性(DPI、分辨率等),唯一不同的是大小。 Gdiplus有一个我想模拟的克隆功能。

谢谢

我最初是从这个开始的

CComPtr<ID2D1Bitmap> imageD;
      // if region is entire original image no need to create a cropped image
      if (left != 0 || top != 0 || right != imageSize.width || bottom != imageSize.height)
      {
         // get format of original image
         D2D1_PIXEL_FORMAT fmt = imageData.image->GetPixelFormat();

         D2D1_SIZE_U bitmapPixelSize = D2D1::SizeU((UINT)(right - left), (UINT)(bottom - top));

         // create destination image of "clipped" source image
         HRESULT hr = m_pRenderTarget->CreateBitmap(bitmapPixelSize, D2D1::BitmapProperties(
            D2D1::PixelFormat(fmt.format, fmt.alphaMode),
            (float)imageData.resX, (float)imageData.resY), &imageD);

         if (hr != S_OK)
         {
            return;
         }

         D2D1_POINT_2U topleft = D2D1::Point2U(0, 0);
         D2D1_RECT_U srcRect = D2D1::RectU((UINT)left, (UINT)top, (UINT)right, (UINT)bottom);
         // get the "clipped" source
         hr = imageD->CopyFromBitmap(&topleft, imageData.image, &srcRect);

         if (hr != S_OK)
         {
            return;
         }

         imageSize = imageD->GetPixelSize();
      }
      else
         imageD = imageData.image;

      // create a textured brush w/ wrapping capabilities
      CComPtr <ID2D1BitmapBrush> pFabricBitmapBrush;
      HRESULT hr = m_pRenderTarget->CreateBitmapBrush(imageD, &pFabricBitmapBrush);
      pFabricBitmapBrush->SetExtendModeX(D2D1_EXTEND_MODE_WRAP);
      pFabricBitmapBrush->SetExtendModeY(D2D1_EXTEND_MODE_WRAP);

如您所见,我将新的(裁剪的)位图用作画笔。当我这样做时,画笔本身会绘制一个较小的图像,使用带有 Gdiplus 的“克隆”API,逻辑会正确绘制。这就是让我想到 Direct2d“副本”没有给我与原始图像相同的属性的想法。

这似乎有效。分辨率、DPI、尺寸等 - 必须仔细阅读。

float dpiXs, dpiYs;
         imageData.image->GetDpi(&dpiXs, &dpiYs);

         D2D1_SIZE_U bitmapPixelSize = D2D1::SizeU((UINT)(right - left), (UINT)(bottom - top));

         // create destination image of "clipped" source image
         HRESULT hr = m_pRenderTarget->CreateBitmap(bitmapPixelSize, D2D1::BitmapProperties(
            D2D1::PixelFormat(fmt.format, fmt.alphaMode), dpiXs, dpiYs), &imageD);

【问题讨论】:

    标签: bitmap clone direct2d


    【解决方案1】:

    没有这样的函数,但你可以自己写。

    function CloneBitmap(src: ID2D1Bitmap; x, y, width, height: Integer; dst: ID2D1Bitmap): HResult;
    begin
      Result :=
        // I assume that you have an access to the DeviceContext (or an ID2D1RenderTarget),
        // so you can create a new bitmap
        yourDeviceContext.CreateBitmap(
          D2D_SIZE_U(width, height),
          nil,
          0,
          src.GetPixelFormat,
          dst);
    
      if Succeeded(Result) then
        Result :=
          dst.CopyFromBitmap(
            D2D_POINT_2F(0, 0),
            src,
            D2D_RECT_U(
              x,
              y,
              x + width,
              y + height));
    end;
    

    我建议阅读ID2D1RenderTarget::CreateBitmapID2D1Bitmap::CopyFromBitmap,这样您就会了解这些方法何时会失败(即,当上述函数结果与S_OK 不同时)。

    顺便说一句,您是否考虑过使用ID2D1RenderTarget::DrawBitmap 方法(直接在您的渲染循环中)“即时”裁剪和绘制?

    【讨论】:

    • 查看我在原始帖子中使用的代码 - 我会挖掘更多内容并稍后发布。
    猜你喜欢
    • 2010-11-22
    • 2015-04-20
    • 1970-01-01
    • 2013-02-23
    • 2017-05-07
    • 1970-01-01
    • 2015-05-02
    • 2012-05-06
    • 2016-10-01
    相关资源
    最近更新 更多