我正在开发一个自定义的WPF .NET 3.5和Visual Studio 2010中的图像控件。
在WinForms中,PictureBox控件有SizeMode属性,其中包括“CenterImage”。
我希望我的图像控件也有这个能力。
有没有办法实现?
谢谢
我的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>
我的自定义控件代码:
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;
}
}
"webcam_backgroud" 是默认 Visual Studio 资源编辑器添加的 png 图像。