castorix / WinUI3_SwapChainPanel_Direct2D

6 stars 0 forks source link

anti-aliasing or image quality issues. #1

Closed zkvii closed 3 months ago

zkvii commented 3 months ago
blur

I'm not sure if this is obvious, there is a bit of a difference between the rectangle I added in the swapchainpanel and the one drawn with D2D. the modify code as follows

// in HRESULT Render() function of MainWindow.xaml.cs
   m_pD2DDeviceContext.CreateSolidColorBrush(new ColorF(ColorF.Enum.Black), null, out var rectBrush);
   // var oldmode= m_pD2DDeviceContext.GetAntialiasMode();
   // m_pD2DDeviceContext.SetAntialiasMode(D2D1_ANTIALIAS_MODE.D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
   m_pD2DDeviceContext.FillRectangle(new D2D1_RECT_F(100f,100f,200f,200f),rectBrush);

// in MainWIndow.xaml
  <SwapChainPanel x:Name="scpD2D" Grid.RowSpan="2"                       
                  Margin="10, 10, 0, 10"
                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                   RenderTransformOrigin="0.5 0.5" >
      <Rectangle Width="100" Height="100" Fill="Black"></Rectangle>
      <!--Width ="800" Height="400"-->
      <!--HorizontalAlignment="Center" VerticalAlignment="Center"-->

  </SwapChainPanel>
zkvii commented 3 months ago

as doc in this

image

add code as

  D2DPanel.CompositionScaleChanged+=D2DPanel_CompositionScaleChanged;
  private void D2DPanel_CompositionScaleChanged(SwapChainPanel sender, object args)
  {
      var nDpi=Win32Helpers.GetDpiForWindow(hwnd);
      double nScaleX = 96.0f / (double)nDpi;
      double nScaleY = 96.0f / (double)nDpi;
      D2DPanel.RenderTransform = new ScaleTransform()
      {
          ScaleX = nScaleX,
          ScaleY = nScaleY
      };

  }

I use vortice d2d lib as replace