Direct2D Intro - Load Bitmap From Resource

本文介绍如何利用Windows Imaging Component (WIC)中的IWICBitmapClipper组件从IWICBitmapSource中获取矩形区域。通过创建IWICImagingFactory并使用CreateDecoderFromFilename方法解码图像文件,接着获取图像帧并初始化IWICBitmapClipper来定义裁剪区域。


https://msdn.microsoft.com/en-us/library/windows/desktop/ee719657(v=vs.85).aspx


This topic demonstrates how to obtain a rectangular portion of an IWICBitmapSource using an IWICBitmapClipper component.

To clip a bitmap source

  1. Create an IWICImagingFactory object to create Windows Imaging Component (WIC) objects.

    C++
    // Create WIC factory
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&m_pIWICFactory)
        );
    
    
    
  2. Use the CreateDecoderFromFilename method to create an IWICBitmapDecoder from an image file.

    C++
    HRESULT hr = S_OK;
    
    IWICBitmapDecoder *pIDecoder = NULL;
    IWICBitmapFrameDecode *pIDecoderFrame  = NULL;
    
    hr = m_pIWICFactory->CreateDecoderFromFilename(
       L"turtle.jpg",                  // Image to be decoded
       NULL,                           // Do not prefer a particular vendor
       GENERIC_READ,                   // Desired read access to the file
       WICDecodeMetadataCacheOnDemand, // Cache metadata when needed
       &pIDecoder                      // Pointer to the decoder
       );
    
    
    
  3. Get the first IWICBitmapFrameDecode of the image.

    C++
    // Retrieve the first bitmap frame.
    if (SUCCEEDED(hr))
    {
       hr = pIDecoder->GetFrame(0, &pIDecoderFrame);
    }
    
    
    

    The JPEG file format only supports a single frame. Because the file in this example is a JPEG file, the first frame (0) is used. For image formats that have multiple frames, see How to Retrieve the Frames of an Image for accessing each frame of the image.

  4. Create the IWICBitmapClipper to use for the image clipping.

    C++
    IWICBitmapClipper *pIClipper = NULL;
    
    if (SUCCEEDED(hr))
    {
       hr = m_pIWICFactory->CreateBitmapClipper(&pIClipper);
    }
    
    
    
  5. Initialize the clipper object with the image data within the given rectangle of the bitmap frame.

    C++
    // Create the clipping rectangle.
    WICRect rcClip = { 0, 0, uiWidth/2, uiHeight/2 };
    
    // Initialize the clipper with the given rectangle of the frame's image data.
    if (SUCCEEDED(hr))
    {
       hr = pIClipper->Initialize(pIDecoderFrame, &rcClip);
    }
    
    
    
  6. Draw or process the clipped image.

    The following illustration demonstrates imaging clipping. The original image on the left is 200 x 130 pixels. The image on the right is the original image clipped to a rectangle defined as {20,20,100,100}.

    Illustration of imaging clipping

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值