Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SharpDX and external methods

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SharpDX and external methods

    HI

    I outline some text in my indicator OnRender method and I have tried to offload it to a separate method using the following

    Code:
    Variables
    private SharpDX.Vector2 textPoint1;
            private SharpDX.Vector2 textPoint2;
            private SharpDX.DirectWrite.TextLayout textLayout1;
            private SharpDX.DirectWrite.TextLayout textLayout2;
    
    ​
        private void OutlineClockText(int dx,int dy)
            {
             
                for ( dx = -1; dx <= 1; dx++)
                {
                    for ( dy = -1; dy <= 1; dy++)
                    {
                        if (dx != 0 || dy != 0) // Skip the center point
                        {
                            var outlinePosition = new SharpDX.Vector2(textPoint1.X + dx, textPoint1.Y + dy);
                            RenderTarget.DrawTextLayout(outlinePosition, textLayout1, outlineBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                            outlinePosition = new SharpDX.Vector2(textPoint2.X + dx, textPoint2.Y + dy);
                            RenderTarget.DrawTextLayout(outlinePosition, textLayout2, counteroutlineBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                        }
                    }
                }
            }​
    it complies but I am getting a protected memory error.
    Is it possible to do this in a method or does it have to be done in OnRender?
    If it can, can you point me to where my error is?

    thanks and Happy Xmas.

    #2
    Hello Mindset,

    Thank you for your post.

    What is the full error message?

    Are you declaring any SharpDX brushes outside of OnRender() or OnRenderTargetChanged?

    Comment


      #3
      I do declare brushes outside of OnRender ( as variables) but I am not using ONRenderTargetChanged().
      Maybe I should be?

      Comment


        #4
        Hello,

        SharpDX Brushes are device-dependent resources, which means they can only be used with the device (i.e., RenderTarget) that created them. In practice, this means you should ONLY create your SharpDX brushes during the chart object's OnRender() or OnRenderTargetChanged() methods. Otherwise, you will likely encounter memory errors as you have seen.

        The HG page below goes into detail.

        Comment


          #5
          Ah I see. Thank you for that.
          I have removed the variable declarations and will see if that helps with the issue.

          Comment


            #6
            Gaby
            Ok I got this to work having removed the declarations in the variables and declaring the brushes inside the method and in OnRender.

            Code:
                    private void OutlineClockText(int dx,int dy)
                    {
                         SharpDX.Direct2D1.Brush outlineBrush = ClockBrushOutline.ToDxBrush(RenderTarget);
                         SharpDX.Direct2D1.Brush counteroutlineBrush = CounterBrushOutline.ToDxBrush(RenderTarget);
            
                        for ( dx = -1; dx <= 1; dx++)
                        {
                            for ( dy = -1; dy <= 1; dy++)
                            {
                                if (dx != 0 || dy != 0) // Skip the center point
                                {
                                    var outlinePosition = new SharpDX.Vector2(textPoint1.X + dx, textPoint1.Y + dy);
                                    RenderTarget.DrawTextLayout(outlinePosition, textLayout1, outlineBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                                    outlinePosition = new SharpDX.Vector2(textPoint2.X + dx, textPoint2.Y + dy);
                                    RenderTarget.DrawTextLayout(outlinePosition, textLayout2, counteroutlineBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                                }
                            }
                        }
                    }​
            and in OnRender I use

            Code:
                        using SharpDX.Direct2D1.Brush outlineBrush = ClockBrushOutline.ToDxBrush(RenderTarget);
                        using SharpDX.Direct2D1.Brush counteroutlineBrush = CounterBrushOutline.ToDxBrush(RenderTarget);
            ​
            I seem to be declaring the brushes twice - not sure if this is correct?
            Also if I use the word using do I actually need to dispose of the brushes using
            outlineBrush.Dispose();
            counteroutlineBrush.Dispose();
            ​only the help file says it's called natively anyway?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            558 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            324 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            545 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X