如何拉伸图像以填充此 WPF / XAML 应用程序?

发布于 2024-12-13 18:19:43 字数 1848 浏览 0 评论 0原文

当我的程序显示小于 XAML 中定义的图像 GUI 对象的图像时,它不会像我希望的那样拉伸以适合。 例如,256x256 图像仅占据 512x512 图像 GUI 对象的左上象限。我很困惑,因为我在 XAML 代码中设置了 Stretch="Fill"。我还需要做什么?任何有关代码的帮助(见下文)将不胜感激。

GUI代码:

  <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>

XAML 代码定义了我用来绘制图像的

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);

When my program displays an image which is smaller than the Image GUI object defined in XAML, it does not get stretched to fit as I would like it to.
For example a 256x256 image only occupies the upper left quadrant of my 512x512 Image GUI object. I am puzzled because I have set Stretch="Fill" in the XAML code. What else do I need to be doing? Any help with the code (see below) would be greatly appreciated.

XAML code defining the GUI

  <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>

Code I use to draw the 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

土豪我们做朋友吧 2024-12-20 18:19:43

使用图像画笔代替图像,它将为您完成工作

 <Border.Background>
            <ImageBrush x:Name="image" Stretch="UniformToFill"/>
 </Border.Background>

并且在后面的代码中您可以设置

   image.ImageSource = bmpsrc; // if you can give the URL of the File Located on the Disk

Instead of Image use Image Brush that will do the work for you

 <Border.Background>
            <ImageBrush x:Name="image" Stretch="UniformToFill"/>
 </Border.Background>

And in the Code Behind you can Set

   image.ImageSource = bmpsrc; // if you can give the URL of the File Located on the Disk
听你说爱我 2024-12-20 18:19:43

正如上面很多人所说,这里是使用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}"/>

As many people said above here is the code for using ImageBrush. Which also sets the image while MouseOver.
Thought it might helps new users.

<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}"/>
于我来说 2024-12-20 18:19:43

如果您知道磁盘上图像的 URI,则应该使用 ImageBrush 控件。

If you known the URI of the image on the disk you should use the ImageBrush control.

鼻尖触碰 2024-12-20 18:19:43

我对您的代码有一些评论:

在您的 XAML 中,您命名您的图像:

imagePanel1

在您使用的代码隐藏中:

image

在 XAML 的图像标记中,您说:

Canvas.Left="-135" Canvas.Top="-32"

该图像标记不在画布中,而是在网格中。


在后面的代码中,您使用调度程序使事物使用多个核心?

调度程序用于将其他线程中的内容执行到 UI 线程中。如果图像(您从中使用调度程序)也在 UI 线程中(我猜它是因为它是一个 UI 对象),那么这不会使您的应用程序使用多个核心。


如果将图像边距设置为 0 并删除水平和垂直对齐。还要删除宽度和高度。然后你的图像应该完全填满你的窗口。

你能为我测试一下吗?


您始终可以使用 Uri 将位图源设置为系统上的文件。

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;

I have a few comments on your code:

And in your XAML you name your image:

imagePanel1

and in codebehind you used:

image

In the image tag in the XAML you said:

Canvas.Left="-135" Canvas.Top="-32"

This image tag is not in a canvas,, it is in a grid.


In the code behind you use the dispatcher to make things use multiple cores?

The dispatcher is used to execute thing from other threads into the UI thread. And if image (from which you use the dispatcher) is also in the UI thread (what I guess it is since it is a UI object) then this wont make your application use multiple cores.


If you set the image margin to 0 and remove the horizontal and vertical alignment. Also remove the width and height. Then your image should completely fill your window.

Can you test this for me.


And you can always use and Uri to set the bitmapsource to the file on your system.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文