Why am I getting a "D2DERR_WRONG_FACTORY" error in this code when I use a list?

Forrest Crawford 60 Reputation points
2024-06-18T12:31:19.3566667+00:00

I'm attempting to use Sharp DX to display a dxf file and I'm getting a "[D2DERR_WRONG_FACTORY/WrongFactory]" whenever I render using the below code:

public override void Render(RenderTarget target)
{
    target.Clear(new RawColor4(1.0f, 1.0f, 1.0f, 1.0f));
    Brush brush = resCache["BlackBrush"] as Brush;
    if (!dxfLoaded)
    {
        DxfDocument doc = DxfDocument.Load(FilePath);
        foreach (var line in doc.Entities.Lines)
        {
            PathGeometry pathGeometry = new(resCache.Factory);
            var sink = pathGeometry.Open();
            sink.BeginFigure(new RawVector2((float)line.StartPoint.X, (float)line.StartPoint.Y), FigureBegin.Filled);
            sink.AddLine(new RawVector2((float)line.EndPoint.X, (float)line.EndPoint.Y));
            sink.EndFigure(FigureEnd.Closed);
            sink.Close();
            sink.Dispose();
            Geometries.Add(pathGeometry);
        }
        dxfLoaded = true;
    }
    foreach (var geometry in Geometries)
    {
        target.DrawGeometry(geometry, brush, currentThickness);
    }

    target.Transform = new((float)matrix.M11, (float)matrix.M12, (float)matrix.M21, (float)matrix.M22,
       (float)matrix.OffsetX, (float)matrix.OffsetY);
    NeedsUpdate = true;
}

If I use this same code but instead just draw the geometries to the target without adding it to a list, the code works fine, as seen below:

public override void Render(RenderTarget target)
{
    target.Clear(new RawColor4(1.0f, 1.0f, 1.0f, 1.0f));
    Brush brush = resCache["BlackBrush"] as Brush;
    if (!dxfLoaded)
    {
        DxfDocument doc = DxfDocument.Load(FilePath);
        foreach (var line in doc.Entities.Lines)
        {
            PathGeometry pathGeometry = new(resCache.Factory);
            var sink = pathGeometry.Open();
            sink.BeginFigure(new RawVector2((float)line.StartPoint.X, (float)line.StartPoint.Y), FigureBegin.Filled);
            sink.AddLine(new RawVector2((float)line.EndPoint.X, (float)line.EndPoint.Y));
            sink.EndFigure(FigureEnd.Closed);
            sink.Close();
            sink.Dispose();

            target.DrawGeometry(pathGeometry, brush, currentThickness);
        }
        dxfLoaded = true;
    }

    target.Transform = new((float)matrix.M11, (float)matrix.M12, (float)matrix.M21, (float)matrix.M22,
       (float)matrix.OffsetX, (float)matrix.OffsetY);
    NeedsUpdate = true;
}


Why is a generic list causing this error? Here is a link to the project.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,550 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,176 Reputation points Microsoft Vendor
    2024-06-19T05:50:50.46+00:00

    Hi,@Forrest Crawford . Welcome to Microsoft Q&A.

    The D2DERR_WRONG_FACTORY error you're encountering with SharpDX and Direct2D typically occurs when you try to use resources that were created with different factories than the ones being used for rendering. In your case, this error is happening because the PathGeometry objects are being created and stored in a list, and then later used for rendering. This can cause issues if the rendering target or the resource cache uses a different factory than the one used to create the geometries.

    Ensure that the same Direct2D factory instance is used for creating PathGeometry objects and for the RenderTarget. The factory used to create PathGeometry should be the same factory used by the RenderTarget.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful