直接2D 加载和绘制位图
Direct2D Loading and Drawing Bitmap
我正在尝试将位图绘制到屏幕上;调用以从文件加载位图的代码有效,但似乎实际的绘图部分导致一些程序崩溃。
我遵循了MSDN的教程,但我所做的唯一更改是使用ID2D1RenderTarget,而是使用ID2D1DCRenderTarget。
下面是有效的 Load 方法:
HRESULT LoadBitmapFromFile(
ID2D1DCRenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCWSTR uri,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
)
{
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
uri,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr))
{
// If a new width or height was specified, create an
// IWICBitmapScaler and use it to resize the image.
if (destinationWidth != 0 || destinationHeight != 0)
{
UINT originalWidth, originalHeight;
hr = pSource->GetSize(&originalWidth, &originalHeight);
if (SUCCEEDED(hr))
{
if (destinationWidth == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
}
else if (destinationHeight == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
}
hr = pIWICFactory->CreateBitmapScaler(&pScaler);
if (SUCCEEDED(hr))
{
hr = pScaler->Initialize(
pSource,
destinationWidth,
destinationHeight,
WICBitmapInterpolationModeCubic
);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pScaler,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
}
else // Don't scale the image.
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
if (SUCCEEDED(hr))
{
// Create a Direct2D bitmap from the WIC bitmap.
hr = pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
ppBitmap
);
}
if (pDecoder) pDecoder->Release();
if (pSource) pSource->Release();
if (pStream) pStream->Release();
if (pScaler) pScaler->Release();
return hr;
}
在渲染目标的 BeginDraw() 和 EndDraw() 之间,我有以下代码来绘制位图。这是我尝试运行程序时使程序崩溃的部分:
d2dg->pRT->BeginDraw();
// Create a WIC Factory
HRESULT hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&wicFactory);
hr = LoadBitmapFromFile(d2dg->pRT, wicFactory, L"image1.png", 400, 400, &pBitmap);
D2D1_SIZE_F size = pBitmap->GetSize();
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(100.f, 10.f);
// Draw a bitmap.
d2dg->pRT->DrawBitmap(
pBitmap,
D2D1::RectF(
upperLeftCorner.x,
upperLeftCorner.y,
upperLeftCorner.x + size.width,
upperLeftCorner.y + size.height)
);
d2dg->pRT->EndDraw();
发现问题:我想绘制的位图仍然是 NULL。
可以通过从LoadBitmapFromFile
中删除 ID2D1Bitmap **ppBitmap
参数并仅使用指向&pBitmap
的指针来修复对方法中位图的任何引用。
这样,pBitmap
就会纵,并且不会保持 NULL。
相关文章:
- 如何在快板的屏幕中显示子位图的绘制?
- 尝试绘制矩形(位图)
- 使用C 绘制Linux并创建位图
- Allegro 5如何绘制缩放位图区域
- 另一个从剪贴板线程绘制位图
- 直接2D 加载和绘制位图
- 将用于绘制为位图的像素格式更改
- 位图未在 CFrameWnd 的 OnPaint 中绘制
- 如何在C++中绘制纹理/位图的区域
- 如何在启用Windows Aero主题时在标题栏上绘制位图图标
- GDI+:绘制位图时出现未处理的异常
- BitBlt颠倒绘制位图
- Adafruit gfx库绘制位图没有程序
- 从内存中绘制位图
- c++ MFC:绘制位图到CFrame
- 绘制到GDI+位图
- 如何在c++中绘制位图作为OpenGL纹理
- 直接在对话框中绘制位图
- 是否可以将Windows位图绘制到cairo表面
- 将 OpenGL 绘制到屏幕外的位图