2009-08-31 9 views
1

Je suis très nouveau à WPF et essaye de porter une application de VB6 à C# et XAML.Comment coller et superposer des images dans WPF?

Ce que je dois faire maintenant est de créer une grande image parmi un certain nombre de petits, disposés comme une série de "carreaux". Certains de ces plus petits auront des superpositions superposées sur eux.

Dans VB6, l'accomplissement du pavage et de la superposition serait simplement une question d'utilisation de la méthode PaintPicture avec le contrôle PictureBox.

C'est ma tentative du carrelage et superposant en une seule étape (si vraiment la superposition pourrait se produire à l'avance):

ImageDrawing Drawing1 = new ImageDrawing(new BitmapImage(new Uri(@"c:\one.bmp", 
              UriKind.Absolute)), 
             new Rect(0, 0, 40, 130)); 

ImageDrawing Drawing2 = new ImageDrawing(new BitmapImage(new Uri(@"c:\two.bmp", 
              UriKind.Absolute)), 
             new Rect(40, 0, 45, 130)); 

ImageDrawing Drawing3 = new ImageDrawing(new BitmapImage(new Uri(@"c:\overlay.bmp", 
              UriKind.Absolute)), 
             new Rect(40, 0, 45, 130)); 

DrawingGroup myDrawingGroup = new DrawingGroup(); 
myDrawingGroup.Children.Add(Drawing1); 
myDrawingGroup.Children.Add(Drawing2); 
myDrawingGroup.Children.Add(Drawing3); 

myImage.Source = new DrawingImage(myDrawingGroup); 

Le carrelage fonctionne très bien, mais la superposition est un no-go. Je me demandais si

  1. quelqu'un pourrait me diriger vers un moyen d'accomplir les superpositions et
  2. quelqu'un pourrait indiquer si c'est la meilleure façon de faire le carrelage.

Merci !!

Répondre

1

je trouve quelque chose dans un post sur un forum MSDN qui m'a permis de résoudre le problème de superposition, aussi, en utilisant GDI + appels:

ImageDrawing Drawing1 = new ImageDrawing(new BitmapImage(new Uri(@"c:\one.bmp", 
                   UriKind.Absolute)), 
                 new Rect(0, 0, 40, 130)); 

ImageDrawing Drawing2 = new ImageDrawing(new BitmapImage(new Uri(@"c:\two.bmp", 
                   UriKind.Absolute)), 
                 new Rect(40, 0, 45, 130)); 

Bitmap bitmap = new Bitmap(@"c:\overlay.bmp"); 

bitmap.MakeTransparent(); 

ImageDrawing Drawing3 = new ImageDrawing(Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), 
                       IntPtr.Zero, 
                       Int32Rect.Empty, 
                       BitmapSizeOptions.FromEmptyOptions()), 
             new Rect(40, 0, 45, 130)); 

DrawingGroup myDrawingGroup = new DrawingGroup(); 
myDrawingGroup.Children.Add(Drawing1); 
myDrawingGroup.Children.Add(Drawing2); 
myDrawingGroup.Children.Add(Drawing3); 

myImage.Source = new DrawingImage(myDrawingGroup); 

Bien que cela fonctionne, il me surprend comme un moyen particulièrement alambiquée à une fin. Sûrement, il y a une façon plus directe, tout-WPF!

Questions connexes