如何进行图像拉伸以填充此WPF / XAML应用程序? [英] How do I make an image stretch to fill in this WPF / XAML application?

查看:124
本文介绍了如何进行图像拉伸以填充此WPF / XAML应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

定义GUI的XAML代码

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


推荐答案

而不是图像使用Image Brush来完成工作为你

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

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

在Code Behind中你可以设置

And in the Code Behind you can Set

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

这篇关于如何进行图像拉伸以填充此WPF / XAML应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆