本文整理汇总了C++中IWICImagingFactory::CreateStream方法的典型用法代码示例。如果您正苦于以下问题:C++ IWICImagingFactory::CreateStream方法的具体用法?C++ IWICImagingFactory::CreateStream怎么用?C++ IWICImagingFactory::CreateStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IWICImagingFactory
的用法示例。
在下文中一共展示了IWICImagingFactory::CreateStream方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: decodeImageData
bool WICImageLoader::decodeImageData(ImageBlob blob, size_t size)
{
bool bRet = false;
HRESULT hr = E_FAIL;
IWICStream* pWicStream = NULL;
IWICImagingFactory* pWicFactory = getWICFactory();
if(NULL != pWicFactory)
{
hr = pWicFactory->CreateStream(&pWicStream);
}
if(SUCCEEDED(hr))
{
hr = pWicStream->InitializeFromMemory((BYTE*)blob, static_cast<DWORD>(size));
}
IWICBitmapDecoder* pDecoder = NULL;
if(SUCCEEDED(hr))
{
hr = pWicFactory->CreateDecoderFromStream(pWicStream, NULL, WICDecodeMetadataCacheOnLoad, &pDecoder);
}
bRet = processImage(pDecoder);
SafeRelease(&pWicStream);
SafeRelease(&pDecoder);
return bRet;
}
示例2: CreateWICTextureFromMemoryEx
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const uint8_t* wicData,
size_t wicDataSize,
size_t maxsize,
D3D11_USAGE usage,
unsigned int bindFlags,
unsigned int cpuAccessFlags,
unsigned int miscFlags,
bool forceSRGB,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView )
{
if ( texture )
{
*texture = nullptr;
}
if ( textureView )
{
*textureView = nullptr;
}
if (!d3dDevice || !wicData || (!texture && !textureView))
return E_INVALIDARG;
if ( !wicDataSize )
return E_FAIL;
#ifdef _M_AMD64
if ( wicDataSize > 0xFFFFFFFF )
return HRESULT_FROM_WIN32( ERROR_FILE_TOO_LARGE );
#endif
IWICImagingFactory* pWIC = _GetWIC();
if ( !pWIC )
return E_NOINTERFACE;
// Create input stream for memory
ComPtr<IWICStream> stream;
HRESULT hr = pWIC->CreateStream( stream.GetAddressOf() );
if ( FAILED(hr) )
return hr;
hr = stream->InitializeFromMemory( const_cast<uint8_t*>( wicData ), static_cast<DWORD>( wicDataSize ) );
if ( FAILED(hr) )
return hr;
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
hr = pWIC->CreateDecoderFromStream( stream.Get(), 0, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf() );
if ( FAILED(hr) )
return hr;
ComPtr<IWICBitmapFrameDecode> frame;
hr = decoder->GetFrame( 0, frame.GetAddressOf() );
if ( FAILED(hr) )
return hr;
hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
texture, textureView );
if ( FAILED(hr))
return hr;
if (texture != 0 && *texture != 0)
{
SetDebugObjectName(*texture, "WICTextureLoader");
}
if (textureView != 0 && *textureView != 0)
{
SetDebugObjectName(*textureView, "WICTextureLoader");
}
return hr;
}
示例3: encodeImageData
bool WICImageLoader::encodeImageData(std::string path, const unsigned char* data, size_t dataLen, WICPixelFormatGUID pixelFormat, int width, int height, GUID containerFormat)
{
assert(data != NULL);
assert(dataLen > 0 && width > 0 && height > 0);
IWICImagingFactory* pFact = getWICFactory();
HRESULT hr = E_FAIL;
IWICStream* pStream = NULL;
if (NULL != pFact) {
hr = pFact->CreateStream(&pStream);
}
if (SUCCEEDED(hr)) {
hr = pStream->InitializeFromFilename(StringUtf8ToWideChar(path).c_str(), GENERIC_WRITE);
}
IWICBitmapEncoder* pEnc = NULL;
if (SUCCEEDED(hr)) {
hr = pFact->CreateEncoder(containerFormat, NULL, &pEnc);
}
if (SUCCEEDED(hr)) {
hr = pEnc->Initialize(pStream, WICBitmapEncoderNoCache);
}
IWICBitmapFrameEncode* pFrame = NULL;
IPropertyBag2* pProp = NULL;
if (SUCCEEDED(hr)) {
hr = pEnc->CreateNewFrame(&pFrame, &pProp);
}
if (SUCCEEDED(hr)) {
hr = pFrame->Initialize(pProp);
}
if (SUCCEEDED(hr)) {
hr = pFrame->SetSize(width, height);
}
if (SUCCEEDED(hr)) {
WICPixelFormatGUID targetFormat = pixelFormat;
hr = pFrame->SetPixelFormat(&targetFormat);
if (targetFormat != pixelFormat) {
hr = E_INVALIDARG;
}
}
if (SUCCEEDED(hr)) {
size_t bpp = getBitsPerPixel(pixelFormat);
size_t stride = (width * bpp + 7) / 8;
hr = pFrame->WritePixels(height, static_cast<UINT>(stride), static_cast<UINT>(dataLen), (BYTE*)data);
}
if (SUCCEEDED(hr)) {
hr = pFrame->Commit();
}
if (SUCCEEDED(hr)) {
hr = pEnc->Commit();
}
SafeRelease(&pStream);
SafeRelease(&pEnc);
SafeRelease(&pFrame);
SafeRelease(&pProp);
return SUCCEEDED(hr);
}
示例4: SaveToImageFile
//.........这里部分代码省略.........
// and write "Hello, World".
pRT->SetTransform(
D2D1::Matrix3x2F::Rotation(
45,
D2D1::Point2F(
rtSize.width / 2,
rtSize.height / 2))
);
static const WCHAR sc_helloWorld[] = L"Hello, World!";
pRT->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
pTextFormat,
D2D1::RectF(0, 0, rtSize.width, rtSize.height),
pBlackBrush);
//
// Reset back to the identity transform
//
pRT->SetTransform(D2D1::Matrix3x2F::Translation(0, rtSize.height - 200));
pRT->FillGeometry(pPathGeometry, pLGBrush);
pRT->SetTransform(D2D1::Matrix3x2F::Translation(rtSize.width - 200, 0));
pRT->FillGeometry(pPathGeometry, pLGBrush);
hr = pRT->EndDraw();
}
if (SUCCEEDED(hr))
{
//
// Save image to file
//
hr = pWICFactory->CreateStream(&pStream);
}
WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;
if (SUCCEEDED(hr))
{
static const WCHAR filename[] = L"output.png";
hr = pStream->InitializeFromFilename(filename, GENERIC_WRITE);
}
if (SUCCEEDED(hr))
{
hr = pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
}
if (SUCCEEDED(hr))
{
hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
}
if (SUCCEEDED(hr))
{
hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->Initialize(NULL);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->SetSize(sc_bitmapWidth, sc_bitmapHeight);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->SetPixelFormat(&format);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->WriteSource(pWICBitmap, NULL);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->Commit();
}
if (SUCCEEDED(hr))
{
hr = pEncoder->Commit();
}
SafeRelease(&pWICFactory);
SafeRelease(&pD2DFactory);
SafeRelease(&pDWriteFactory);
SafeRelease(&pWICBitmap);
SafeRelease(&pRT);
SafeRelease(&pTextFormat);
SafeRelease(&pPathGeometry);
SafeRelease(&pSink);
SafeRelease(&pGradientStops);
SafeRelease(&pLGBrush);
SafeRelease(&pBlackBrush);
SafeRelease(&pEncoder);
SafeRelease(&pFrameEncode);
SafeRelease(&pStream);
return hr;
}
示例5: LoadResourceImage
/// <summary>
/// Load an image from a resource into a buffer
/// </summary>
/// <param name="resourceName">name of image resource to load</param>
/// <param name="resourceType">type of resource to load</param>
/// <param name="nOutputWidth">width (in pixels) of scaled output bitmap</param>
/// <param name="nOutputHeight">height (in pixels) of scaled output bitmap</param>
/// <param name="pOutputBuffer">buffer that will hold the loaded image</param>
/// <returns>S_OK on success, otherwise failure code</returns>
HRESULT CCoordinateMappingBasics::LoadResourceImage(PCWSTR resourceName, PCWSTR resourceType, UINT nOutputWidth, UINT nOutputHeight, RGBQUAD* pOutputBuffer)
{
IWICImagingFactory* pIWICFactory = NULL;
IWICBitmapDecoder* pDecoder = NULL;
IWICBitmapFrameDecode* pSource = NULL;
IWICStream* pStream = NULL;
IWICFormatConverter* pConverter = NULL;
IWICBitmapScaler* pScaler = NULL;
HRSRC imageResHandle = NULL;
HGLOBAL imageResDataHandle = NULL;
void *pImageFile = NULL;
DWORD imageFileSize = 0;
HRESULT hrCoInit = CoInitialize(NULL);
HRESULT hr = hrCoInit;
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pIWICFactory);
}
if (SUCCEEDED(hr))
{
// Locate the resource
imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType);
hr = imageResHandle ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Load the resource
imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageResDataHandle ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Lock it to get a system memory pointer.
pImageFile = LockResource(imageResDataHandle);
hr = pImageFile ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Calculate the size.
imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageFileSize ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Create a WIC stream to map onto the memory.
hr = pIWICFactory->CreateStream(&pStream);
}
if (SUCCEEDED(hr))
{
// Initialize the stream with the memory pointer and size.
hr = pStream->InitializeFromMemory(
reinterpret_cast<BYTE*>(pImageFile),
imageFileSize);
}
if (SUCCEEDED(hr))
{
// Create a decoder for the stream.
hr = pIWICFactory->CreateDecoderFromStream(
pStream,
NULL,
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))
{
hr = pIWICFactory->CreateBitmapScaler(&pScaler);
}
//.........这里部分代码省略.........
示例6: Initialize
bool Image::Initialize(wchar_t *filename, Graphics *graphics)
{
std::ifstream file;
IWICImagingFactory *pWIC;
IWICStream *stream;
IWICBitmapDecoder *decoder;
IWICBitmapFrameDecode *frame;
unsigned int width, height, twidth, theight, maxsize, bpp, i, support, rowPitch, imageSize;
float ratio;
WICPixelFormatGUID pixelFormat;
DXGI_FORMAT format;
IWICComponentInfo *cinfo;
IWICPixelFormatInfo *pfinfo;
IWICBitmapScaler *scaler;
bool autogen = false;
D3D11_TEXTURE2D_DESC desc;
D3D11_SUBRESOURCE_DATA initData;
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
file.open(filename, std::ios::in | std::ios::binary | std::ios::ate);
if(!file.is_open()) return false;
m_size = file.tellg();
file.seekg(0, std::ios::beg);
m_bytes = new unsigned char [m_size];
file.read((char *)m_bytes, m_size);
file.close();
RETURN_FAIL( CoCreateInstance( CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory), (LPVOID*)&pWIC ) );
RETURN_FAIL( pWIC->CreateStream( &stream ) );
RETURN_FAIL( stream->InitializeFromMemory( m_bytes, m_size ) );
RETURN_FAIL( pWIC->CreateDecoderFromStream( stream, 0, WICDecodeMetadataCacheOnDemand, &decoder ) );
RETURN_FAIL( decoder->GetFrame( 0, &frame ) );
RETURN_FAIL( frame->GetSize( &width, &height ) );
SAFE_RELEASE( stream );
SAFE_RELEASE( decoder );
if( width <= 0 || height <= 0 ) return false;
maxsize = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
if( width > maxsize || height > maxsize )
{
ratio = static_cast<float>(height) / static_cast<float>(width);
if(width > height)
{
twidth = maxsize;
theight = static_cast<unsigned int>( static_cast<float>(maxsize) * ratio );
}
else
{
theight = maxsize;
twidth = static_cast<unsigned int>( static_cast<float>(maxsize) / ratio );
}
}
else
{
twidth = width;
theight = height;
}
RETURN_FAIL( frame->GetPixelFormat( &pixelFormat ) );
format = DXGI_FORMAT_UNKNOWN;
for( i = 0; i < _countof(g_WICFormats); ++i )
{
if( memcmp( &g_WICFormats[i].wic, &pixelFormat, sizeof(GUID) ) == 0 )
{
format = g_WICFormats[i].dxgi;
break;
}
}
RETURN_FAIL( pWIC->CreateComponentInfo( pixelFormat, &cinfo ) );
RETURN_FAIL( cinfo->QueryInterface( __uuidof(IWICPixelFormatInfo), (LPVOID*)&pfinfo ) );
RETURN_FAIL( pfinfo->GetBitsPerPixel( &bpp ) );
SAFE_RELEASE( cinfo );
SAFE_RELEASE( pfinfo );
RETURN_FAIL( graphics->GetDevice()->CheckFormatSupport( format, &support ) );
if( !(support & D3D11_FORMAT_SUPPORT_TEXTURE2D) ) return false;
rowPitch = ( twidth * bpp + 7 ) / 8;
imageSize = rowPitch * theight;
std::unique_ptr<uint8_t[]> temp( new (std::nothrow) uint8_t[ imageSize ] );
if( !temp ) return false;
if( twidth == width && theight == height )
{
RETURN_FAIL( frame->CopyPixels( 0, rowPitch, imageSize, temp.get() ) );
}
else
{
RETURN_FAIL( pWIC->CreateBitmapScaler( &scaler ) );
RETURN_FAIL( scaler->Initialize( frame, twidth, theight, WICBitmapInterpolationModeFant ) );
//.........这里部分代码省略.........
示例7: LoadResourceImage
/// <summary>
/// Load an image from a resource into a buffer
/// </summary>
/// <param name="resourceName">name of image resource to load</param>
/// <param name="resourceType">type of resource to load</param>
/// <param name="cOutputBuffer">size of output buffer, in bytes</param>
/// <param name="outputBuffer">buffer that will hold the loaded image</param>
/// <returns>S_OK on success, otherwise failure code</returns>
HRESULT KinectEasyGrabber::LoadResourceImage(
PCWSTR resourceName,
PCWSTR resourceType,
DWORD cOutputBuffer,
BYTE* outputBuffer
)
{
HRESULT hr = S_OK;
IWICImagingFactory* pIWICFactory = NULL;
IWICBitmapDecoder* pDecoder = NULL;
IWICBitmapFrameDecode* pSource = NULL;
IWICStream* pStream = NULL;
IWICFormatConverter* pConverter = NULL;
IWICBitmapScaler* pScaler = NULL;
HRSRC imageResHandle = NULL;
HGLOBAL imageResDataHandle = NULL;
void *pImageFile = NULL;
DWORD imageFileSize = 0;
hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pIWICFactory);
if ( FAILED(hr) ) return hr;
// Locate the resource.
imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType);
hr = imageResHandle ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
// Load the resource.
imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageResDataHandle ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Lock it to get a system memory pointer.
pImageFile = LockResource(imageResDataHandle);
hr = pImageFile ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Calculate the size.
imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageFileSize ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr))
{
// Create a WIC stream to map onto the memory.
hr = pIWICFactory->CreateStream(&pStream);
}
if (SUCCEEDED(hr))
{
// Initialize the stream with the memory pointer and size.
hr = pStream->InitializeFromMemory(
reinterpret_cast<BYTE*>(pImageFile),
imageFileSize
);
}
if (SUCCEEDED(hr))
{
// Create a decoder for the stream.
hr = pIWICFactory->CreateDecoderFromStream(
pStream,
NULL,
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))
{
hr = pIWICFactory->CreateBitmapScaler(&pScaler);
}
//.........这里部分代码省略.........
示例8: SaveToImageFile
//.........这里部分代码省略.........
// pTextFormat,
// D2D1::RectF(0, 0, rtSize.width, rtSize.height),
// pBlackBrush);
float scaleX = (rtSize.width - leftMargin) / rangeX;
float scaleY = (rtSize.height - bottomMargin) / rangeY;
//
// Reset back to the identity transform
//
pRT->DrawLine(D2D1::Point2F(leftMargin, 0.0f), D2D1::Point2F(leftMargin, rtSize.height - bottomMargin), pSolidBrush);
pRT->DrawLine(D2D1::Point2F(leftMargin, rtSize.height - bottomMargin), D2D1::Point2F(rtSize.width, rtSize.height - bottomMargin), pSolidBrush);
pSolidBrush->SetColor(D2D1::ColorF(D2D1::ColorF::LightGray, 0.4f));
size_t countTicks = rangeY / yTick;
for(size_t tick = 1; tick <= countTicks; ++tick)
{
float y = rtSize.height - bottomMargin - static_cast<float>(tick) * yTick * scaleY;
pRT->DrawLine(D2D1::Point2F(leftMargin - 3.0f, y), D2D1::Point2F(rtSize.width, y), pSolidBrush);
}
countTicks = rangeX / xTick;
for(size_t tick = 1; tick <= countTicks; ++tick)
{
float x = leftMargin + static_cast<float>(tick) * xTick * scaleX;
pRT->DrawLine(D2D1::Point2F(x, 0.0f), D2D1::Point2F(x, rtSize.height - bottomMargin + 3.0f), pSolidBrush);
}
//D2D1_MATRIX_3X2_F rotation = D2D1::Matrix3x2F::Rotation(-90.0f);
D2D1_MATRIX_3X2_F translation = D2D1::Matrix3x2F::Translation(leftMargin, rtSize.height - bottomMargin);
//D2D1_MATRIX_3X2_F scale = D2D1::Matrix3x2F::Scale(D2D1::SizeF(600.0f, 1.0f));
pRT->SetTransform(translation);
GraphData(data, pD2DFactory, pRT, pSolidBrush, scaleX, scaleY );
hr = pRT->EndDraw();
}
if (SUCCEEDED(hr))
{
//
// Save image to file
//
hr = pWICFactory->CreateStream(&pStream);
}
WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;
if (SUCCEEDED(hr))
{
static const WCHAR filename[] = L"output.png";
hr = pStream->InitializeFromFilename(filename, GENERIC_WRITE);
}
if (SUCCEEDED(hr))
{
hr = pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
}
if (SUCCEEDED(hr))
{
hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
}
if (SUCCEEDED(hr))
{
hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->Initialize(NULL);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->SetSize(imageWidth, imageHeight);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->SetPixelFormat(&format);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->WriteSource(pWICBitmap, NULL);
}
if (SUCCEEDED(hr))
{
hr = pFrameEncode->Commit();
}
if (SUCCEEDED(hr))
{
hr = pEncoder->Commit();
}
SafeRelease(&pWICFactory);
SafeRelease(&pD2DFactory);
SafeRelease(&pDWriteFactory);
SafeRelease(&pWICBitmap);
SafeRelease(&pRT);
SafeRelease(&pTextFormat);
SafeRelease(&pSolidBrush);
SafeRelease(&pEncoder);
SafeRelease(&pFrameEncode);
SafeRelease(&pStream);
return hr;
}
示例9: encodeImageData
bool WICImageLoader::encodeImageData(std::string path, ImageBlob data, size_t dataLen, WICPixelFormatGUID pixelFormat, int width, int height, GUID containerFormat)
{
assert(data != NULL);
assert(dataLen > 0 && width > 0 && height > 0);
IWICImagingFactory* pFact = getWICFactory();
HRESULT hr = S_FALSE;
IWICStream* pStream = NULL;
if(NULL != pFact)
{
hr = pFact->CreateStream(&pStream);
}
if(SUCCEEDED(hr))
{
std::wstring wpath;
wpath.assign(path.begin(), path.end());
hr = pStream->InitializeFromFilename(wpath.c_str(), GENERIC_WRITE);
}
IWICBitmapEncoder* pEnc = NULL;
if(SUCCEEDED(hr))
{
hr = pFact->CreateEncoder(containerFormat, NULL, &pEnc);
}
if(SUCCEEDED(hr))
{
hr = pEnc->Initialize(pStream, WICBitmapEncoderNoCache);
}
IWICBitmapFrameEncode* pFrame = NULL;
IPropertyBag2* pProp = NULL;
if(SUCCEEDED(hr))
{
hr = pEnc->CreateNewFrame(&pFrame, &pProp);
}
if(SUCCEEDED(hr))
{
hr = pFrame->Initialize(pProp);
}
if(SUCCEEDED(hr))
{
hr = pFrame->SetSize(width, height);
}
if(SUCCEEDED(hr))
{
hr = pFrame->SetPixelFormat(&pixelFormat);
}
if(SUCCEEDED(hr))
{
size_t bpp = getBitsPerPixel(pixelFormat);
size_t stride = (width * bpp + 7) / 8;
hr = pFrame->WritePixels(height, stride, dataLen, (BYTE*)data);
}
if(SUCCEEDED(hr))
{
hr = pFrame->Commit();
}
if(SUCCEEDED(hr))
{
hr = pEnc->Commit();
}
SafeRelease(&pStream);
SafeRelease(&pEnc);
SafeRelease(&pFrame);
SafeRelease(&pProp);
return SUCCEEDED(hr);
}
示例10: _tmain
//.........这里部分代码省略.........
pSink->AddBezier(
D2D1::BezierSegment(
D2D1::Point2F(50, 150),
D2D1::Point2F(50, 50),
D2D1::Point2F(0, 0))
);
pSink->EndFigure(D2D1_FIGURE_END_CLOSED);
CHECKHR(pSink->Close());
// Create GradientStopCollection
ID2D1GradientStopCollection *pGradientStops = NULL;
static const D2D1_GRADIENT_STOP stops[] =
{
{ 0.f, { 0.f, 1.f, 1.f, 1.f } },
{ 1.f, { 0.f, 0.f, 1.f, 1.f } },
};
CHECKHR(g_pRenderTarget->CreateGradientStopCollection(
stops,
ARRAYSIZE(stops),
&pGradientStops
));
ID2D1LinearGradientBrush* pLGBrush = NULL;
ID2D1SolidColorBrush* pBlackBrush = NULL;
CHECKHR(g_pRenderTarget->CreateLinearGradientBrush(
D2D1::LinearGradientBrushProperties(
D2D1::Point2F(100, 0),
D2D1::Point2F(100, 200)),
D2D1::BrushProperties(),
pGradientStops,
&pLGBrush
));
CHECKHR(g_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&pBlackBrush
));
// Render into the bitmap.
g_pRenderTarget->BeginDraw();
g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
D2D1_SIZE_F rtSize = g_pRenderTarget->GetSize();
// Set the world transform to a 45 degree rotation at the center of the render target
// and write "Hello, World".
g_pRenderTarget->SetTransform(
D2D1::Matrix3x2F::Rotation(
45,
D2D1::Point2F(
rtSize.width / 2,
rtSize.height / 2))
);
static const WCHAR sc_helloWorld[] = L"Hello, World!";
g_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
pTextFormat,
D2D1::RectF(0, 0, rtSize.width, rtSize.height),
pBlackBrush);
// Reset back to the identity transform.
g_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(0, rtSize.height - 200));
g_pRenderTarget->FillGeometry(pPathGeometry, pLGBrush);
g_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(rtSize.width - 200, 0));
g_pRenderTarget->FillGeometry(pPathGeometry, pLGBrush);
CHECKHR(g_pRenderTarget->EndDraw());
// Save the image to a file.
IWICStream* pStream = NULL;
CHECKHR(pWICFactory->CreateStream(&pStream));
// Use InitializeFromFilename to write to a file. If there is need to write inside the memory, use InitializeFromMemory.
static const WCHAR filename[] = L"output.png";
CHECKHR(pStream->InitializeFromFilename(filename, GENERIC_WRITE));
IWICBitmapEncoder* pEncoder = NULL;
CHECKHR(pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder));
CHECKHR(pEncoder->Initialize(pStream, WICBitmapEncoderNoCache));
IWICBitmapFrameEncode* pFrameEncode = NULL;
CHECKHR(pEncoder->CreateNewFrame(&pFrameEncode, NULL));
// Use IWICBitmapFrameEncode to encode the bitmap into the picture format you want.
CHECKHR(pFrameEncode->Initialize(NULL));
CHECKHR(pFrameEncode->SetSize(sc_bitmapWidth, sc_bitmapHeight));
WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;
CHECKHR(pFrameEncode->SetPixelFormat(&format));
CHECKHR(pFrameEncode->WriteSource(pWICBitmap, NULL));
CHECKHR(pFrameEncode->Commit());
CHECKHR(pEncoder->Commit());
return 0;
}