MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Быстрое преобразование ID3D11Texture2D в management Bitmap"
Answer 719696
На c# не знаю, как это сделать. На с++/cli вот самое лучшее, чего удалось добиться:
System::Drawing::Bitmap ^ ExtractBitmap2(ID3D11Texture2D* d3dtex, ID3D11Device* pDevice) { System::Diagnostics::Stopwatch ^ sw =gcnew System::Diagnostics::Stopwatch(); Int32 t1,t2; HRESULT hr; HBITMAP hBitmapTexture = NULL; HGDIOBJ hBitmap; D3D11_TEXTURE2D_DESC desc; ID3D11Texture2D* pNewTexture = NULL; D3D11_TEXTURE2D_DESC description; sw->Start(); t1=sw->ElapsedMilliseconds; d3dtex->GetDesc(&desc); d3dtex->GetDesc(&description); description.BindFlags = 0; description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; description.Usage = D3D11_USAGE_STAGING; description.MiscFlags = 0; if (FAILED(pDevice->CreateTexture2D(&description, NULL, &pNewTexture))) { Console::WriteLine("CreateTexture2D failed!"); return nullptr; } ID3D11DeviceContext* ctx = NULL; pDevice->GetImmediateContext(&ctx); ctx->CopyResource(pNewTexture, d3dtex); D3D11_MAPPED_SUBRESOURCE resource; UINT subresource = D3D11CalcSubresource(0, 0, 0); ctx->Map(pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource); // Copy from texture to bitmap buffer. uint8_t* sptr = reinterpret_cast<uint8_t*>(resource.pData); uint8_t* dptr = new uint8_t[desc.Width*desc.Height * 4]; UINT* p = NULL; int i; uint32_t x; t2=sw->ElapsedMilliseconds; Console::WriteLine("init time "+Convert::ToString(t2-t1)); t1=sw->ElapsedMilliseconds; for (size_t h = 0; h < desc.Height; ++h) { size_t msize = std::min<size_t>(desc.Width * 4, resource.RowPitch); memcpy_s(dptr, desc.Width * 4, sptr, msize); //// sptr += resource.RowPitch; dptr += desc.Width * 4; } t2=sw->ElapsedMilliseconds; Console::WriteLine("copy time "+Convert::ToString(t2-t1)); dptr -= desc.Width*desc.Height * 4; t1=sw->ElapsedMilliseconds; System::Drawing::Bitmap ^ bmp=gcnew System::Drawing::Bitmap(desc.Width,desc.Height,desc.Width*4, System::Drawing::Imaging::PixelFormat::Format32bppRgb,(IntPtr)dptr); t2=sw->ElapsedMilliseconds; Console::WriteLine("convert time "+Convert::ToString(t2-t1)); return bmp; }
Время: 15-17 мс на Intel Core i3
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.