示例#1
0
    /// <summary>
    /// Creates an <see cref="ID2D1Bitmap"/> instance.
    /// </summary>
    /// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> instance to use to create the bitmap source.</param>
    /// <param name="width">The width of the bitmap to create.</param>
    /// <param name="height">The height of the bitmap to create.</param>
    /// <returns>A new <see cref="ID2D1Bitmap"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Bitmap> CreateD2D1BitmapAndSetAsTarget(ID2D1DeviceContext *d2D1DeviceContext, uint width, uint height)
    {
        D2D_SIZE_U d2DSize;

        d2DSize.width  = width;
        d2DSize.height = height;

        D2D1_BITMAP_PROPERTIES1 d2DBitmapProperties1Target = default;

        d2DBitmapProperties1Target.pixelFormat.format    = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
        d2DBitmapProperties1Target.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED;
        d2DBitmapProperties1Target.bitmapOptions         =
            D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_TARGET |
            D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW;

        using ComPtr <ID2D1Bitmap> d2D1Bitmap1Target = default;

        // Create a target D2D1 bitmap
        d2D1DeviceContext->CreateBitmap(
            size: d2DSize,
            sourceData: null,
            pitch: 0,
            bitmapProperties: &d2DBitmapProperties1Target,
            bitmap: (ID2D1Bitmap1 **)d2D1Bitmap1Target.GetAddressOf()).Assert();

        d2D1DeviceContext->SetTarget((ID2D1Image *)d2D1Bitmap1Target.Get());

        return(d2D1Bitmap1Target.Move());
    }
示例#2
0
    /// <summary>
    /// Creates an <see cref="ID2D1Bitmap1"/> instance and copies data from a source bitmap.
    /// </summary>
    /// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> instance to use to create the bitmap.</param>
    /// <param name="d2D1Bitmap">The input <see cref="ID2D1Bitmap1"/> to read data from.</param>
    /// <param name="d2D1MappedRect">The resulting <see cref="D2D1_MAPPED_RECT"/> for the bitmap.</param>
    /// <returns>A new <see cref="ID2D1Bitmap1"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Bitmap1> CreateD2D1Bitmap1Buffer(ID2D1DeviceContext *d2D1DeviceContext, ID2D1Bitmap *d2D1Bitmap, out D2D1_MAPPED_RECT d2D1MappedRect)
    {
        D2D_SIZE_U d2DSize = d2D1Bitmap->GetPixelSize();

        D2D1_BITMAP_PROPERTIES1 d2DBitmapProperties1Buffer = default;

        d2DBitmapProperties1Buffer.pixelFormat.format    = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
        d2DBitmapProperties1Buffer.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED;
        d2DBitmapProperties1Buffer.bitmapOptions         = D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CPU_READ | D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW;

        using ComPtr <ID2D1Bitmap1> d2D1Bitmap1Buffer = default;

        // Create a buffer D2D1 bitmap
        d2D1DeviceContext->CreateBitmap(
            size: d2DSize,
            sourceData: null,
            pitch: 0,
            bitmapProperties: &d2DBitmapProperties1Buffer,
            bitmap: d2D1Bitmap1Buffer.GetAddressOf()).Assert();

        D2D_POINT_2U d2DPointDestination = default;
        D2D_RECT_U   d2DRectSource       = new(0, 0, d2DSize.width, d2DSize.height);

        // Copy the image from the target to the readback bitmap
        d2D1Bitmap1Buffer.Get()->CopyFromBitmap(
            destPoint: &d2DPointDestination,
            bitmap: d2D1Bitmap,
            srcRect: &d2DRectSource);

        fixed(D2D1_MAPPED_RECT *d2D1MappedRectPtr = &d2D1MappedRect)
        {
            // Map the buffer bitmap
            d2D1Bitmap1Buffer.Get()->Map(
                options: D2D1_MAP_OPTIONS.D2D1_MAP_OPTIONS_READ,
                mappedRect: d2D1MappedRectPtr).Assert();
        }

        return(d2D1Bitmap1Buffer.Move());
    }
示例#3
0
        public void Draw(IntPtr hWnd)
        {
            if (m_disposed)
            {
                return;
            }
            EnsureDevice(hWnd);

            using (var backbuffer = new IDXGISurface2())
            {
                // setup backbuffer
                m_swapchain.GetBuffer(0, ref IDXGISurface2.IID, out backbuffer.PtrForNew).ThrowIfFailed();
                backbuffer.GetDesc(out DXGI_SURFACE_DESC desc);


                var prop = new D2D1_BITMAP_PROPERTIES1
                {
                    bitmapOptions = D2D1_BITMAP_OPTIONS._TARGET | D2D1_BITMAP_OPTIONS._CANNOT_DRAW,
                    pixelFormat   = new D2D1_PIXEL_FORMAT
                    {
                        format    = DXGI_FORMAT._B8G8R8A8_UNORM,
                        alphaMode = D2D1_ALPHA_MODE._IGNORE
                    }
                };
                m_d2dContext.CreateBitmapFromDxgiSurface(backbuffer, ref prop, out ID2D1Bitmap1 bitmap).ThrowIfFailed();
                using (bitmap)
                {
                    m_d2dContext.SetTarget(bitmap);

                    // draw
                    m_d2dContext.BeginDraw();
                    var color = new Vector4(0, 0.1f, 0, 0);
                    m_d2dContext.Clear(ref color);

                    var brushColor = new Vector4(1, 0, 0, 1);
                    var brushProp  = new D2D1_BRUSH_PROPERTIES
                    {
                        opacity   = 1.0f,
                        transform = new Matrix3x2
                        {
                            M11 = 1.0f,
                            M22 = 1.0f,
                        }
                    };
                    m_d2dContext.CreateSolidColorBrush(ref brushColor, ref brushProp, out ID2D1SolidColorBrush brush).ThrowIfFailed();
                    using (brush)
                    {
                        var ellipse = new D2D1_ELLIPSE
                        {
                            radiusX = 50.0f,
                            radiusY = 50.0f,
                        };
                        m_d2dContext.DrawEllipse(ref ellipse, brush, 10.0f, null);
                    }
                }

                // dwrite
                var font   = new WSTR("Consolas");
                var text   = new WSTR("A0漢字B");
                var locale = new WSTR("ja-jp");

                m_dwriteFactory.CreateTextFormat(
                    ref font.Data,
                    null,
                    DWRITE_FONT_WEIGHT._REGULAR,
                    DWRITE_FONT_STYLE._NORMAL,
                    DWRITE_FONT_STRETCH._NORMAL,
                    144.0f,
                    ref locale.Data,
                    out IDWriteTextFormat pTextFormat
                    ).ThrowIfFailed();
                using (pTextFormat)
                {
                    pTextFormat.SetTextAlignment(DWRITE_TEXT_ALIGNMENT._CENTER).ThrowIfFailed();
                    pTextFormat.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT._CENTER).ThrowIfFailed();

                    var brushColor = new Vector4(0.7f, 0.7f, 1, 1);
                    var brushProp  = new D2D1_BRUSH_PROPERTIES
                    {
                        opacity   = 1.0f,
                        transform = new Matrix3x2
                        {
                            M11 = 1.0f,
                            M22 = 1.0f,
                        }
                    };
                    m_d2dContext.CreateSolidColorBrush(ref brushColor, ref brushProp, out ID2D1SolidColorBrush brush).ThrowIfFailed();
                    using (brush)
                    {
                        var rect = new Vector4
                        {
                            X = 0,
                            Y = 0,
                            Z = m_width,
                            W = m_height,
                        };

                        m_d2dContext.DrawText(
                            ref text.Data,
                            (uint)text.Length, // The string's length.
                            pTextFormat,       // The text format.
                            ref rect,          // The region of the window where the text will be rendered.
                            brush,             // The brush used to draw the text.
                            D2D1_DRAW_TEXT_OPTIONS._NONE,
                            DWRITE_MEASURING_MODE._NATURAL
                            );
                    }
                }
                m_d2dContext.EndDraw(out ulong tag1, out ulong tag2);
                // }

                m_context.Flush();
                m_swapchain.Present(0, 0);
                m_d2dContext.SetTarget(null);
            }
        }