【发布时间】:2011-03-11 15:36:20
【问题描述】:
我正在 WPF .NET 3.5 和 Visual Studio 2010 中开发自定义图像控件。
在 WinForms 中,PicutreBox 控件具有 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图像。
【问题讨论】:
标签: c# wpf image alignment center