Joa*_*Fdz 8 c# wpf image center alignment
我正在开发WPF .NET 3.5和Visual Studio 2010中的自定义图像控件.
在WinForms中,PicutreBox控件具有SizeMode属性,其中包含" CenterImage ".
我希望我的Image控件具备这种能力.
反正呢?
谢谢
我的XAML代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="768" Width="1024" xmlns:my="http://schemas.sharpsoft.net/xaml" xmlns:my1="clr-namespace:WpfApplication1">
<Grid>
<my1:CustomControl1
x:Name="customControl11"
Width="206"
Height="197"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="18,58,0,0"
Stretch="Uniform"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
我的CustomControl代码:
public class CustomControl1 : Image
{
public CustomControl1()
{
// Bitmap to Stream
Stream ms = new MemoryStream();
Properties.Resources.webcam_background.Save(ms, ImageFormat.Png);
// Stream to BitmapImage
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = ms;
bitmap.EndInit();
// Set it
Source = bitmap;
}
}
Run Code Online (Sandbox Code Playgroud)
其中"webcam_backgroud"是默认视觉工作室资源编辑器添加的png图像.
您应该尝试Image
使用对齐来使整个元素居中:
<Grid>
<Image Stretch="None"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
Run Code Online (Sandbox Code Playgroud)