2012-06-25 79 views
1

我有一個畫布,其中包含一個Image,在其中展示現有的BMP。我在畫布上繪製矩形並將它們添加到兒童角色。當我點擊保存時,我想更新底層的BMP文件。繪製到WPF畫布的位圖

以下代碼有效,但繪製到BMP的矩形比我繪製的小得多。我猜這個座標有一些不同嗎?也許我不應該使用System.Drawing?

 using (Graphics g = Graphics.FromImage(image)) 
     { 
      g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; 

      foreach (var child in canvas.Children) 
      { 
      if (child is System.Windows.Shapes.Rectangle) 
      { 
       var oldRect = child as System.Windows.Shapes.Rectangle; 

       // need to do something here to make the new rect bigger as the scale is clearly different 
       var rect = new Rectangle((int)Canvas.GetLeft(oldRect), (int)Canvas.GetTop(oldRect), (int)oldRect.Width, (int)oldRect.Height); 
       g.FillRectangle(Brushes.Black, rect); 
      } 
      } 
      ... code to save bmp 

歡迎任何建議!

由於

回答

3

嘗試使用System.Windows.Media.Imaging.RenderTargetBitmap類(一個example here)。
Wpf uses Device Independent Graphics所以你必須補償DPI:

RenderTargetBitmap bmp = new RenderTargetBitmap((int)Canvas1.Width, (int)Canvas1.Height, 96, 96, PixelFormats.Default); 
bmp.Render(Canvas1); 

從第三環節:

有兩個系統因素決定你的屏幕上的文字和圖形的大小:分辨率和DPI。分辨率描述了屏幕上出現的像素數量。隨着分辨率的提高,像素變小,圖形和文字顯得更小。在分辨率更改爲1600 x 1200時,顯示器設置爲1024 x 768時顯示的圖形會顯得更小。

+0

非常好,非常感謝。我實際上沒有明白你的答案,但至少我現在有一個地方可以開始閱讀。這成像goop對我來說都是新的。 – Jonesie