sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 641 forks source link

Can't Draw Text with Direct2D1 & DirectWrite :( #999

Closed SharinganShuriken closed 6 years ago

SharinganShuriken commented 6 years ago

Hello Guys,

i want to draw a text in my form, but everytime i get a Access Violation Exception if i want to create the rendertarget :(

Maybe you can help me and explain me what i do wrong?

` private DeviceContextRenderTarget device; private TextFormat format; private SolidColorBrush brush;

    private void button1_Click(object sender, EventArgs e)
    {
        this.device = new DeviceContextRenderTarget(new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded),
            new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)));
        this.format = new TextFormat(new SharpDX.DirectWrite.Factory(), "Arial", 1.0f);
        SharpDX.DXGI.Surface surface = new SharpDX.DXGI.Surface(this.Handle);
        RenderTarget renderTarget = new RenderTarget(this.device.Factory, surface, new RenderTargetProperties());
        this.brush = new SolidColorBrush(renderTarget, SharpDX.Color4.Black);

        this.device.BeginDraw();
        this.device.DrawText("Hello World", this.format, new SharpDX.RectangleF(0, 0, 100, 100), this.brush);
        this.device.EndDraw();
    }`
xoofx commented 6 years ago

SharpDX.DXGI.Surface surface = new SharpDX.DXGI.Surface(this.Handle);

You can't do this. The handle of the window has nothing to do with the COM pointer of a DXGI surface. Please follow the Direct2D documentation to properly use Direct2D with a HWND.