2010-08-16 5 views
0

Je voudrais dessiner une ligne (ou n'importe quelle forme géométrique) sur un objet BitmapSource existant dans mon application WPF. Quelle est la meilleure façon de le faire?Comment dessiner une ligne sur un BitmapSource existant dans WPF?

Le bitmapSource est le résultat d'un appel BitmapSource.Create (...).

Merci

  • Romain
+0

Je ne sais pas ce que vous voulez acheter vous ne pouvez regarder cette http://msdn.microsoft.com/en-us/ library/system.windows.media.imaging.writeablebitmap.aspx –

Répondre

0

Ci-dessous exemple affiche une image créée à partir d'une BitmapSource avec une ligne rouge sur le dessus de celui-ci. Est-ce ce que vous essayez d'accomplir?

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> 

code derrière:

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

Programmeur Merci pour votre réponse. En fait j'essaye d'écrire directement dans le BitmapSource. Puis, à un stade ultérieur, je peux sauvegarder le contenu de BitmapSource dans un autre tampon ou dans un fichier! – HW2015

Questions connexes