Question

I want to show a brush on my stackpanel in the following behavior:

  • Brush is an image 240x120
  • panel is 240x60

I want to show a part of the brush like rect(0, 30, 240, 60) (so that the image on the panel is moved down a bit) tried it with viewport and Viewbox with no result (empty Panel)

This is my code:

for (int i = 0; i < listExplorationData.Count; i++)
{ 
    StackPanel panelLoop = new StackPanel();
    panelLoop.Name = "panel_" + i.ToString();
    panelLoop.Width = 240;
    panelLoop.Height = 60;
    panelLoop.Margin = new Thickness(0, 60 * i, 0, 0);

    BitmapImage image = new BitmapImage(
        new Uri("pack://application:,,,/GW2-MyWorldExploration;component/Images/" +
                listExplorationData[i].mapname_en.Replace(" ", "_") +
                "_loading_screen.jpg"));
    ImageBrush brush = new ImageBrush();
    brush.ImageSource = image;
    brush.Stretch = Stretch.None;
    brush.Viewport = new Rect(0, 30, 240, 60);
    panelLoop.Background = brush;
    mainStackPanel.Children.Add(panelLoop);
 }
Was it helpful?

Solution

In order to show part of the ImageBrush you would have to set the Viewbox. If you want to specify the viewbox in absolute units, you would also have to set the ViewboxUnits property:

brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.Viewbox = new Rect(0, 30, 240, 60);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top