C#有很多示例,但只有一些MSDN上的C++代碼片段。我把它放在一起,我認爲它會工作,但我不確定是否釋放所有必須的COM參考。如何使用C++訪問WriteableBitmap.PixelBuffer像素?
回答
你的代碼是正確的 - 的*buffer
的IBufferByteAccess
接口上的引用計數由呼叫遞增爲QueryInterface
,你必須調用Release
一次釋放該參考。
但是,如果你使用ComPtr<T>
,這變得簡單多了 - 與ComPtr<T>
,你不能調用任何IUnknown
(AddRef
,Release
和QueryInterface
)的三名成員;它阻止你打電話給他們。相反,它以這種方式封裝對這些成員函數的調用,這使得難以理解。下面是如何做到這一點看一個例子:
// Get the buffer from the WriteableBitmap:
IBuffer^ buffer = bitmap->PixelBuffer;
// Convert from C++/CX to the ABI IInspectable*:
ComPtr<IInspectable> bufferInspectable(AsInspectable(buffer));
// Get the IBufferByteAccess interface:
ComPtr<IBufferByteAccess> bufferBytes;
ThrowIfFailed(bufferInspectable.As(&bufferBytes));
// Use it:
byte* pixels(nullptr);
ThrowIfFailed(bufferBytes->Buffer(&pixels));
到bufferInspectable.As(&bufferBytes)
調用執行安全QueryInterface
:它的bufferBytes
類型計算IID,執行QueryInterface
,重視所產生的指針bufferBytes
。當bufferBytes
超出範圍時,它將自動呼叫Release
。代碼與您的代碼具有相同的效果,但沒有容易出錯的顯式資源管理。
的示例使用以下兩種實用程序,這有助於保持代碼的清潔:
auto AsInspectable(Object^ const object) -> Microsoft::WRL::ComPtr<IInspectable>
{
return reinterpret_cast<IInspectable*>(object);
}
auto ThrowIfFailed(HRESULT const hr) -> void
{
if (FAILED(hr))
throw Platform::Exception::CreateException(hr);
}
細心的讀者會發現,因爲這個代碼對IInspectable*
一個ComPtr
我們從buffer
得到,這與原始代碼相比,代碼實際上執行額外的AddRef
/Release
。我認爲這種影響性能的可能性很小,最好從易於驗證爲正確的代碼開始,然後在理解了熱點後優化性能。
這是我試過到目前爲止:
// Get the buffer from the WriteableBitmap
IBuffer^ buffer = bitmap->PixelBuffer;
// Get access to the base COM interface of the buffer (IUnknown)
IUnknown* pUnk = reinterpret_cast<IUnknown*>(buffer);
// Use IUnknown to get the IBufferByteAccess interface of the buffer to get access to the bytes
// This requires #include <Robuffer.h>
IBufferByteAccess* pBufferByteAccess = nullptr;
HRESULT hr = pUnk->QueryInterface(IID_PPV_ARGS(&pBufferByteAccess));
if (FAILED(hr))
{
throw Platform::Exception::CreateException(hr);
}
// Get the pointer to the bytes of the buffer
byte *pixels = nullptr;
pBufferByteAccess->Buffer(&pixels);
// *** Do the work on the bytes here ***
// Release reference to IBufferByteAccess created by QueryInterface.
// Perhaps this might be done before doing more work with the pixels buffer,
// but it's possible that without it - the buffer might get released or moved
// by the time you are done using it.
pBufferByteAccess->Release();
您應該使用'ComPtr
- 1. 如何使用C#訪問攝像頭?
- 2. 如何使用OpenCV在C++中快速訪問像素值
- 3. 訪問元素使用C#
- 4. 使用cv2訪問圖像的像素
- 5. 如何從捕獲的圖像訪問/編輯像素C#
- 6. 使用FreeImage訪問像素數據
- 7. OpenCV使用鼠標訪問像素值
- 8. 使用Mat OpenCV訪問像素
- 9. 訪問像素值OpenCV 2.3 - C++
- 10. 如何從ID3D11Texture2D訪問像素數據?
- 11. 使用C++訪問打開的cv中的圖像元素
- 12. 如何使用pydicom從DICOM文件訪問RGB像素陣列?
- 13. 如何使用jquery從svg圖像訪問內部元素?
- 14. 訪問圖像的像素
- 15. 訪問圖像像素值
- 16. 如何使用jQuery訪問此元素
- 17. 如何使用Javascript訪問iframe元素?
- 18. 如何使用Javascript訪問SVG元素
- 19. SlimDX/DirectX9/C# - 如何訪問紋理中的像素數據
- 20. C++如何訪問元素對象
- 21. 如何在C#中使用Selenium訪問此元素?
- 22. 如何使用循環訪問C#中的表單元素?
- 23. 如何使用C#訪問特定的HTML元素?
- 24. 如何使用c#訪問iframe中的父元素?
- 25. 如何使用SWT訪問攝像頭
- 26. 如何使用JavaScript訪問攝像頭
- 27. 如何使用對象繪製一個像素逐像素C
- 28. 如何使用c複製訪問表#
- 29. 如何使用C#訪問SQLite?
- 30. 如何使用c#訪問dll?
謝謝!這有效,它非常有啓發性! –