The image is cropped when the window is resized - c#

I decided to migrate my application from WindForms to WPF and faced the following problem: My image is cropped inexplicably.
Can you help me? What am I doing wrong?
Before resizing
https://ibb.co/z4yYdJn
After resizing
https://ibb.co/6J60Sfc
<Window x:Class="ClientWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ClientWPF"
mc:Ignorable="d"
Title="Test WPF" Height="535" Width="567" Background="#FF1D1E20">
<Grid>
<Image x:Name="back_1" Source="/Resources/background_1.png" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="None"/>
<Image x:Name="back_2" Source="/Resources/background_2.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" Stretch="None"/>
<Image Margin="192,75,188,236" Width="160" Height="160" Source="/Resources/logo.png" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Window>

I found my mistake. Image cropped due to property: Margin="192,75,188,236"

Related

Positioning controls - WPF

I am in the process of teaching myself WPF and have an issue that is confusing me. In the XAML below when I open the app the label and image are in the top left part of the window, but when I maximize the window they shift towards the middle. What am I missing that will make the controls keep their relative position?
<Window x:Class="WafLuckyDog.Presentation.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:WafLuckyDog.Applications.ViewModels"
mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="994.273" Height="840"
d:DataContext="{d:DesignInstance vm:ShellViewModel}">
<Window.Background>
<ImageBrush ImageSource="{StaticResource Background}"></ImageBrush>
</Window.Background>
<DockPanel Margin="0,0,-539,0">
<Grid>
<Image Name="Logo" HorizontalAlignment="Left" Margin="0,10,0,669" Width="129"
Source="{StaticResource Logo}" />
<Label Content="Label" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Foreground="AntiqueWhite"/>
</Grid>
</DockPanel>
</Window>
First, you have a DockPanel that has no close tag and is doing nothing, it will give a compile error, so remove it. Also, remove the bottom margin and add a Height and VerticalAlignment properties. The VerticalAlignment and HorizontalAlignment ensures a top right corner anchor.
<Window x:Class="WafLuckyDog.Presentation.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:WafLuckyDog.Applications.ViewModels"
mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="994.273" Height="840"
d:DataContext="{d:DesignInstance vm:ShellViewModel}">
<Window.Background>
<ImageBrush ImageSource="{StaticResource Background}"></ImageBrush>
</Window.Background>
<Grid>
<Image Name="Logo" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="129" Height="129"
Source="{StaticResource Logo}" />
<Label Content="Label" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Foreground="AntiqueWhite"/>
</Grid>
</Window>

My content leaves my screen, how can I prevent this?

I am making a full-screen c# application, with WPF.
But my content is leaving my screen because i want the height and width of the application to be dynamic.
This is my current test:
<Window x:Class="examen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:examen"
mc:Ignorable="d"
Background="#024886"
Title="MainWindow" WindowStyle="None" WindowState="Maximized" >
<Canvas>
<Rectangle Fill="#FBB510" Height="84" RadiusX="20" RadiusY="20" Stroke="Black"
VerticalAlignment="Bottom" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"
Canvas.Left="248" Width="89" Canvas.Top="242"/>
</Canvas>
</Window>
How can I make sure the content will stay at its place?
Try this solution...
Suround all of your windows controls with a ViewBox....
<Viewbox>
<Canvas>
<Rectangle Fill="#FBB510" Height="84" RadiusX="20" RadiusY="20" Stroke="Black" VerticalAlignment="Bottom" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown" Canvas.Left="248" Width="89" Canvas.Top="242"/>
</Canvas>
</Viewbox>

How can set absolute position on stackpanel or grid in xaml WPF

Is it possible to set my StackPanel or Grid to be position absolute like CSS.
In CSS is have property Position of the elements and can set to be relative, absolute and is working good.
In XAML can make Grid, StackPanel to use position absolute.
You have to use Canvas in order to set absolute position in WPF.
In case of buttons in a window, here is a sample :
<Window x:Class="tobedeleted.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Button Canvas.Left="10" Canvas.Bottom="20">Bottom left</Button>
</Canvas>
</Window>
The output is :
Feel free to ask if help is needed.
Absolute positioning defeats the purpose of WPF, but I agree, sometimes there is no other way so you have two basic options.
Elements under the root grid
Elements in a canvas that is the same size as the window (as Vasilievski pointed out)
Code example:
<Window x:Class="WpfApplication1.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Window3" Height="300" Width="300">
<Grid>
<Rectangle Fill="Red" Width="100" Height="120"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Panel.ZIndex="13"
Margin="12,34"
/>
<Rectangle Fill="Green" Width="100" Height="120"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="24,54"
/>
<Canvas>
<Rectangle Canvas.Left="5" Canvas.Top="5" Panel.ZIndex="2" Fill="Yellow" Width="120" Height="30" />
<Rectangle Canvas.Left="25" Canvas.Top="17" Panel.ZIndex="0" Fill="Blue" Width="120" Height="30" />
</Canvas>
</Grid>
</Window>
try this.
<StackPanel>
<Canvas>
<TextBlock Canvas.Left="10" Canvas.Top="6" Text="Choose a value from combobox:" Width="180"/>
<ComboBox Canvas.Left="190" Canvas.Top="4" Width="180"></ComboBox>
</Canvas>
</StackPanel>
RESULT:

Adding a background image in Windows Store Apps

Recently I started making a Windows Store App in XAML and in C#. Unfortunately I failed with adding a background png image stored in '/Assets'. Looked everywhere for any glues but I couldn't find anything helpful.
Here is my code:
<Page
x:Class="BoggleV01.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BoggleV01"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Margin="160,100" Width="1040">
<Grid.Background>
<ImageBrush ImageSource="/Assets/Background.png" Stretch="UniformToFill" />
</Grid.Background>
<StackPanel x:Name="levelsStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260"/>
<StackPanel x:Name="playersStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260" Margin="260,0,0,0"/>
<StackPanel x:Name="statisticsStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260" Margin="520,0,0,0"/>
<StackPanel x:Name="settingsStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260" Margin="780,0,0,0"/>
</Grid>
</Page>
you can use the below mentioned code
<ImageBrush ImageSource="/Applicationname;component/Assets/Background.png"
Stretch="UniformToFill" />

Making window size the same as background image size in WPF

I am new to WPF so bear with me. I have set the background image of my window using an ImageBrush and now I want my window's size to be exactly the size of the background image. The obvious solution to this is to simply set the width/height property manually with the pixel measurements of my background image, but this seems too naive. What is the best way to do this? Here is my XAML so far:
<Window x:Class="FruitFactory.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Fruit Factory" Height="576" WindowStyle="None" DataContext="{Binding}" ResizeMode="NoResize" Width="834">
<Window.Background>
<ImageBrush ImageSource="/FruitFactory;component/Graphics/FruitFactoryBackground.png"></ImageBrush>
</Window.Background>
<Window.Resources>
</Window.Resources>
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="52,27,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
I'm using Visual Studio 2010 and .NET 4.0
You can bind Width and Height to PixelWidth and PixelHeight of the ImageSource for ImageBrush like this
Update
Realized my previous solution didn't work because ImageSource didn't have PixelWidth/PixelHeight since it wasn't a BitmapImage. I had to use a BitmapImage resource instead but then the bindings didn't work if I didn't declare the Resource before the bindings (bug anyone?)
<Window x:Class="WindowSameSizeAsBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Fruit Factory"
WindowStyle="None"
DataContext="{Binding}"
ResizeMode="NoResize">
<Window.Resources>
<BitmapImage x:Key="backgroundBrush"
UriSource="/FruitFactory;component/Graphics/FruitFactoryBackground.png"/>
</Window.Resources>
<Window.Width>
<Binding Source="{StaticResource backgroundBrush}" Path="PixelWidth"/>
</Window.Width>
<Window.Height>
<Binding Source="{StaticResource backgroundBrush}" Path="PixelHeight"/>
</Window.Height>
<Window.Background>
<ImageBrush x:Name="imageBrush"
ImageSource="{StaticResource backgroundBrush}"></ImageBrush>
</Window.Background>
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="52,27,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="258,179,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
ImageBrush isn't best solution for such requirements, because it hasn't Width/Height properties and does not present in VisualTree (it's just a tool for rendering).
Layers will help you:
<Window>
<Grid >
<Image Stretch="Fill"
Source="***some uri***"/>
<StackPanel>
<Button Height="40"
Margin="20"
Content="Ololo"/>
</StackPanel>
</Grid>
<Window>

Categories