{"id":3454,"date":"2012-05-24T15:29:03","date_gmt":"2012-05-23T21:29:03","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/3454"},"modified":"2012-05-24T15:32:08","modified_gmt":"2012-05-24T06:32:08","slug":"win8-%e7%94%bb%e5%83%8f%e5%8a%a0%e5%b7%a5%e3%82%92directx%e3%81%ab%e4%bb%bb%e3%81%9b%e3%81%a6%e3%80%81ui%e3%81%afc%e3%81%ab%e3%81%99%e3%82%8b%e6%8a%80","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/3454","title":{"rendered":"[win8] \u753b\u50cf\u52a0\u5de5\u3092DirectX\u306b\u4efb\u305b\u3066\u3001UI\u306fC#\u306b\u3059\u308b\u6280"},"content":{"rendered":"<p>[win8] \u30b9\u30bf\u30fc\u30c8\u753b\u9762\u306e\u30bf\u30a4\u30eb\u3092\u52d5\u7684\u306b\u4f5c\u6210\u3059\u308b\uff08\u524d\u54e8\u6226\uff09 | Moonmile Solutions Blog<br \/>\n<a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3443\">http:\/\/www.moonmile.net\/blog\/archives\/3443<\/a><\/p>\n<p>\u304b\u3089\u3061\u3087\u3063\u3068\u6a2a\u9053\u306b\u9038\u308c\u3066\u3001\u753b\u50cf\u30c7\u30fc\u30bf\uff08BitmapImage\uff09\u306b\u76f4\u63a5\u66f8\u304d\u8fbc\u3080\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\nmetro \u306e Image \u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u306b\u306f Source \u30d7\u30ed\u30d1\u30c6\u30a3\u306b BitmapImage \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u76f4\u63a5\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002BitmapImage \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u6307\u5b9a\u3082\u3067\u304d\u308b\u306e\u3067\u3059\u304c\u3001\u76f4\u63a5\u30e1\u30e2\u30ea\u304b\u3089\u4f5c\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3001\u3063\u3066\u3053\u3068\u3067\u3059\u3002\u5358\u7d14\u306a\u3068\u3053\u308d\u3067\u306f\u3001\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nprivate async void Button_Click_2(object sender, RoutedEventArgs e)<br \/>\n{<br \/>\n\tvar mem = new InMemoryRandomAccessStream();<br \/>\n\tvar enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, mem);<\/p>\n<p>\tint width = 140, height = 140;<br \/>\n\tint plane = 4;<br \/>\n\tbyte[] data = new byte[width * height * plane];<br \/>\n\tint offset = 0;<br \/>\n\tfor (int y = 0; y &lt; width; y++)<br \/>\n\t{<br \/>\n\t\tfor (int x = 0; x &lt; height; x++)<br \/>\n\t\t{<br \/>\n\t\t\tdata[offset + 0] = 0xFF; \/\/ R<br \/>\n\t\t\tdata[offset + 1] = 0x00; \/\/ G<br \/>\n\t\t\tdata[offset + 2] = 0x00; \/\/ B<br \/>\n\t\t\tdata[offset + 3] = 0xFF; \/\/ Alpha<br \/>\n\t\t\toffset += 4;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tenc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight,<br \/>\n\t\t(uint)width, (uint)height, 96, 96, data);<br \/>\n\tawait enc.FlushAsync();<\/p>\n<p>\tBitmapImage bmp = new BitmapImage();<br \/>\n\tbmp.SetSource(mem);<br \/>\n\timage1.Source = bmp;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001140&#215;140 \u306e\u77e9\u5f62\u304c\u8d64\u3067\u5857\u308a\u3064\u3076\u3055\u308c\u307e\u3059\uff08\u3053\u308c\u81ea\u4f53\u306e\u30b5\u30f3\u30d7\u30eb\u304c\u3042\u305f\u306e\u3067\u3059\u304c\u3001URL\u3092\u5fd8\u5374\u3002\u898b\u3064\u3051\u305f\u3089\u8a18\u9332\u3057\u3066\u304a\u304d\u307e\u3059\uff09\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120524_01.jpg\" border=\"0\" \/><\/p>\n<p>\u30e1\u30e2\u30ea\u4e0a\u3067\u64cd\u4f5c\u3059\u308b\u5834\u5408\u306f\u3001byte[] \u3068 InMemoryRandomAccessStream \u3092\u4f7f\u3044\u307e\u3059\u3002\u3053\u306e\u3042\u305f\u308a\u3044\u304f\u3064\u304b\u306e\u65b9\u6cd5\u304c\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u7d50\u8ad6\u304b\u3089\u8a00\u3048\u3070\u300c\u65b9\u6cd5\u300d\u81ea\u4f53\u306f\u3069\u3046\u3067\u3082\u3088\u304f\u3066\u3001.NET Framework \u306e\u3088\u3046\u306b System.Drawing \u304c\u4f7f\u3048\u308b\u3068\u4fbf\u5229\u306a\u3093\u3067\u3059\u3051\u3069\u306d\u3047\u3001\u3068\u3044\u3046\u5177\u5408\u3067\u3059\u3002<\/p>\n<p>\u3053\u3093\u306a\u98a8\u306b\u8272\u3092\u5857\u308a\u3064\u3076\u3059\u3060\u3051\u306a\u3089\u3070\u7c21\u5358\u306b\u3067\u308b\u3057\u3001\u753b\u50cf\u3092\u8cbc\u308a\u3064\u3051\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002\u56de\u8ee2\u306a\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3082\u3042\u3063\u305f\u306e\u3067\u3001\u305d\u306e\u304f\u3089\u3044\u306f\u3067\u304d\u305d\u3046\u306a\u306e\u3067\u3059\u304c\u3001\u6839\u672c\u7684\u306b\u6a5f\u80fd\u4e0d\u8db3\u306f\u5426\u3081\u307e\u305b\u3093\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u5148\u306e\u8a18\u4e8b\u306e\u3088\u3046\u306b DirectX \u3092\u76f4\u63a5\u6271\u3044\u307e\u3059\u3002 DirectX \u306f C++\/CX \u304b\u3089\u6271\u3046\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001C#\u3084VB\u306e\u30d7\u30ed\u30b0\u30e9\u30de\u306b\u306f\u8f9b\u3044\u3068\u3053\u308d\u304c\u3042\u308a\u307e\u3059\u3002\u307e\u305f\u3001C++\/CX \u304b\u3089\u76f4\u63a5.NET Framework \u3092\u6271\u3048\u306a\u3044\uff08C#\u3092\u7d4c\u7531\u3057\u3066\u6271\u3046\u3053\u3068\u306f\u53ef\u80fd\uff09\u306a\u306e\u3067\u3001\u3053\u308c\u3082\u75db\u3057\u304b\u3086\u3057\u3068\u3044\u3046\u5177\u5408\u3067\u3059\u3002\u306a\u306e\u3067\u3001\u672c\u683c\u7684\u306a\u30b2\u30fc\u30e0\u3092\u4f5c\u308b\u5834\u5408\u306b\u306f\u5168\u753b\u9762\u3092 DirectX \u3092\u4f7f\u3046\u306e\u304c\u826f\u3044\u306e\u3067\u3057\u3087\u3046\u304c\u3001\u3061\u3087\u3063\u3068\u3057\u305f\u30d1\u30ba\u30eb\u30b2\u30fc\u30e0\u3084\u753b\u50cf\u306e\u52a0\u5de5\u30c4\u30fc\u30eb\u3050\u3089\u3044\u306a\u3089\u3070\u3001<\/p>\n<ul>\n<li>DirectX \u3092\u6271\u3046\u753b\u50cf\u52a0\u5de5\u306f C++\/CX \u306b\u62bc\u3057\u8fbc\u3081\u308b<\/li>\n<li>UI \u3092\u6271\u3046\u90e8\u5206\u306f C# \u3067\u6271\u3046<\/li>\n<\/ul>\n<p>\u5206\u3051\u65b9\u3092\u3059\u308b\u307b\u3046\u304c\u30d9\u30bf\u30fc\u3067\u3059\u3002<\/p>\n<p>\u3072\u3068\u307e\u305a\u3001\u5168\u30bd\u30fc\u30b9\u306f github \u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>win8\/DynamicMakeWritableBitmap<br \/>\n<a href=\"https:\/\/github.com\/moonmile\/win8\/tree\/master\/DynamicMakeWritableBitmap\">https:\/\/github.com\/moonmile\/win8\/tree\/master\/DynamicMakeWritableBitmap<\/a><\/p>\n<p>\u4f59\u8a08\u306a\u3068\u3053\u308d\u306f\u968f\u5206\u524a\u3063\u305f\u3064\u3082\u308a\u306a\u306e\u3067\u3059\u304c\u3001\u307e\u3060\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u3059\u308b\u306b\u306f\u5197\u9577\u306a\u611f\u3058\u306a\u306e\u3067\u3001\u3044\u307e\u3044\u3061\u306a\u306e\u3067\u3059\u304c\u3002Direct2D \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u308b\u6642\u306b\u81ea\u52d5\u4f5c\u6210\u3055\u308c\u308b DirectXBase.* \u304b\u3089\u629c\u304d\u51fa\u3057\u3066\u304d\u305f\u306e\u304c\u3001\u6b21\u306e\u30bd\u30fc\u30b9\u3067\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\n#pragma once<\/p>\n<p>namespace DX<br \/>\n{<br \/>\n    inline void ThrowIfFailed(HRESULT hr)<br \/>\n    {<br \/>\n        if (FAILED(hr))<br \/>\n        {<br \/>\n            \/\/ Set a breakpoint on this line to catch DirectX API errors<br \/>\n            throw Platform::Exception::CreateException(hr);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>namespace MakeBitmapLib<br \/>\n{<br \/>\n    public ref class DXBitmap sealed<br \/>\n    {<br \/>\n    public:<br \/>\n        DXBitmap();<br \/>\n\t\tvoid Initialize();<br \/>\n\t\tPlatform::Array&lt;unsigned char&gt;^ MakeText( Platform::String^ text );<\/p>\n<p>protected:<br \/>\n    \/\/ Direct2D Objects<br \/>\n    Microsoft::WRL::ComPtr&lt;ID2D1Factory1&gt;          m_d2dFactory;<br \/>\n    Microsoft::WRL::ComPtr&lt;ID2D1Device&gt;            m_d2dDevice;<br \/>\n    Microsoft::WRL::ComPtr&lt;ID2D1DeviceContext&gt;     m_d2dContext;<br \/>\n    Microsoft::WRL::ComPtr&lt;ID2D1Bitmap1&gt;           m_d2dTargetBitmap;<\/p>\n<p>    \/\/ DirectWrite &amp; Windows Imaging Component Objects<br \/>\n    Microsoft::WRL::ComPtr&lt;IDWriteFactory1&gt;        m_dwriteFactory;<br \/>\n    Microsoft::WRL::ComPtr&lt;IWICImagingFactory2&gt;    m_wicFactory;<\/p>\n<p>    \/\/ Direct3D Objects<br \/>\n    Microsoft::WRL::ComPtr&lt;ID3D11Device1&gt;          m_d3dDevice;<br \/>\n    Microsoft::WRL::ComPtr&lt;ID3D11DeviceContext1&gt;   m_d3dContext;<br \/>\n    Microsoft::WRL::ComPtr&lt;IDXGISwapChain1&gt;        m_swapChain;<br \/>\n    Microsoft::WRL::ComPtr&lt;ID3D11RenderTargetView&gt; m_renderTargetView;<br \/>\n    Microsoft::WRL::ComPtr&lt;ID3D11DepthStencilView&gt; m_depthStencilView;<\/p>\n<p>\tD3D_FEATURE_LEVEL                              m_featureLevel;<br \/>\n    Windows::Foundation::Size                      m_renderTargetSize;<br \/>\n    Windows::Foundation::Rect                      m_windowBounds;<br \/>\n    Windows::UI::Core::CoreWindow^                 m_window;<br \/>\n    float                                          m_dpi;<br \/>\n\t};<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>protected \u306a\u3068\u3053\u308d\u306f\u3001DirectX \u95a2\u4fc2\u3067\u89e6\u308b COM \u3067\u3059\u306d\u3002\u4f59\u5206\u306a\u3068\u3053\u308d\u3082\u521d\u671f\u5316\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u304c\u3001\u307e\u3041\u3001\u305d\u306e\u307e\u307e\u30b3\u30d4\u30fc\u3057\u3066\u304d\u6301\u3063\u3066\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e DirectX \u3092\u6271\u3046\u70ba\u306e\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u3092 pch.h \u306b\u5165\u308c\u3066\u304a\u304d\u307e\u3059\u3002pch.h \u306f\u30d7\u30ea\u30b3\u30f3\u30d1\u30a4\u30eb\u30d8\u30c3\u30c0\u306a\u306e\u3067\u3001\u56fa\u5b9a\u5316\u3055\u308c\u3066\u3044\u308b\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u3092\u7a81\u3063\u8fbc\u3093\u3067\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\n#pragma once<br \/>\n&lt;\/p&gt;<br \/>\n&lt;p&gt;<br \/>\n#include &lt;wrl.h&gt;<br \/>\n#include &lt;d3d11_1.h&gt;<br \/>\n#include &lt;d2d1_1.h&gt;<br \/>\n#include &lt;d2d1effects.h&gt;<br \/>\n#include &lt;dwrite_1.h&gt;<br \/>\n#include &lt;wincodec.h&gt;<br \/>\n[\/code]<\/p>\n<p>C++ \u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u6b21\u306a\u611f\u3058\u3067\u3059\u306d\u3002Initialize \u30e1\u30bd\u30c3\u30c9\u3067\u521d\u671f\u5316\u3057\u3066\u304a\u3044\u3066\u3001MakeText \u30e1\u30bd\u30c3\u30c9\u3067\u753b\u50cf\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002Initialize \u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u8eab\u306f DirectXBase.cpp \u306e\u3082\u306e\u3092\u9069\u5b9c\u30b3\u30d4\u30fc\u3057\u3066\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\n\/\/ WinRTComponent.cpp<br \/>\n#include &quot;pch.h&quot;<br \/>\n#include &quot;WinRTComponent.h&quot;<\/p>\n<p>using namespace MakeBitmapLib;<br \/>\nusing namespace Platform;<br \/>\nusing namespace Microsoft::WRL;<br \/>\nusing namespace Windows::UI::Core;<br \/>\nusing namespace Windows::Foundation;<br \/>\nusing namespace D2D1;<br \/>\nusing namespace Windows::Storage::Streams;<\/p>\n<p>DXBitmap::DXBitmap()<br \/>\n{<br \/>\n}<br \/>\nvoid DXBitmap::Initialize()<br \/>\n{<br \/>\n\t\/\/ CreateDeviceIndependentResources<br \/>\n    D2D1_FACTORY_OPTIONS options;<br \/>\n    ZeroMemory(&amp;options, sizeof(D2D1_FACTORY_OPTIONS));<\/p>\n<p>#if defined(_DEBUG)<br \/>\n     \/\/ If the project is in a debug build, enable Direct2D debugging via SDK Layers.<br \/>\n    options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;<br \/>\n#endif<\/p>\n<p>    DX::ThrowIfFailed(<br \/>\n        D2D1CreateFactory(<br \/>\n            D2D1_FACTORY_TYPE_SINGLE_THREADED,<br \/>\n            __uuidof(ID2D1Factory1),<br \/>\n            &amp;options,<br \/>\n            &amp;m_d2dFactory<br \/>\n            )<br \/>\n        );<\/p>\n<p>    DX::ThrowIfFailed(<br \/>\n        DWriteCreateFactory(<br \/>\n            DWRITE_FACTORY_TYPE_SHARED,<br \/>\n            __uuidof(IDWriteFactory),<br \/>\n            &amp;m_dwriteFactory<br \/>\n            )<br \/>\n        );<\/p>\n<p>\tDX::ThrowIfFailed(<br \/>\n        CoCreateInstance(<br \/>\n            CLSID_WICImagingFactory,<br \/>\n            nullptr,<br \/>\n            CLSCTX_INPROC_SERVER,<br \/>\n            IID_PPV_ARGS(&amp;m_wicFactory)<br \/>\n            )<br \/>\n        );<\/p>\n<p>    \/\/ This flag adds support for surfaces with a different color channel ordering than the API default.<br \/>\n    \/\/ It is recommended usage, and is required for compatibility with Direct2D.<br \/>\n    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;<\/p>\n<p>#if defined(_DEBUG)<br \/>\n    \/\/ If the project is in a debug build, enable debugging via SDK Layers with this flag.<br \/>\n    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;<br \/>\n#endif<\/p>\n<p>    \/\/ This array defines the set of DirectX hardware feature levels this app will support.<br \/>\n    \/\/ Note the ordering should be preserved.<br \/>\n    \/\/ Don&#8217;t forget to declare your application&#8217;s minimum required feature level in its<br \/>\n    \/\/ description.  All applications are assumed to support 9.1 unless otherwise stated.<br \/>\n    D3D_FEATURE_LEVEL featureLevels[] =<br \/>\n    {<br \/>\n        D3D_FEATURE_LEVEL_11_1,<br \/>\n        D3D_FEATURE_LEVEL_11_0,<br \/>\n        D3D_FEATURE_LEVEL_10_1,<br \/>\n        D3D_FEATURE_LEVEL_10_0,<br \/>\n        D3D_FEATURE_LEVEL_9_3,<br \/>\n        D3D_FEATURE_LEVEL_9_2,<br \/>\n        D3D_FEATURE_LEVEL_9_1<br \/>\n    };<\/p>\n<p>    \/\/ Create the DX11 API device object, and get a corresponding context.<br \/>\n    ComPtr&lt;ID3D11Device&gt; d3dDevice;<br \/>\n    ComPtr&lt;ID3D11DeviceContext&gt; d3dContext;<br \/>\n    DX::ThrowIfFailed(<br \/>\n        D3D11CreateDevice(<br \/>\n            nullptr,                  \/\/ specify null to use the default adapter<br \/>\n            D3D_DRIVER_TYPE_HARDWARE,<br \/>\n            nullptr,                  \/\/ leave as nullptr unless software device<br \/>\n            creationFlags,            \/\/ optionally set debug and Direct2D compatibility flags<br \/>\n            featureLevels,            \/\/ list of feature levels this app can support<br \/>\n            ARRAYSIZE(featureLevels), \/\/ number of entries in above list<br \/>\n            D3D11_SDK_VERSION,        \/\/ always set this to D3D11_SDK_VERSION for modern<br \/>\n            &amp;d3dDevice,               \/\/ returns the Direct3D device created<br \/>\n            &amp;m_featureLevel,          \/\/ returns feature level of device created<br \/>\n            &amp;d3dContext               \/\/ returns the device immediate context<br \/>\n            )<br \/>\n        );<\/p>\n<p>    \/\/ Get the DirectX11.1 device by QI off the DirectX11 one.<br \/>\n    DX::ThrowIfFailed(<br \/>\n        d3dDevice.As(&amp;m_d3dDevice)<br \/>\n        );<\/p>\n<p>    \/\/ And get the corresponding device context in the same way.<br \/>\n    DX::ThrowIfFailed(<br \/>\n        d3dContext.As(&amp;m_d3dContext)<br \/>\n        );<\/p>\n<p>    \/\/ Obtain the underlying DXGI device of the Direct3D11.1 device.<br \/>\n    ComPtr&lt;IDXGIDevice&gt; dxgiDevice;<br \/>\n    DX::ThrowIfFailed(<br \/>\n        m_d3dDevice.As(&amp;dxgiDevice)<br \/>\n        );<\/p>\n<p>    \/\/ Obtain the Direct2D device for 2D rendering.<br \/>\n    DX::ThrowIfFailed(<br \/>\n        m_d2dFactory-&gt;CreateDevice(dxgiDevice.Get(), &amp;m_d2dDevice)<br \/>\n        );<\/p>\n<p>    \/\/ And get its corresponding device context object.<br \/>\n    DX::ThrowIfFailed(<br \/>\n        m_d2dDevice-&gt;CreateDeviceContext(<br \/>\n            D2D1_DEVICE_CONTEXT_OPTIONS_NONE,<br \/>\n            &amp;m_d2dContext<br \/>\n            )<br \/>\n        );<\/p>\n<p>    \/\/ Save the DPI of this display in our class.<br \/>\n    m_d2dContext-&gt;SetDpi(m_dpi, m_dpi);<\/p>\n<p>    \/\/ Release the swap chain (if it exists) as it will be incompatible with<br \/>\n    \/\/ the new device.<br \/>\n    m_swapChain = nullptr;<\/p>\n<p>}<br \/>\nPlatform::Array&lt;unsigned char&gt;^ DXBitmap::MakeText( Platform::String^ text )<br \/>\n{<br \/>\n\tIWICStream *pStream = NULL;<br \/>\n    IWICBitmapEncoder *pEncoder = NULL;<br \/>\n    IWICBitmapFrameEncode *pFrameEncode = NULL;<br \/>\n    static const UINT sc_bitmapWidth = 140;<br \/>\n    static const UINT sc_bitmapHeight = 140;<br \/>\n    IWICBitmap *pWICBitmap = NULL;<br \/>\n    ID2D1RenderTarget *pRT = NULL;<br \/>\n    IDWriteTextFormat *pTextFormat = NULL;<br \/>\n    ID2D1SolidColorBrush *pBlackBrush = NULL;<\/p>\n<p>\tDX::ThrowIfFailed(<br \/>\n\t\tm_wicFactory-&gt;CreateBitmap(<br \/>\n            sc_bitmapWidth,<br \/>\n            sc_bitmapHeight,<br \/>\n            GUID_WICPixelFormat32bppBGR,<br \/>\n            WICBitmapCacheOnLoad,<br \/>\n            &amp;pWICBitmap<br \/>\n\t\t\t));<\/p>\n<p>\tDX::ThrowIfFailed(<br \/>\n        m_d2dFactory-&gt;CreateWicBitmapRenderTarget(<br \/>\n            pWICBitmap,<br \/>\n            D2D1::RenderTargetProperties(),<br \/>\n            &amp;pRT<br \/>\n            ));<\/p>\n<p>        pRT-&gt;BeginDraw();<br \/>\n        pRT-&gt;Clear(D2D1::ColorF(D2D1::ColorF::White));<br \/>\n        D2D1_SIZE_F rtSize = pRT-&gt;GetSize();<\/p>\n<p>\t\t\/\/ static const WCHAR sc_fontName[] = L&quot;Calibri&quot;;<br \/>\n\t\tstatic const WCHAR sc_fontName[] = L&quot;HGP\u660e\u671dE&quot;;<br \/>\n        static const FLOAT sc_fontSize = 32;<\/p>\n<p>        m_dwriteFactory-&gt;CreateTextFormat(<br \/>\n            sc_fontName,<br \/>\n            NULL,<br \/>\n            DWRITE_FONT_WEIGHT_NORMAL,<br \/>\n            DWRITE_FONT_STYLE_NORMAL,<br \/>\n            DWRITE_FONT_STRETCH_NORMAL,<br \/>\n            sc_fontSize,<br \/>\n            L&quot;&quot;, \/\/locale<br \/>\n            &amp;pTextFormat<br \/>\n            );<\/p>\n<p>        pRT-&gt;CreateSolidColorBrush(<br \/>\n            D2D1::ColorF(D2D1::ColorF::Black),<br \/>\n            &amp;pBlackBrush<br \/>\n            );<\/p>\n<p>        pRT-&gt;DrawText(<br \/>\n\t\t\ttext-&gt;Data(),<br \/>\n\t\t\ttext-&gt;Length(),<br \/>\n            pTextFormat,<br \/>\n            D2D1::RectF(0, 0, rtSize.width, rtSize.height),<br \/>\n            pBlackBrush);<\/p>\n<p>\t\tpRT-&gt;EndDraw();<\/p>\n<p>    \/\/ Save image to file<br \/>\n    DX::ThrowIfFailed(<br \/>\n\t\tm_wicFactory-&gt;CreateStream(&amp;pStream));<\/p>\n<p>    WICPixelFormatGUID format = GUID_WICPixelFormatDontCare;<br \/>\n    \/\/    static const WCHAR filename[] = L&quot;output.png&quot;;<\/p>\n<p>\tunsigned char buff[sc_bitmapWidth*sc_bitmapHeight*4];<\/p>\n<p>\/\/\tDX::ThrowIfFailed(<br \/>\n\/\/\t\tpStream-&gt;InitializeFromFilename(filename, GENERIC_WRITE));<br \/>\n\tDX::ThrowIfFailed(<br \/>\n\t\tpStream-&gt;InitializeFromMemory( buff, sizeof(buff)));<br \/>\n    DX::ThrowIfFailed(<br \/>\n\t\tm_wicFactory-&gt;CreateEncoder(GUID_ContainerFormatPng, NULL, &amp;pEncoder));<br \/>\n\tDX::ThrowIfFailed(<br \/>\n        pEncoder-&gt;Initialize(pStream, WICBitmapEncoderNoCache));<br \/>\n    DX::ThrowIfFailed(<br \/>\n        pEncoder-&gt;CreateNewFrame(&amp;pFrameEncode, NULL));<br \/>\n\tDX::ThrowIfFailed(<br \/>\n\t\tpFrameEncode-&gt;Initialize(NULL));<br \/>\n\tDX::ThrowIfFailed(<br \/>\n        pFrameEncode-&gt;SetSize(sc_bitmapWidth, sc_bitmapHeight));<br \/>\n    DX::ThrowIfFailed(<br \/>\n        pFrameEncode-&gt;SetPixelFormat(&amp;format));<br \/>\n    DX::ThrowIfFailed(<br \/>\n        pFrameEncode-&gt;WriteSource(pWICBitmap, NULL));<br \/>\n\tDX::ThrowIfFailed(<br \/>\n        pFrameEncode-&gt;Commit());<br \/>\n\tDX::ThrowIfFailed(<br \/>\n        pEncoder-&gt;Commit());<\/p>\n<p>\tPlatform::Array&lt;unsigned char&gt;^ buffer = ref new Platform::Array&lt;unsigned char&gt;(sizeof(buff));<br \/>\n\tfor( int i=0; i&lt;sizeof(buff); i++ ) {<br \/>\n\t\tbuffer[i] = buff[i];<br \/>\n\t}<br \/>\n\treturn buffer;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u6700\u5f8c\u306e byte[] \u304b\u3089 array \u306b\u79fb\u3057\u66ff\u3048\u308b\u306e\u304c\u30c0\u30b5\u3044\u306e\u3067\u3059\u304c\u3001\u307e\u3042\u66ab\u5b9a\u7684\u306b\u3053\u3046\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u9069\u5f53\u306a\u30b3\u30d4\u30fc\u30e1\u30bd\u30c3\u30c9\u304c\u898b\u5f53\u305f\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3002<br \/>\n\u5b9f\u306f\u3001\u3053\u3053\u3067\u554f\u984c\u306a\u306e\u306f buff \u306e\u30b5\u30a4\u30ba\u306a\u306e\u3067\u3059\u304c\u3001\u52dd\u624b\u306b width * height * 4 \u3068\u6c7a\u3081\u3064\u3051\u3066\u3044\u307e\u3059\u3002\u30b3\u30fc\u30c9\u3092\u898b\u308b\u3068\u308f\u304b\u308a\u307e\u3059\u304c\u3001BITMAP \u5f62\u5f0f\u304b\u3089 PNG \u5f62\u5f0f\u306b\u5909\u63db\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u30b5\u30a4\u30ba\u304c\u6b63\u3057\u304f\u306a\u3044\u306e\u3067\u3059\u3088\u306d\u3002\u3053\u306e\u3042\u305f\u308a\u3001\u306f\u6539\u4fee\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u305d\u3046\u305d\u3046\u3001\u30d3\u30eb\u30c9\u3092\u3059\u308b\u3068\u304d\u306b\u306f\u3001DirectX \u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u5fc5\u8981\u3067\u3059\u3002\u30ea\u30f3\u30ab\u30fc\u306e\u5165\u529b\u3068\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nd2d1.lib<br \/>\nd3d11.lib<br \/>\ndxgi.lib<br \/>\nole32.lib<br \/>\nwindowscodecs.lib<br \/>\ndwrite.lib<br \/>\n[\/code]<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120524_02org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120524_02thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u9762\u5012\u306a\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u3092\u76f4\u63a5\u8cbc\u308a\u4ed8\u3051\u3066\u3057\u307e\u3063\u3066\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f Direct2D \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u4f7f\u3063\u3066\u3044\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nkernel32.lib;user32.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;%(AdditionalDependencies)<br \/>\n[\/code]<\/p>\n<p>\u3055\u3066\u3001\u3053\u308c\u3067 C++\/CX \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u307b\u3046\u306f\u6e96\u5099\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5229\u7528\u3059\u308b C# \u306e\u30b3\u30fc\u30c9\u3092\u66f8\u304d\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nusing Windows.UI.Xaml.Media.Imaging;<br \/>\nusing Windows.Storage;<br \/>\nusing Windows.Storage.Streams;<br \/>\nusing Windows.Graphics.Imaging;<br \/>\nusing System.Runtime.InteropServices.WindowsRuntime;<\/p>\n<p>private async void Button_Click_3(object sender, RoutedEventArgs e)<br \/>\n{<br \/>\n\tvar dxbmp = new MakeBitmapLib.DXBitmap();<br \/>\n\tdxbmp.Initialize();<br \/>\n\tbyte[] data = dxbmp.MakeText(&quot;\u3042\u308c\u3053\u308c&quot;);<\/p>\n<p>\tvar mem = new InMemoryRandomAccessStream();<br \/>\n\tawait mem.WriteAsync(data.AsBuffer());<br \/>\n\t\/\/ await mem.FlushAsync();<br \/>\n\tBitmapImage bmp = new BitmapImage();<br \/>\n\tbmp.SetSource(mem);<br \/>\n\timage1.Source = bmp;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>using \u304c\u305f\u304f\u3055\u3093\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u304c\u5fc5\u9808\u306a\u306e\u3067\u3059\u3088\u306d\uff5e\u3002\u3053\u306e\u3042\u305f\u308a\u306e\u5fae\u5999\u306a\u3068\u3053\u308d\u3082\u3042\u3063\u3066\u82e5\u5e72\u4f7f\u3044\u3065\u3089\u3044\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3067\u6587\u5b57\u304c\u66f8\u3051\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120524_03org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120524_03thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u307e\u3042\u3001\u5b57\u3060\u3051\u3067\u306f\u3064\u307e\u3089\u306a\u3044\u306e\u3067\u3001\u305d\u306e\u3046\u3061\u306b\u753b\u50cf\u3082\u4ed8\u3051\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>\u3055\u3066\u3001C# \u306e\u30b3\u30fc\u30c9\u3067\u3059\u304c\u601d\u3063\u305f\u3088\u308a\u3082\u8907\u96d1\u3067\u3059\u3088\u306d\u3002\u5b9f\u306f\u672c\u6765\u306a\u3089\u3070\u3001\u6b21\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u306b\u3057\u305f\u3044\u306e\u3067\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nprivate void Button_Click_3(object sender, RoutedEventArgs e)<br \/>\n{<br \/>\n\tvar dxbmp = new MakeBitmapLib.DXBitmap();<br \/>\n\tdxbmp.Initialize();<br \/>\n\timage1.Source = dxbmp.MakeText(&quot;\u3042\u308c\u3053\u308c&quot;);<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u3053\u3046\u3059\u308b\u3068\u3059\u3063\u304d\u308a\u3057\u3066\u3001\u3053\u308c\u306a\u3089\u3070\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u3057\u3066\u4f7f\u3048\u305d\u3046\u306a\u611f\u3058\u3067\u3059\u3088\u306d\uff5e\u3002\u4f55\u6545\u3001\u3053\u3046\u3057\u306a\u3044\u304b\/\u3053\u3046\u3067\u304d\u306a\u304b\u3063\u305f\u304b\u3068\u3044\u3046\u3068\u3001<\/p>\n<ul>\n<li>WriteAsync \u30e1\u30bd\u30c3\u30c9\u304c\u975e\u540c\u671f\u306a\u306e\u3067\u3001async\/await \u3092\u4f5c\u3089\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3002<\/li>\n<li>async\/await \u306e\u4f7f\u3044\u65b9\u306f\u308f\u304b\u3063\u305f\u3051\u3069\u3001\u300c\u4f5c\u308a\u65b9\u300d\u304c\u5206\u304b\u3089\u306a\u3044\u3002<\/li>\n<li>\u306a\u306e\u3067\u3001\u30e1\u30bd\u30c3\u30c9\u306b\u62ec\u308a\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u3002<\/li>\n<li>\u66f4\u306b\u3001C++\/CX \u3067 async\/await \u3092\u4f5c\u308a\u65b9\u304c\u5206\u304b\u3089\u306a\u3044\u3002<\/li>\n<\/ul>\n<p>\u3063\u3066\u306a\u8a33\u306a\u3093\u3067\u3059\u3088\u306d\uff5e\u3002<br \/>\n\u3068\u3044\u3046\u8a33\u3067\u3001\u5358\u306a\u308b\u753b\u50cf\u52a0\u5de5\u306b\u3082\u975e\u540c\u671f\u304c\u95a2\u308f\u3063\u3066\u3057\u307e\u3046\u3068\u3044\u3046\u3084\u3084\u3053\u3057\u3055\u306a\u306e\u3067\u3059\u304c\u3001\u307e\u3041\u3001\u3053\u306e\u3042\u305f\u308a\u306f\u3082\u3046\u3061\u3087\u3063\u3068\u8abf\u3079\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u304b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[win8] \u30b9\u30bf\u30fc\u30c8\u753b\u9762\u306e\u30bf\u30a4\u30eb\u3092\u52d5\u7684\u306b\u4f5c\u6210\u3059\u308b\uff08\u524d\u54e8\u6226\uff09 | Moonmile Solutions Blog http:\/\/www.moonmile.net\/blog\/archives\/3443 \u304b\u3089\u3061\u3087\u3063\u3068\u6a2a\u9053\u306b\u9038 &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3454\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[23,46,44],"tags":[],"class_list":["post-3454","post","type-post","status-publish","format-standard","hentry","category-csharp","category-ccx","category-windows-8"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=3454"}],"version-history":[{"count":2,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3454\/revisions"}],"predecessor-version":[{"id":3455,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3454\/revisions\/3455"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=3454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=3454"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=3454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}