Image margin behavior - c#

When I set my image margin in run-time it acts strange, different then when I set it via xaml code.
I want the margin value to be (40, 16, 0, 0).
When I add it in run-time, my code is this:
System.Windows.Controls.Image proba = new System.Windows.Controls.Image();
ImageSource imageSource = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");
proba.Source = imageSource;
proba.Width = 2245721 / 9525;
proba.Height = 2286004 / 9525;
proba.Margin = new Thickness(40, 16, 0, 0);
gridName.Children.Add(proba);
But, when I add image in xaml, the code in cs is this:
image1.Source = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");
and the code in xaml is this:
<Window x:Class="GetInfoFromPptxFile.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="720" Width="960">
<Grid Name="gridName">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Image Height="150" HorizontalAlignment="Left" Margin="40,16,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>
When I run my program, image1 is exactly where I want it to be, and the image named proba is not. Why is this when I give them the same margin values?

That's because you're not setting the horizontal and vertical alignments in C#.
Try adding:
proba.VerticalAlignment = VerticalAlignment.Top;
proba.HorizontalAlignment = HorizontalAlignment.Left;
and it'll look the same.

Related

AppWindow Size not matching expected values - WinUI

I made a method that will allow me to lock the size of any Windows in my application. The method itself works perfectly, however when I pass in the fixed size of my XAML content, it doesn't render properly. As you can see from the XAML, the canvas size is locked at 725x305. However, when I pass those values into my method, the AppWindow cuts off a portion of the picture. I have to set the AppWindow closer to 925x425 to get it to show the whole background image. Is anyone able to explain to me what the difference between the XAML & AppWindow height and width? Is it possible to calculate the exact AppWindow size needed based on the content size?
What I expect the window to look like:
What it actually looks like:
<Window
x:Class="Aerloc_App.Views.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Aerloc_App.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Canvas Width="725" Height="305">
<Canvas.Background>
<ImageBrush ImageSource="../Assets/Aerloc_Logo.png"
Opacity=".75"
Stretch="Uniform"/>
</Canvas.Background>
<StackPanel Canvas.Left="400" Canvas.Top="25">
<!-- Username & Password Box -->
<TextBox PlaceholderText="Username"
Text="{x:Bind Path=ViewModel.Username, Mode=TwoWay}"
Width="300"
Opacity="1"/>
<PasswordBox PlaceholderText="Password"
PasswordChanged="{x:Bind Path=ViewModel.PasswordBox_PasswordChanged}"
KeyDown="{x:Bind Path=ViewModel.PasswordBox_KeyDown}"
Width="300"
Margin="0,8,0,8"
Opacity="1"/>
<!-- Error Notification Window -->
<TextBlock Text="{x:Bind Path=ViewModel.ErrorMessage, Mode=OneWay}"
Foreground="GhostWhite"
FontWeight="ExtraBold"
Width="300"
TextAlignment="Right"/>
</StackPanel>
</Canvas>
public void SetWindowSize(Window window, int width, int height, bool isResizable = true)
{
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
var presenter = appWindow.Presenter as OverlappedPresenter;
appWindow.Resize(new Windows.Graphics.SizeInt32 { Width = width, Height = height });
presenter.IsResizable = isResizable;
presenter.IsMaximizable = isResizable;
}

Direct3D Image Brush [duplicate]

It seems like it's quite complicated to load an image in runtime to a WPF window.
Image image;
image = new Uri("Bilder/sas.png", UriKind.Relative);
????.Source = new BitmapImage(image);
I'm trying this code, but I need some help to get it to work. I get some red lines below the code! I also wonder if I need to add some extra code inside the XAML code or is in enough with this:
<Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1"
Stretch="Fill" VerticalAlignment="Top" Width="350" />
Wonder because I have seen examples with sorces to the images inside the XAML tags.
EDIT:
I'm using this now:
var uri = new Uri("pack://application:,,,/sas.png");
var bitmap = new BitmapImage(uri);
image1.Source = bitmap;
The XAML:
<Grid Width="374">
<Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="350" />
<Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="12,226,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" />
<Button Content="Land" Height="23" HorizontalAlignment="Left" Margin="287,226,0,0" Name="btnLand" VerticalAlignment="Top" Width="75" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" />
</Grid>
EDIT 2:
My issue is solved, this code works fine:
BitmapImage image = new BitmapImage(
new Uri("pack://application:,,,/Resources/" + company + ".png"));
image2.Source = image;
In WPF an image is typically loaded from a Stream or an Uri.
BitmapImage supports both and an Uri can even be passed as constructor argument:
var uri = new Uri("http://...");
var bitmap = new BitmapImage(uri);
If the image file is located in a local folder, you would have to use a file:// Uri. You could create such a Uri from a path like this:
var path = Path.Combine(Environment.CurrentDirectory, "Bilder", "sas.png");
var uri = new Uri(path);
If the image file is an assembly resource, the Uri must follow the the Pack Uri scheme:
var uri = new Uri("pack://application:,,,/Bilder/sas.png");
In this case the Visual Studio Build Action for sas.png would have to be Resource.
Once you have created a BitmapImage and also have an Image control like in this XAML
<Image Name="image1" />
you would simply assign the BitmapImage to the Source property of that Image control:
image1.Source = bitmap;
Make sure that your sas.png is marked as Build Action: Content and Copy To Output Directory: Copy Always in its Visual Studio Properties...
I think the C# source code goes like this...
Image image = new Image();
image.Source = (new ImageSourceConverter()).ConvertFromString("pack://application:,,,/Bilder/sas.png") as ImageSource;
and XAML should be
<Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0"
Name="image1" Stretch="Fill" VerticalAlignment="Top"
Source="../Bilder/sas.png"
Width="350" />
EDIT
Dynamically I think XAML would provide best way to load Images ...
<Image Source="{Binding Converter={StaticResource MyImageSourceConverter}}"
x:Name="MyImage"/>
where image.DataContext is string path.
MyImage.DataContext = "pack://application:,,,/Bilder/sas.png";
public class MyImageSourceConverter : IValueConverter
{
public object Convert(object value_, Type targetType_,
object parameter_, System.Globalization.CultureInfo culture_)
{
return (new ImageSourceConverter()).ConvertFromString (value.ToString());
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Now as you set a different data context, Image would be automatically loaded at runtime.

Render Xaml Controls from C# code behind in UWP

The Image that I have created from Code behind is visible in my Visual tree but is not rendered on screen when I run my app.
This is my Code Behind
public MainPage()
{
this.InitializeComponent();
//Image that was created in Xaml
image.Source = new BitmapImage(new Uri("ms-appx:///testimage.jpg"));
//Image creation from C# code
Image image1 = new Image();
image1.Width = 200;
image1.Height = 200;
image1.Margin = new Thickness(50, 50, 0, 0);
image1.Source = new BitmapImage(new Uri("ms-appx:///download.jpg"));
//grid is the Grid name in Xaml
grid.Children.Add(image1);
}
This is my XAML
<Page Name="HI"
x:Class="test1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="grid">
<Image x:Name="image" Visibility="Visible" HorizontalAlignment="Left" Height="200" Margin="10,200,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
What am I missing here?
Your Xaml isn't complete. You are missing a closing tag for Page.

how to a zoom option to my wpf app?

I'm working on wpf application , the application draws the countours on an existing image and i want to add a button which give me the hand to zoom the image to see the countours
i added a button and a fonction on .cs but it doesn't work
here's the code cs
namespace AnimationTest
{
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 1000;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
image1.BeginAnimation(ScaleTransform.CenterXProperty, da);
}
}
}
and XAML Code
<Window x:Class="AnimationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="720" Width="1280">
<Grid>
<Image Height="681" HorizontalAlignment="Left" Name="image1" Stretch="None" VerticalAlignment="Top" Width="1258" Source="/AnimationTest;component/Images/world.jpg" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="1171,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
what's the problem ? and how can i fixe it ?
thanks
This has no sens image1.BeginAnimation(ScaleTransform.CenterXProperty, da); You start an animation on an image of a property that don't belong to Image control. You should create a scale transform, set it on the image and then start the animation on scale transform object, not the image.

make process queue graphically in c#

I implemented some cpu scheduling algorithm.and i want to show it in windows form application in c# graphically but i don't know how?
example:
I have these processes:
p1,p2,p3,p4,p5,p6,p7,p8,p9,...
p2 do its process before p1,and p1 before p6 and p6 before p3 and...
I want something like this to show it for me:
http://i.stack.imgur.com/zEWbG.jpg
also each process length change based on its own process time, and show process start time and end time too.
How can I make something like that?
thank u.
I would recommend using WPF. You could achieve this many different ways.
One example is a WPF Grid with multiple columns. You can set the width of the column as a proportion. Eg A width of "3*" would indicate a process that takes half the time as a width of "6*"
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="MainGrid" Height="80">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="4*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle Fill="LightBlue"/>
<Rectangle Grid.Column="1" Fill="LightGreen"/>
<Rectangle Grid.Column="2" Fill="LightPink"/>
<Label HorizontalAlignment="Center" VerticalAlignment="Center">P2</Label>
<Label Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center">P1</Label>
<Label Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">P6</Label>
</Grid>
</Window>
This XAML code would produce this.
http://img835.imageshack.us/img835/4742/picturelx.png
You could programatically add columns using C# code-behind like this.
private void AddColumn()
{
//Create the columndefinition with a width of "3*"
ColumnDefinition column = new ColumnDefinition();
column.Width = new GridLength(3, GridUnitType.Star);
//Add the column to the grid
MainGrid.ColumnDefinitions.Add(column);
//Create the rectangle
Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Beige);
MainGrid.Children.Add(rect);
Grid.SetColumn(rect, 3);
//Create the label
Label label = new Label();
label.VerticalAlignment = System.Windows.VerticalAlignment.Center;
label.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
label.Content = "P4";
MainGrid.Children.Add(label);
Grid.SetColumn(label, 3);
}

Categories

Resources