#include <d2d1.h>
#include <wincodec.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "windowscodecs.lib")
using D2D1::ColorF;
using D2D1::Point2F;
int main()
{
ID2D1Factory* factory;
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
ID2D1DCRenderTarget* renderTarget;
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
0, 0, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_DEFAULT
);
factory->CreateDCRenderTarget(&props, &renderTarget);
HDC hdc = GetDC(NULL);
RECT rect{ 0, 0, 120, 40 };
renderTarget->BindDC(hdc, &rect);
ID2D1SolidColorBrush* brush = nullptr;
renderTarget->CreateSolidColorBrush(ColorF(ColorF::Black), &brush);
renderTarget->BeginDraw();
renderTarget->Clear(ColorF(ColorF::White));
renderTarget->DrawLine(Point2F(20, 20.5), Point2F(100, 21.5), brush);
// Insert breakpoint here to view the drawing results
renderTarget->EndDraw();
if (renderTarget) renderTarget->Release();
if (factory) factory->Release();
return 0;
}
#include <d2d1.h>
#include <wincodec.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "windowscodecs.lib")
using D2D1::ColorF;
using D2D1::Point2F;
int main()
{
CoInitialize(nullptr);
IWICImagingFactory* wicFactory = nullptr;
ID2D1Factory* d2dFactory = nullptr;
IWICBitmap* wicBitmap = nullptr;
ID2D1RenderTarget* renderTarget = nullptr;
ID2D1SolidColorBrush* brush = nullptr;
IWICBitmapEncoder* encoder = nullptr;
IWICBitmapFrameEncode* frameEncode = nullptr;
IWICStream* stream = nullptr;
CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (void**)&wicFactory);
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2dFactory);
wicFactory->CreateBitmap(120, 40, GUID_WICPixelFormat32bppBGR, WICBitmapCacheOnLoad, &wicBitmap);
d2dFactory->CreateWicBitmapRenderTarget(wicBitmap, D2D1::RenderTargetProperties(), &renderTarget);
renderTarget->CreateSolidColorBrush(ColorF(ColorF::Black), &brush);
renderTarget->BeginDraw();
renderTarget->Clear(ColorF(ColorF::White));
renderTarget->DrawLine(Point2F(20, 20.5), Point2F(100, 21.5), brush);
renderTarget->EndDraw();
wicFactory->CreateStream(&stream);
const WCHAR filename[] = L"output.png";
stream->InitializeFromFilename(filename, GENERIC_WRITE);
wicFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &encoder);
encoder->Initialize(stream, WICBitmapEncoderNoCache);
encoder->CreateNewFrame(&frameEncode, NULL);
frameEncode->Initialize(NULL);
frameEncode->SetSize(120, 40);
WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;
frameEncode->SetPixelFormat(&format);
frameEncode->WriteSource(wicBitmap, NULL);
frameEncode->Commit();
encoder->Commit();
return 0;
}