Visual studio 2010 CreateBitmapFromWicBitmap访问冲突
我的问题是: 我正在做一个测试项目,它初始化Direct2D来绘制一些东西 现在,我需要创建BMP背景,所以我在MSDN站点上查看了一些加载BMP以与Direct2D一起使用的教程。经过一些编码和调试,我发现了唯一一个我不能完全理解的问题,我被困在这里了。问题很简单:我在这一行遇到访问冲突: pRenderTarget->CreateBitmapFromWicBitmap pConverter,NULL,ppBitmap; 我修复了所有可能发现的问题,并测试了每个HRESULT 下面是background.cpp的完整代码: ` ` 下面是父类Render.cpp 我管理类继承的方式与这个问题之间没有联系——我试图创建一个只包含一个类(包括render和background)的新项目 `Visual studio 2010 CreateBitmapFromWicBitmap访问冲突,visual-studio-2010,rendering,direct2d,rendertarget,Visual Studio 2010,Rendering,Direct2d,Rendertarget,我的问题是: 我正在做一个测试项目,它初始化Direct2D来绘制一些东西 现在,我需要创建BMP背景,所以我在MSDN站点上查看了一些加载BMP以与Direct2D一起使用的教程。经过一些编码和调试,我发现了唯一一个我不能完全理解的问题,我被困在这里了。问题很简单:我在这一行遇到访问冲突: pRenderTarget->CreateBitmapFromWicBitmap pConverter,NULL,ppBitmap; 我修复了所有可能发现的问题,并测试了每个HRESULT 下面是backg
`从您声明的双“pp”I asume;作为D2D1Bitmap**,也是因为您将其作为值传递给CreateBitmapFromWicBitmap 如果正确,您的解决方案很简单:声明一个ptrP*,而不是ptrpp** 在您的示例中,您将其声明为null ptr,并将其传递给一个函数,该函数采用指向指针的地址,导致函数读取0x000000,从而导致访问冲突
#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();
}
#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();
}
// in class declaration or c'tor(?)
D2D1Bitmap* pBitmap = nullptr; // instead of D2D1Bitmap**
/// at the point of creation
D2D1wndTarget->CreateBitmapFromWicBitmap(<yourConverter>, NULL, &pBitmap);