Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm unsure why you try to set it in code. It is easier to set this in XAML:</p> <pre><code>&lt;ScrollViewer Name="MyScrollViewer" CanContentScroll="True" HorizontalScrollBarVisibility="Visible" Grid.Row="1" Grid.Column="1" &gt; &lt;Image Name="newImage" ImageFailed="newImage_ImageFailed" HorizontalAlignment="Right" Width="900" Height="700" Margin="10" Source="/Images/test.jpg" /&gt; &lt;/ScrollViewer&gt; </code></pre> <p>The above should already do the trick. Note that I've also set the <code>Width</code> and <code>Height</code> values for the image. No code is needed to achieve this.</p> <p>And if you really want to set the image in the code on start, you need to be in the constructor of the WPF page/window. The constructor of a class gets called when the class is being created. In this case when the Window or Page needs to be created, the first thing your application does is calling the constructor. In this constructor, the application also learns that some graphical stuff has to be built. That's how constructors work.</p> <p>After <code>InitializeComponent();</code>, you can put in whatever you like. For example:</p> <pre><code>public partial class MainWindow : Page { public MainWindow() { InitializeComponent(); displayImage(); } private void displayImage() { newImage.Width = 900; newImage.Height = 700; BitmapImage testim = new BitmapImage(); testim.BeginInit(); testim.UriSource = new Uri("E:\\WpfApplication1\\Images\\test.jpg"); testim.EndInit(); newImage.Source = testim; } } </code></pre> <p>If the path is going to be dynamic, binding your image is the best practice. In your XAML, have something like:</p> <pre><code>&lt;Image&gt; &lt;Image.Source&gt; &lt;BitmapImage UriSource="{Binding Path=ImagePath}" /&gt; &lt;/Image.Source&gt; &lt;/Image&gt; </code></pre> <p>So the UriSource gets bound to your <code>ImagePath</code>. In the code, have the following:</p> <pre><code>public BitmapImage ImagePath { get { return testim; } } </code></pre> <p>So it gets bound to that. If you change the value, the binding makes sure the value in the GUI changes with it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload