2010-08-16 39 views
0

我想在我的WPF應用程序中現有的BitmapSource對象上繪製一條線(或任何幾何形狀)。什麼是最好的方式來做到這一點?如何在WPF中現有的BitmapSource上繪製一條線?

BitmapSource是BitmapSource.Create(...)調用的結果。

由於

  • 羅曼
+0

不知道你想幹什麼你買可以看看這個 什麼http://msdn.microsoft.com/en-us/ library/system.windows.media.imaging.writeablebitmap.aspx – 2010-08-16 18:51:22

回答

0

下面樣品將顯示從用的BitmapSource在它上面一個紅線創建的圖像。那是你想要達到的目標嗎?

XAML:

<Window x:Class="WpfApplication.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Grid Background="LightBlue"> 
     <Image Source="{Binding Path=ImageSource}" /> 
     <Line 
      Stroke="Red" StrokeThickness="10" 
      X1="0" 
      Y1="0" 
      X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" 
      Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" /> 
    </Grid> 
</Window> 

後面的代碼:

using System; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

namespace WpfApplication 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      DataContext = this; 
     } 

     public BitmapSource ImageSource 
     { 
      get 
      { 
       PixelFormat pf = PixelFormats.Bgr32; 
       int width = 200; 
       int height = 200; 
       int rawStride = (width * pf.BitsPerPixel + 7)/8; 
       byte[] rawImage = new byte[rawStride * height]; 

       Random value = new Random(); 
       value.NextBytes(rawImage); 

       return BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride); 
      } 
     } 
    } 
} 
+0

程序員 感謝您的回答。其實我試圖直接在BitmapSource中寫入。然後在稍後階段,我可以將BitmapSource的內容保存到另一個緩衝區或文件中! – HW2015 2010-08-17 07:53:55