2010-02-04 56 views
0

使用下面的代碼,我試圖用UIElements填充Canvas並將其保存爲tif Image。但是,我的Image總是空白。這是因爲Canvas永遠不會顯示在屏幕上,並且某種初始化和繪圖從未發生過?我該如何做這項工作?創建VisualBrush而不會顯示它代表的視覺

Canvas創作會去是這樣的:

Canvas theCanvas = new Canvas(); 
theCanvas.Width = 2740; 
theCanvas.Height = 2280; 

... 

Button button = new Button(); 
button.Content = "Push Me."; 
button.Height = 50; 
button.Width = 200; 
Canvas.SetTop(button, 200); 
Canvas.SetLeft(button, 300); 
theCanvas.Children.Add(button); 

要創建Image並保存:

using (System.IO.FileStream fs = 
      new System.IO.FileStream(path, System.IO.FileMode.Create)) 
{ 
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
     (int)inWidth, 
     (int)inHeight, 1/300, 1/300, 
     PixelFormats.Pbgra32); 

    DrawingVisual visual = new DrawingVisual(); 
    using (DrawingContext context = visual.RenderOpen()) 
    { 
     VisualBrush brush = new VisualBrush(inCanvas); 
     context.DrawRectangle(
      brush, 
      null, 
      new Rect(new Point(), new Size(inWidth, inHeight))); 
    }     

    renderBitmap.Render(visual); 

    BitmapEncoder encoder = new TiffBitmapEncoder(); 
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
    encoder.Save(fs); 
    fs.Close(); 
} 
+2

我有同樣的問題。 http://stackoverflow.com/questions/1877115/create-wpf-element-offscreen-and-render-to-bitmap – Will 2010-02-04 20:16:59

+0

那麼,可能是一個差異問題(我做錯了什麼),但代碼是所有的.. – Will 2010-02-04 20:19:19

+0

是啊,這有幫助。我錯過了測量和安排。 – 2010-02-05 18:45:15

回答

0

看到我原來的職位的第一個評論。

0

在這種情況下,控制永遠不會呈現在屏幕上。我只是將畫布添加到網格,然後點擊一個按鈕,我稱之爲保存命令。它的工作。

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 

    theCanvas.Width = 2740; 
    theCanvas.Height = 2280; 

    Button button = new Button(); 
    button.Content = "Push Me."; 
    button.Height = 50; 
    button.Width = 200; 
    Canvas.SetTop(button, 200); 
    Canvas.SetLeft(button, 300); 
    theCanvas.Children.Add(button); 
    mainGri.Children.Add(theCanvas); 

    } 

    private void mainGri_MouseDown(object sender, MouseButtonEventArgs e) 
    { 



    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     String path = @"c:\\a.jpg"; 
     using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create)) 
     { 
      int inWidth = 300; 
      int inHeight = 400; 
      RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)inWidth, 
                   (int)inHeight, 1/300, 1/300, PixelFormats.Pbgra32); 

      DrawingVisual visual = new DrawingVisual(); 
      using (DrawingContext context = visual.RenderOpen()) 
      { 
       VisualBrush brush = new VisualBrush(theCanvas); 
       context.DrawRectangle(brush, 
             null, 
             new Rect(new Point(), new Size(inWidth, inHeight))); 
      } 

      renderBitmap.Render(visual); 

      BitmapEncoder encoder = new TiffBitmapEncoder(); 
      encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
      encoder.Save(fs); 
      fs.Close(); 
     } 
    } 
} 

}

+0

我的目標是不必顯示畫布。 – 2010-02-05 18:50:54

相關問題