Benutzer mit den meisten Antworten
Grid und Images

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!- Bearbeitet Robert BreitenhoferModerator Dienstag, 25. Januar 2011 13:58 Formatierung
- Verschoben Robert BreitenhoferModerator Dienstag, 25. Januar 2011 14:00 WPF (aus:Visual C#)
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- Als Antwort vorgeschlagen Robert BreitenhoferModerator Dienstag, 25. Januar 2011 13:59
- Als Antwort markiert Robert BreitenhoferModerator Montag, 14. Februar 2011 14:06
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!
-
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- Als Antwort vorgeschlagen Robert BreitenhoferModerator Dienstag, 25. Januar 2011 13:59
- Als Antwort markiert Robert BreitenhoferModerator Montag, 14. Februar 2011 14:06