none
Grid und Images RRS feed

  • Frage

  • Hallo! Ich habe en Grid (nur eine row, mehrere Columns) und möchte durch Code Images zu dem Grid hinzufügen. Wenn ich probehalber rectangles hinzufüge, bekomme ich diese angezeigt:

    ImageGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150) } );
    Rectangle rect1 = new Rectangle();
        rect1.Fill = new SolidColorBrush(Color.FromArgb(0xFF, (byte)rand.Next(0, 0xFF), (byte)rand.Next(0, 0xFF), (byte)rand.Next(0, 0xFF)));
        rect1.SetValue(Grid.RowProperty, 0);
        rect1.SetValue(Grid.ColumnProperty, 0);
        ImageGrid.Children.Add(rect1);
    		Image image = new Image();

    Möchte ich jedoch ein Bild statt rectangle anzeigen, so bekomme ich diese nicht angezigt:

    image .Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
    image .SetValue(Grid.RowProperty, 0);
    image .SetValue(Grid.ColumnProperty, 0);
    ImageGrid.Children.Add(image );
    Was mache ich falsch? Danke für die Hilfe!
    Sonntag, 23. Januar 2011 18:40

Antworten

  • Hallo D.,

    diese Fragen-Typen sind normal besser im WPF-Forum aufgehoben. Aber gut.
    Bei mir funktioniert das - also evtl. ein Pfad falsch o.ä.. Hier ein Beispiel:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Media;
    
    namespace WpfGridImageDemo
    {
     public partial class MainWindow : Window
     {
      static ColumnDefinition colName = new ColumnDefinition();
      static ColumnDefinition colBild = new ColumnDefinition();
      static RowDefinition rowDef1 = new RowDefinition();
      static RowDefinition rowDef2 = new RowDefinition();
      Grid grid = new Grid { Width = 400, Height = 100 };
    
      public MainWindow()
      {
       InitializeComponent();
    
       GridEigenschaftenSetzen();
       SpaltenDefinitionenZufügen();
       ZeilenDefinitionenZufügen();
       TextZelleSetzen(0, 1);
       BildZelleSetzen(BildErstellen(), zeilenIndex:0, spaltenIndex:1);
       //TestRectangleInZelle0();
       TestBildInZelle0();
       this.Content = grid;
      }
    
      private void GridEigenschaftenSetzen()
      {
       grid.HorizontalAlignment = HorizontalAlignment.Left;
       grid.VerticalAlignment = VerticalAlignment.Top;
       grid.ShowGridLines = true;
      }
    
      private void TextZelleSetzen(int zeilenIndex, int spaltenSpan)
      {
       TextBlock txt1 = new TextBlock();
       txt1.Text = "Beispiel-Text";
       txt1.FontSize = 20;
       txt1.FontWeight = FontWeights.Bold;
       Grid.SetColumnSpan(txt1, spaltenSpan);
       Grid.SetRow(txt1, zeilenIndex);
       grid.Children.Add(txt1);
      }
    
      private void ZeilenDefinitionenZufügen()
      {
       grid.RowDefinitions.Add(rowDef1);
       grid.RowDefinitions.Add(rowDef2);
      }
    
      private void SpaltenDefinitionenZufügen()
      {
       grid.ColumnDefinitions.Add(colName);
       grid.ColumnDefinitions.Add(colBild);
      }
    
      Image BildErstellen()
      {
       Image bild = new Image();
       bild.Width = 200;
       BitmapImage myBitmapImage = new BitmapImage();
       myBitmapImage.BeginInit();
       string bildVerzeichnis = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
       string bildPfad = System.IO.Path.Combine(bildVerzeichnis, "Sample Pictures\\Koala.jpg");
       myBitmapImage.UriSource = new Uri(bildPfad);
       myBitmapImage.DecodePixelWidth = 200;
       myBitmapImage.EndInit();
       bild.Source = myBitmapImage;
       return bild;
      }
    
      public void BildZelleSetzen(Image image, int zeilenIndex, int spaltenIndex)
      {
       grid.ColumnDefinitions.Add(
        new ColumnDefinition { Width = new GridLength(150) });
       image.SetValue(Grid.RowProperty, zeilenIndex);
       image.SetValue(Grid.ColumnProperty, spaltenIndex);
       grid.Children.Add(image);
      }
    
      void TestRectangleInZelle0()
      {
       grid.ColumnDefinitions.Add(
         new ColumnDefinition { Width = new GridLength(150) });
       Rectangle rect1 = new Rectangle();
       Random rand = new Random();
       rect1.Fill = new SolidColorBrush(
        Color.FromArgb(0xFF, (byte)rand.Next(0, 0xFF), 
        (byte)rand.Next(0, 0xFF), (byte)rand.Next(0, 0xFF)));
       rect1.SetValue(Grid.RowProperty, 0);
       rect1.SetValue(Grid.ColumnProperty, 0);
       grid.Children.Add(rect1);
      }
    
      void TestBildInZelle0()
      {
       Image image = new Image();
       string bildVerzeichnis = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
       string bildPfad = System.IO.Path.Combine(bildVerzeichnis, "Sample Pictures\\Koala.jpg");
       image.Source = new BitmapImage(new Uri(bildPfad, UriKind.RelativeOrAbsolute));
       image.SetValue(Grid.RowProperty, 0);
       image.SetValue(Grid.ColumnProperty, 0);
       grid.Children.Add(image); 
      }
    
     }
    }
    

    ciao Frank
    Montag, 24. Januar 2011 10:03

Alle Antworten

  • Hallo!

    Wenn ich vorer das Image lade und warte bis es geldane wurde, dann fuktioniert es:

     public void AddImage(BitmapImage bmpImage)
        {
          ImageGrid.ColumnDefinitions.Add(
            new ColumnDefinition { Width = new GridLength(150) });
          Image image = new Image();
          image.Source = bmpImage;
          image.SetValue(Grid.RowProperty, 0);
          image.SetValue(Grid.ColumnProperty, 0);
          ImageGrid.Children.Add(image);
        }
    

    AddImage(...) wird dann aufgerufen,wenn bmpImage vollständig geladen wurde.

    Wenn ich aber eine Pfad fürdas Bild übergebe, dann wird das Bild nicht angezeigt, obwohl das Bild exisatiert (keine Exception).

    Wie kann man das erklären?

    Danke für die Antwort!

     

    Sonntag, 23. Januar 2011 20:02
  • Hallo D.,

    diese Fragen-Typen sind normal besser im WPF-Forum aufgehoben. Aber gut.
    Bei mir funktioniert das - also evtl. ein Pfad falsch o.ä.. Hier ein Beispiel:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Media;
    
    namespace WpfGridImageDemo
    {
     public partial class MainWindow : Window
     {
      static ColumnDefinition colName = new ColumnDefinition();
      static ColumnDefinition colBild = new ColumnDefinition();
      static RowDefinition rowDef1 = new RowDefinition();
      static RowDefinition rowDef2 = new RowDefinition();
      Grid grid = new Grid { Width = 400, Height = 100 };
    
      public MainWindow()
      {
       InitializeComponent();
    
       GridEigenschaftenSetzen();
       SpaltenDefinitionenZufügen();
       ZeilenDefinitionenZufügen();
       TextZelleSetzen(0, 1);
       BildZelleSetzen(BildErstellen(), zeilenIndex:0, spaltenIndex:1);
       //TestRectangleInZelle0();
       TestBildInZelle0();
       this.Content = grid;
      }
    
      private void GridEigenschaftenSetzen()
      {
       grid.HorizontalAlignment = HorizontalAlignment.Left;
       grid.VerticalAlignment = VerticalAlignment.Top;
       grid.ShowGridLines = true;
      }
    
      private void TextZelleSetzen(int zeilenIndex, int spaltenSpan)
      {
       TextBlock txt1 = new TextBlock();
       txt1.Text = "Beispiel-Text";
       txt1.FontSize = 20;
       txt1.FontWeight = FontWeights.Bold;
       Grid.SetColumnSpan(txt1, spaltenSpan);
       Grid.SetRow(txt1, zeilenIndex);
       grid.Children.Add(txt1);
      }
    
      private void ZeilenDefinitionenZufügen()
      {
       grid.RowDefinitions.Add(rowDef1);
       grid.RowDefinitions.Add(rowDef2);
      }
    
      private void SpaltenDefinitionenZufügen()
      {
       grid.ColumnDefinitions.Add(colName);
       grid.ColumnDefinitions.Add(colBild);
      }
    
      Image BildErstellen()
      {
       Image bild = new Image();
       bild.Width = 200;
       BitmapImage myBitmapImage = new BitmapImage();
       myBitmapImage.BeginInit();
       string bildVerzeichnis = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
       string bildPfad = System.IO.Path.Combine(bildVerzeichnis, "Sample Pictures\\Koala.jpg");
       myBitmapImage.UriSource = new Uri(bildPfad);
       myBitmapImage.DecodePixelWidth = 200;
       myBitmapImage.EndInit();
       bild.Source = myBitmapImage;
       return bild;
      }
    
      public void BildZelleSetzen(Image image, int zeilenIndex, int spaltenIndex)
      {
       grid.ColumnDefinitions.Add(
        new ColumnDefinition { Width = new GridLength(150) });
       image.SetValue(Grid.RowProperty, zeilenIndex);
       image.SetValue(Grid.ColumnProperty, spaltenIndex);
       grid.Children.Add(image);
      }
    
      void TestRectangleInZelle0()
      {
       grid.ColumnDefinitions.Add(
         new ColumnDefinition { Width = new GridLength(150) });
       Rectangle rect1 = new Rectangle();
       Random rand = new Random();
       rect1.Fill = new SolidColorBrush(
        Color.FromArgb(0xFF, (byte)rand.Next(0, 0xFF), 
        (byte)rand.Next(0, 0xFF), (byte)rand.Next(0, 0xFF)));
       rect1.SetValue(Grid.RowProperty, 0);
       rect1.SetValue(Grid.ColumnProperty, 0);
       grid.Children.Add(rect1);
      }
    
      void TestBildInZelle0()
      {
       Image image = new Image();
       string bildVerzeichnis = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
       string bildPfad = System.IO.Path.Combine(bildVerzeichnis, "Sample Pictures\\Koala.jpg");
       image.Source = new BitmapImage(new Uri(bildPfad, UriKind.RelativeOrAbsolute));
       image.SetValue(Grid.RowProperty, 0);
       image.SetValue(Grid.ColumnProperty, 0);
       grid.Children.Add(image); 
      }
    
     }
    }
    

    ciao Frank
    Montag, 24. Januar 2011 10:03