【问题标题】:CreateBitmapFromWicBitmap access violationCreateBitmapFromWicBitmap 访问冲突
【发布时间】:2015-01-29 21:21:36
【问题描述】:

这是我的问题: 我正在做一个测试项目,它初始化 Direct2D 以绘制一些东西。

现在,我需要创建 BMP 背景,所以我在 MSDN 网站上查看了一些加载 bmp 以将其与 Direct2D 一起使用的教程。经过一些编码和调试后,我遇到了唯一无法完全理解的问题,好吧,我被困在这里了。问题很简单:我在这一行得到 ACCESS VIOLATION: pRenderTarget->CreateBitmapFromWicBitmap( pConverter, NULL, ppBitmap); 我修复了所有我能找到的问题并测试了每个 HRESULT。

这是 background.cpp 的完整代码: `

#include "Background.h"
using namespace D2D1;
Background::Background()
{
    pIWICFactory = nullptr;
    pDecoder = nullptr;
    pSource = nullptr;
    pStream = nullptr;
    pConverter = nullptr;
    ppBitmap = nullptr;
    destinationWidth = 0;
    destinationHeight = 0;
    file_path = L"Background.bmp";
}
bool Background::init(HWND hwnd)
{
    CoInitializeEx(0, COINIT_MULTITHREADED);
    CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWICFactory));
    pIWICFactory->CreateDecoderFromFilename(file_path, NULL, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &pDecoder);
    pIWICFactory->CreateStream(&pStream);
    pStream->InitializeFromFilename(file_path, GENERIC_READ);
    pDecoder->Initialize(pStream,WICDecodeMetadataCacheOnLoad);
    pDecoder->GetFrame(0, &pSource);
    pIWICFactory->CreateFormatConverter(&pConverter);
    pConverter->Initialize(pSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut);
    pRenderTarget->CreateBitmapFromWicBitmap( pConverter, NULL, ppBitmap);
    return true;
}
Background::~Background()
{
    CoUninitialize();
    pIWICFactory->Release();
    pDecoder->Release();
    pSource->Release();
    pStream->Release();
    pConverter->Release();
    CoUninitialize();
}

`

这里是父类“Render.cpp” (我如何管理类继承与问题之间没有联系 - 我试图创建一个只包含一个类的新项目,包括渲染和背景)

`

#include "Render.h"
using namespace D2D1;
Render::Render()
{
    pD2DFactory = nullptr;
    pRenderTarget = nullptr;
    pGreenBrush = nullptr;
}

bool Render::Init(HWND hwnd)
{
    HRESULT hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory );
    RECT rc;
    GetClientRect(hwnd, &rc);
    hr = pD2DFactory->CreateHwndRenderTarget(RenderTargetProperties(), HwndRenderTargetProperties(hwnd, SizeU( rc.right - rc.left, rc.bottom - rc.top)),&pRenderTarget);
    if (SUCCEEDED(hr))          
      pRenderTarget->CreateSolidColorBrush(ColorF(ColorF::Green), &pGreenBrush ); 
    else
      return false;
    return true;
}

bool Render::Draw(HWND hwnd)
{
    RECT rc;
    GetClientRect(hwnd, &rc);

    pRenderTarget->BeginDraw();

    pRenderTarget->FillRectangle(
        RectF(
            rc.left + 500.0f,
            rc.top + 250.0f,
            rc.right - 500.0f,
            rc.bottom - 500.0f),
            pGreenBrush);   

    HRESULT hr = pRenderTarget->EndDraw();  

    if (SUCCEEDED(hr))
        return true;
    else
        return false;
}
void Render::ShutDown()
{
    if (pD2DFactory)
        pD2DFactory->Release();
    if (pRenderTarget)
        pRenderTarget->Release();
    if (pGreenBrush)
        pGreenBrush->Release();
}

`

【问题讨论】:

    标签: visual-studio-2010 rendering direct2d rendertarget


    【解决方案1】:

    从双 'pp' 我假设您将 ppBitmap; 声明为 D2D1Bitmap**,也是因为您将它作为值传递给 CreateBitmapFromWicBitmap

    如果正确,您的解决方案很简单:声明 ptrP*,而不是 ptrptrPP**

    // in class declaration or c'tor(?)
    D2D1Bitmap* pBitmap = nullptr;      // instead of D2D1Bitmap**
    
    /// at the point of creation
    D2D1wndTarget->CreateBitmapFromWicBitmap(<yourConverter>, NULL, &pBitmap);
    

    在您的示例中,您将其声明为 null-ptr 并将其传递给一个函数,该函数将地址指向指针,导致函数读取 0x000000 导致访问冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-06
      • 2013-02-26
      • 2015-04-23
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多