2012-04-12 1 views
0

WPF 툴킷의 MS 차트를 PNG로 내보낼 수 없습니다. 다른 포럼의 단계를 밟았지만 모든 후 PNG는 완전히 검은 색입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?WPF Toolkit (MS Chart)에서 PNG로 차트를 내보내는 방법. 작동하지 않습니다. 검정색 만 생성합니다.

private void export_graf_Click(object sender, RoutedEventArgs e) 
     { 
      if (mcChart.Series[0] == null) 
      { 
       MessageBox.Show("there is nothing to export"); 
      } 
      else 
      { 

      RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)mcChart.ActualWidth, (int)mcChart.ActualHeight, 95d, 95d, PixelFormats.Pbgra32); 


       renderBitmap.Render(mcChart); 

       Microsoft.Win32.SaveFileDialog uloz_obr = new Microsoft.Win32.SaveFileDialog(); 
       uloz_obr.FileName = "Graf"; 
       uloz_obr.DefaultExt = "png"; 


       Nullable<bool> result = uloz_obr.ShowDialog(); 
       if (result == true) 
       { 
        string obr_cesta = uloz_obr.FileName; //cesta k souboru 

       using (FileStream outStream = new FileStream(obr_cesta, FileMode.Create)) 
        { 
         PngBitmapEncoder encoder = new PngBitmapEncoder(); 
         encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
         encoder.Save(outStream); 
        } 

       } 

답변

3

나는 레이아웃 문제가 발생했다고 생각합니다. RenderTargetBitmap 클래스는 시각적 레이어에서 작동하며 시각적 레이어에서 상속 된 오프셋 및 변형을 포함합니다. 시각적 요소를 렌더링 할 때는 BitmapFrame으로 분리해야합니다. 투명한 배경을 원하지 않는 한, 창 비주얼 트리에 영향을주지 않고 배경색을 지정할 수도 있습니다. PNG 형식은 알파 투명도를 지원하고 일부 이미지 뷰어는 투명한 픽셀을 검정색으로 표시합니다.

WPF의 기본 dpi는 96입니다. 왜 95를 지정했는지 모르겠습니다. 제로 바운드 인덱스 또는 이와 비슷한 것이 아닙니다. 아래 샘플은 96dpi를 사용합니다.

private void export_graf_Click(object sender, RoutedEventArgs e) 
{ 
    if (mcChart.Series[0] == null) 
    { 
     MessageBox.Show("there is nothing to export"); 
    } 
    else 
    { 
     Rect bounds = VisualTreeHelper.GetDescendantBounds(mcChart); 

     RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32); 

     DrawingVisual isolatedVisual = new DrawingVisual(); 
     using (DrawingContext drawing = isolatedVisual.RenderOpen()) 
     { 
      drawing.DrawRectangle(Brushes.White, null, new Rect(new Point(), bounds.Size)); // Optional Background 
      drawing.DrawRectangle(new VisualBrush(mcChart), null, new Rect(new Point(), bounds.Size)); 
     } 

     renderBitmap.Render(isolatedVisual); 

     Microsoft.Win32.SaveFileDialog uloz_obr = new Microsoft.Win32.SaveFileDialog(); 
     uloz_obr.FileName = "Graf"; 
     uloz_obr.DefaultExt = "png"; 

     Nullable<bool> result = uloz_obr.ShowDialog(); 
     if (result == true) 
     { 
      string obr_cesta = uloz_obr.FileName; 

      using (FileStream outStream = new FileStream(obr_cesta, FileMode.Create)) 
      { 
       PngBitmapEncoder encoder = new PngBitmapEncoder(); 
       encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
       encoder.Save(outStream); 
      } 
     } 
    } 
}