2010-02-05 139 views
0

我目前正試圖堅持一個畫布到位圖,我遇到了一些非常奇怪的行爲。以下三種情況的源代碼出現在帖子的末尾。令人困惑的WPF繪製行爲

情況1:如預期的那樣,輸出文件(test.png)中出現紅色矩形。

情況2:輸出文件中不顯示紅色矩形。

情況3:輸出文件中不顯示紅色矩形。

似乎將矩形添加到畫布(即使該畫布從未用於將矩形渲染到磁盤)是必要的。似乎按鈕點擊必須啓動繪圖 - 它不能出現在Window構造函數中。這些都對我沒有意義,我認爲我誤解了一些東西。

此外,我很抱歉代碼格式不正確。我摔了20分鐘,但現在我放棄了。

由於提前,

- 布雷克Fresen

用於所有3例XAML:

<Window x:Class="ScanOutlineCreator.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="250" Width="250"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="20" /> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 
      <Button x:Name="btnGo" Grid.Row="0"> 
       <TextBlock Text="Go" /> 
      </Button> 
      <Canvas Grid.Row="1" x:Name="canvas" Width="200" Height="500"></Canvas> 
     </Grid> 
</Window> 

情況1:

using System.IO; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using Common.Drawing; 
namespace ScanOutlineCreator 
{ 
     public partial class Window1 
     { 
       static Rectangle r = new Rectangle { Width = 100, Height = 100, Fill = Brushes.Red }; 
       public Window1() 
       { 
         InitializeComponent(); 
         btnGo.Click += new RoutedEventHandler(btnGo_Click); 
         canvas.Children.Add(r); 
       } 

       static void btnGo_Click(object sender, RoutedEventArgs e) 
       { 
         using (Stream stm = File.Create("test.png")) 
         { 
           RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32); 
           rtb.Render(r); 
           Util.RenderTargetBitmapToStream(rtb, stm); 
         } 
       } 
     } 
} 

情況2:

using System.IO; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using Common.Drawing; 

namespace ScanOutlineCreator  
{  
    public partial class Window1 
    { 
     static Rectangle r = new Rectangle { Width = 100, Height = 100, Fill = Brushes.Red }; 
     public Window1() 
     { 
      InitializeComponent(); 
      btnGo.Click += new RoutedEventHandler(btnGo_Click); 
     } 

     static void btnGo_Click(object sender, RoutedEventArgs e) 
     { 
      using (Stream stm = File.Create("test.png")) 
      { 
       RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32); 
       rtb.Render(r); 
       Util.RenderTargetBitmapToStream(rtb, stm); 
      } 
     } 
    } 
} 

案例3:

using System.IO; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using Common.Drawing; 
namespace ScanOutlineCreator 
{ 
    public partial class Window1 
    { 
     static Rectangle r = new Rectangle { Width = 100, Height = 100, Fill = Brushes.Red }; 
     public Window1() 
     { 
      InitializeComponent(); 
      canvas.Children.Add(r); 
      using (Stream stm = File.Create("test.png")) 
      { 
       RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32); 
       rtb.Render(r); 
       Util.RenderTargetBitmapToStream(rtb, stm); 
      } 
     } 
    } 
} 

回答

0

這是幾乎可以肯定的佈局做。如果是這樣,它之前已被多次覆蓋。

在添加你必須讓你在渲染之前被執行一定的佈局,這樣的事情其實都是在可視化樹在正確的位置等

你可能需要調用措施的某種組合/安排兒童渲染前。

+0

謝謝。 Calling Measure並沒有改變任何事情,但是安排似乎是在做伎倆。 – 2010-02-05 16:10:11