2011-02-26 79 views

回答

10

你試過RenderTargetBitmaphttps://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx

有一個圍繞着使用一些「屏幕截圖」的方法是,像這樣的拍攝from here:什麼是「視覺:因爲它也沒有你所說的例子在網站上提到的

public static void CreateBitmapFromVisual(Visual target, string fileName) 
    { 
     if (target == null || string.IsNullOrEmpty(fileName)) 
     { 
      return; 
     } 

     Rect bounds = VisualTreeHelper.GetDescendantBounds(target); 

     RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32); 

     DrawingVisual visual = new DrawingVisual(); 

     using (DrawingContext context = visual.RenderOpen()) 
     { 
      VisualBrush visualBrush = new VisualBrush(target); 
      context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size)); 
     } 

     renderTarget.Render(visual); 
     PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder(); 
     bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget)); 
     using (Stream stm = File.Create(fileName)) 
     { 
      bitmapEncoder.Save(stm); 
     } 
    } 
+0

'對象,我該如何使用它?它從何而來? – Lorgarn 2015-10-07 12:12:03

+0

@Lorgarn所有控件和視圖元素(TextBlocks,Grids等)都從'Visual'繼承。所以你可以將它們中的任何一個傳遞給這個方法來創建它的圖像文件。但想法是通過一個'Window'對象來獲取整個窗口的截圖。 – almulo 2015-10-08 08:34:33

+0

謝謝。我厭倦了,它工作到目前爲止,除了所有Windowdecorations被省略... – Lorgarn 2015-10-08 09:50:55