当前位置:网站首页>D2dengine edible tutorial (3) -- export rendering targets as image files
D2dengine edible tutorial (3) -- export rendering targets as image files
2022-07-24 03:48:00 【dgaf】
Catalog
D2DEngine Resource link :https://blog.csdn.net/DGAF2198588973/article/details/125909836
The goal is : Draw the picture on the render target , And export the image on the rendering target as a file

Direct2D API It is not difficult to , however , Because there is no Internet like Win32 API Such a large amount of detailed information , Once its in-depth study , It will be difficult to walk , The only way is to look for a needle in the sea of official Microsoft documents .
about How to export an image in a render target to a file , There are few online tutorials
—————————————————————————————————————————————————————
Windows Window client area and non client area

With the classic Windows7 The ground glass window is an example
Pictured , Green border —— Yellow border ( Including the minimization of the upper right corner , Maximize , close button ) by Non customer area ( The response message WM_NCPAINT) . The non client area accounts for a certain size of the total size of the window . Control creation , Draw operation You can't In non customer areas , When the window properties change , Will produce WM_NCPAINT news , Only then can the non customer area be operated .
Inside the yellow border ( Grey areas ) by Customer area ( The response message WM_PAINT) . Control , You can draw here .
Direct2D Most of the drawings are in Customer area Drawing ( Including previous examples )
Render Target
As shown in the red box , Here, set the rendering target to the whole window , The image covers the whole customer area
As shown in the figure, the red box is the rendering target , Painting beyond the rendered area will bring " beat all " Result
Render Target It is a set of resources , Contains Render area and Rendering devices ( Drawing board , A brush , Palette and so on ) , All operations related to rendering and painting are inseparable from the rendering target . The following figure shows their relationship .

—————————————————————————————————————————————————————
| D2DEngine Member functions | effect |
|---|---|
| bool PackRenderTargetAsImageFile (Pathname,Filename,Transformat); | Package rendering target Finished drawing Image , Become a bitmap file , Path error or occurrence COM The error will return false, Parameters :Pathname Pathname ,Filename file name ,Transformat Format to convert ( File extension ) , Between path name and file name " \ " It will automatically add |
D2DPackImage.CPP
#include<windows.h>
#include"D2D_DRAW.h"
using namespace std;
using namespace D2D1;
LRESULT CALLBACK CallBackFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hin, HINSTANCE, LPSTR, int CmdShow)
{
MSG msg = {
};
WNDCLASSEX wcex = {
};
memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = CallBackFunc;
wcex.lpszClassName = L"D2D";
wcex.hInstance = hin;
wcex.style = CS_GLOBALCLASS;
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
RegisterClassEx(&wcex);
HWND hwnd = CreateWindowW(wcex.lpszClassName, L"D2D",
WS_OVERLAPPED | WS_SYSMENU,
300, 0, 640, 480,
nullptr,
nullptr,
hin,
nullptr
);
ShowWindow(hwnd, CmdShow);
D2DEngine* Engine = new D2DEngine; // establish D2DEngine object
Picture Photo(L" Cao Cao .jpeg"); // Store in this folder Cao Cao .jpeg Picture path for
Engine->Initialize(hwnd); // initialization D2DEngine equipment , The render target specifies the entire window client area by default
Engine->TransformPictureIntoBitmap(Photo, SINGLE_FRAME); // Picture format conversion
Engine->BeginDraw(); // Began to draw
Engine->DrawPicture(Photo, 0, 0, 640, 480); // Draw the picture on the client area of the window
Engine->EndDraw(); // Stop drawing
// thus , D2DEngine Finished drawing
Engine->PackRenderTargetAsImageFile
(L"", L" Cao Cao ", Picture_Format::BMP);
// Intercept the entire window ( Because no rendering target is specified ) , And convert the image of the window into an image file
// Because the pathname is not specified , The program will create a program called Cao Cao .bmp The image file of is exported to its local disk
// You can go and see if there are any more local disks
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
attach :PackRenderTargetAsImageFile Realization principle of function ( The code is longer than the , You can skip )
The following code is taken from D2D_DRAW.CPP
// D2D_DRAW.CPP
namespace D2D_DRAW_BITMAP_FORMAT_MATCH // The content is only available in this file
{
const struct // For image format comparison
{
Picture_Format _format_;
GUID _GUID_format_;
WCHAR* _Extension_;
}
_match_format_table_[] =
{
{
Picture_Format::BMP, GUID_ContainerFormatBmp, L".bmp" },
{
Picture_Format::PNG, GUID_ContainerFormatPng, L".png" },
{
Picture_Format::JPEG, GUID_ContainerFormatJpeg, L".jpeg"},
{
Picture_Format::DDS, GUID_ContainerFormatDds, L".dds" },
{
Picture_Format::JPG, GUID_ContainerFormatJpeg, L".jpg" },
{
Picture_Format::ADNG, GUID_ContainerFormatAdng, L".adng"},
{
Picture_Format::HEIF, GUID_ContainerFormatHeif, L".heif"},
{
Picture_Format::RAW, GUID_ContainerFormatRaw, L".raw" },
{
Picture_Format::WEBP, GUID_ContainerFormatWebp, L".webp"},
{
Picture_Format::GIF, GUID_ContainerFormatGif, L".gif" },
{
Picture_Format::ICO, GUID_ContainerFormatIco, L".ico" },
{
Picture_Format::WMP, GUID_ContainerFormatWmp, L".wmp" },
{
Picture_Format::TIFF, GUID_ContainerFormatTiff, L".tiff"}
};
}
// Package the image on the render target , Become a bitmap file
bool D2DEngine::PackRenderTargetAsImageFile(WCHAR* Pathname,
WCHAR* Filename, Picture_Format TransformFormat)
{
// About "D2D Bitmap Export " There are few online tutorials
// The big hole left by Microsoft ,MSDN The official tutorial is useless ( because FilePacker need WinRT System )
/* The process : 1. Copy a temporary bitmap from the original rendering target (ID2D1Bitmap) 2. Create an export WIC Bitmap (IWICBitmap), And a WIC Render Target (ID2D1RenderTarget) And will WIC Bitmap bound to WIC Render on target 3. Draw a temporary bitmap to WIC Render on target 4. Create file stream , And initialize with file path 5. establish Encoder equipment , Set up Encoder The path and frame that will be written to the file (EncodeFrame) 6. establish EncodeFrame equipment , Bind and write a frame bitmap (WICBitmap) 7. End writing , Close file ,Encoder and EncodeFrame Conduct Commit operation Be careful : The original render target is the same as WIC Render the renderer target on the target (RenderTargetType) Must be software! ( because WIC Only software rendering is supported ) */
try
{
D2DDestRenderTarget.Reset();
WICBitmap.Reset();
WICStream.Reset();
WICEncoder.Reset();
WICEncodeFrame.Reset();
Microsoft::WRL::ComPtr<ID2D1Bitmap> _ExtraBitmap;
UINT _RTw = D2DRenderTarget->GetSize().width;
UINT _RTh = D2DRenderTarget->GetSize().height;
WICPixelFormatGUID _WICformat = GUID_WICPixelFormatDontCare;
D2D1_POINT_2U _destPoint = D2D1::Point2U();
D2D1_RECT_U _Rect = D2D1::RectU(0, 0, _RTw, _RTh);
D2D1_RECT_F _Rect2 = D2D1::RectF(0, 0, _RTw, _RTh);
// 1. Copy a temporary bitmap from the original rendering target (ID2D1Bitmap)
D2DRenderTarget->CreateBitmap(D2D1_SIZE_U{
_RTw, _RTh },
D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED)), &_ExtraBitmap);
_ExtraBitmap->CopyFromRenderTarget(&_destPoint, D2DRenderTarget.Get(), &_Rect);
// 2. Create an export WIC Bitmap (IWICBitmap), And a WIC Render Target (ID2D1RenderTarget)
// And will WIC Bitmap bound to WIC Render on target
WICFactory->CreateBitmap(_RTw, _RTh, GUID_WICPixelFormat32bppPBGRA,
WICBitmapCacheOnDemand, &WICBitmap);
D2DFactory->CreateWicBitmapRenderTarget(WICBitmap.Get(),
D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_SOFTWARE,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)),
&D2DDestRenderTarget);
// 3. Draw a temporary bitmap to WIC Render on target
D2DDestRenderTarget->BeginDraw();
D2DDestRenderTarget->Clear(0);
D2DDestRenderTarget->DrawBitmap(_ExtraBitmap.Get(), _Rect2, 1.0f,
D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,_Rect2);
D2DDestRenderTarget->EndDraw();
// 4. Create file stream , And initialize with file path
WICFactory->CreateStream(&WICStream);
size_t _result = 0;
while (true)
{
if (_result == sizeof(D2D_DRAW_BITMAP_FORMAT_MATCH::_match_format_table_))
{
return false;
}
if (D2D_DRAW_BITMAP_FORMAT_MATCH::_match_format_table_[_result]._format_ == TransformFormat)
{
break;
}
_result++;
}
std::wstring _returnfilename = std::wstring(Pathname) + std::wstring(L"\\") +
std::wstring(Filename) +
std::wstring(D2D_DRAW_BITMAP_FORMAT_MATCH::_match_format_table_[_result]._Extension_);
WCHAR* ReturnFileName = &_returnfilename[0];
WICStream->InitializeFromFilename(ReturnFileName, GENERIC_WRITE);
// 5. establish Encoder equipment , Set up Encoder The path and frame that will be written to the file (EncodeFrame)
WICFactory->CreateEncoder(D2D_DRAW_BITMAP_FORMAT_MATCH::_match_format_table_[_result]._GUID_format_, nullptr, &WICEncoder);
WICEncoder->Initialize(WICStream.Get(), WICBitmapEncoderNoCache);
WICEncoder->CreateNewFrame(&WICEncodeFrame, nullptr);
// 6. establish EncodeFrame equipment , Bind and write a frame bitmap (WICBitmap)
WICEncodeFrame->Initialize(nullptr);
WICEncodeFrame->SetSize(_RTw, _RTh);
WICEncodeFrame->SetPixelFormat(&_WICformat);
WICEncodeFrame->WriteSource(WICBitmap.Get(), nullptr);
// 7. End writing , Close file ,Encoder and EncodeFrame Conduct Commit operation
WICEncodeFrame->Commit();
WICEncoder->Commit();
}
catch (D2DException& Error)
{
return false;
}
return true;
}
Big God link :
1.Direct2D Save the current renderer target content as a bitmap
2.Direct2D course V—— Bitmap (Bitmap) And bitmap brushes (BitmapBrush)
Like it
边栏推荐
- STL set container
- Sword finger offer | minimum number of rotation array
- 6-14 vulnerability exploitation rpcbind vulnerability exploitation
- [untitled]
- The local picture cannot be displayed after the uniapp H5 is packaged
- Complex nested object pool (5) -- unified management and expansion of object pool
- 。 When calling, the incoming t refers to the Devi pass, which basically asks T2,
- Problem solution of supporting problem solution of heavy chain dissection example
- Svg image color modification is not fancy
- Conteneur STL set
猜你喜欢

PAT甲级 1040 Longest Symmetric String

Database foundation and installation

Solution to the problem of "out of sight, out of mind, out of order" in the training competition

Pat grade a 1043 is it a binary search tree

4. Hezhou air32f103_ LCD

Genesis public chain: Tamp the foundation of Web 3.0 development

Internet of things installation and debugging personnel let "smart" life come early

Shengsi YiDianTong | deep learning analysis of classical convolutional neural network

Worthington: characteristics and other parameters of hexokinase from yeast

93. (leaflet chapter) leaflet situation plotting - modification of attack direction
随机推荐
Application of motion capture in automatic control field
Opening soon | openatom openharmony sub forum of 2022 open atom global open source summit "interconnection of all things, enabling thousands of industries"
The progress in the stack will consume functions that cannot meet the needs of the enterprise. We are committed to
Redis transaction learning
Basic syntax of MySQL DDL and DML and DQL
Preliminary exploration of Flink principle and flow batch integration API (II) V2
Redis
Leetcode 204. count prime number (2022.07.23)
Sqlserver backup restore
[untitled]
Why do some people write code so complicated?
Master chip csu18m92 develops intelligent scale scheme
6-14 vulnerability exploitation rpcbind vulnerability exploitation
Problem solution of supporting problem solution of heavy chain dissection example
Y74. Chapter IV Prometheus large factory monitoring system and practice -- Introduction to promql and monitoring pod resources (V)
Prosci Lag3 antibody: improve in vitro research and help cancer immunotherapy
Exercices classiques de langue C (2) - « tri des bulles »
Using global data to realize data sharing in wechat applet
leetcode hot 100(刷題篇8)(232/88/451/offer10/offer22/344/)
Shengsi YiDianTong | deep learning analysis of classical convolutional neural network