C# 如何进行图像拉伸以填充此WPF/XAML应用程序?
当我的程序显示一个比XAML中定义的图像GUI对象小的图像时,它不会像我希望的那样被拉伸以适应。 例如,256x256图像仅占据我的512x512图像GUI对象的左上象限。我感到困惑,因为我在XAML代码中设置了Stretch=“Fill”。我还需要做什么?如果您对代码有任何帮助(见下文),我们将不胜感激 定义GUI的XAML代码C# 如何进行图像拉伸以填充此WPF/XAML应用程序?,c#,wpf,image,xaml,stretch,C#,Wpf,Image,Xaml,Stretch,当我的程序显示一个比XAML中定义的图像GUI对象小的图像时,它不会像我希望的那样被拉伸以适应。 例如,256x256图像仅占据我的512x512图像GUI对象的左上象限。我感到困惑,因为我在XAML代码中设置了Stretch=“Fill”。我还需要做什么?如果您对代码有任何帮助(见下文),我们将不胜感激 定义GUI的XAML代码 <Window x:Class="MyProgram.Window1" xmlns="http://schemas.microsoft.com/wi
<Window x:Class="MyProgram.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyProgarm" Height="900" Width="890" Name="mainWindow" Icon="Icon.ico" MouseDown="mouseDown">
<Grid Name="mainGrid" Background="#FFEBE7F0" Width="800.10">
<Border Margin="139,32,0,0" Name="border1" Height="512" VerticalAlignment="Top" HorizontalAlignment="Left" Width="512" Background="#F0D4D0D0" />
<Border Margin="138,602,0,0" Name="border2" Height="256" VerticalAlignment="Top" HorizontalAlignment="Left" Width="256" Background="#F0D4D0D0" />
<Border Margin="400,602,0,0" Name="border3" Height="256" VerticalAlignment="Top" HorizontalAlignment="Left" Width="256" Background="#F0D4D0D0" />
<Image Margin="135,32,0,0" Name="imagePanel1" Stretch="Fill" HorizontalAlignment="Left" Width="512" MouseMove="imagePanelAxl_MouseMove" Height="512" VerticalAlignment="Top" Canvas.Left="-135" Canvas.Top="-32">
</Image>
我用于绘制图像的代码:
byte[] myColorImage=// 256x256 RGB image loaded from disk
int W=256;
int H=256; // dimensions of myColorImage
// Use multiple cores
image.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action<byte[]>(
delegate(byte[] myColorImage_IN)
{
const int dpi = 96;
BitmapSource bmpsrc = BitmapSource.Create(W, H, dpi, dpi, PixelFormats.Bgr24, null, myColorImage_IN, W * 3);
image.Source = bmpsrc;
}
), myColorImage);
byte[]myColorImage=//256x256从磁盘加载的RGB图像
int W=256;
int H=256;//霉色素图像的尺寸
//使用多核
image.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
新行动(
委托(字节[]myColorImage_IN)
{
常数int dpi=96;
BitmapSource bmpsrc=BitmapSource.Create(W,H,dpi,dpi,PixelFormats.Bgr24,null,myColorImage_IN,W*3);
image.Source=bmpsrc;
}
)霉色素成像);
如果您知道磁盘上图像的URI,则应使用控件。使用图像刷代替图像刷,这样可以为您完成工作
<Border.Background>
<ImageBrush x:Name="image" Stretch="UniformToFill"/>
</Border.Background>
我对您的代码有一些评论: 在您的XAML中,您可以命名您的图像:
imagePanel1
在codebehind中,您使用了:
image
在XAML中的图像标记中,您说:
Canvas.Left="-135" Canvas.Top="-32"
此图像标记不在画布中,而是在网格中
在代码背后,您使用dispatcher使事物使用多个核心 dispatcher用于将其他线程中的内容执行到UI线程中。如果图像(您从中使用dispatcher)也在UI线程中(我猜是因为它是一个UI对象),那么这不会使您的应用程序使用多个核心
如果将图像边距设置为0并删除水平和垂直对齐。同时移除宽度和高度。然后你的图像应该完全填满你的窗口 你能帮我测试一下吗
您可以始终使用和Uri将bitmapsource设置为系统上的文件
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative); //instead you can also use Urikind.Absolute
bi3.EndInit();
imagePanel1.Stretch = Stretch.Fill;
imagePanel1.Source = bi3;
正如上面很多人所说,这里是使用ImageBrush的代码。这也会在鼠标悬停时设置图像。 认为这可能会帮助新用户
<Grid.Resources>
<ImageBrush x:Key="AddButtonImageBrush" ImageSource="/DemoApp;component/Resources/AddButton.png" Stretch="Uniform"/>
<Style x:Key="AddButtonImageStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource AddButtonImageBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource AddButtonImageBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Button Content="If Text is also Required" Style="{StaticResource AddButtonImageStyle}"/>
似乎名称不是ImageBrush的属性。。如果是这样的话,是否可以通过编程方式分配图像?x:Name这应该是我的错误。
<Grid.Resources>
<ImageBrush x:Key="AddButtonImageBrush" ImageSource="/DemoApp;component/Resources/AddButton.png" Stretch="Uniform"/>
<Style x:Key="AddButtonImageStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource AddButtonImageBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource AddButtonImageBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Button Content="If Text is also Required" Style="{StaticResource AddButtonImageStyle}"/>