Domanda

I have a problem with Uri and my image file.

I tested a lot of things on how to make it work but without success.

(I have image A on start. I click on image and image A change to B.) Please can anyone expain me this problem? I know here is lot questions about this but i still dont understand it. Thx in advance

XAML:

<Image x:Name="obr_0_1" Grid.ColumnSpan="1"
Grid.Column="0" Grid.Row="0"
Tapped="obr_0_1_Tapped" Loaded="obr_0_1_Loaded"/>`


C#:

private void obr_0_1_Tapped(object sender, TappedRoutedEventArgs e)
{
   zmenObrazek();
}

private void zmenObrazek()
{
   obr_0_1.Source = new BitmapImage(new Uri("/Content/Obrazky/half-life.png", UriKind.Relative));       
}

When I set the source this way, I get:

An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not > handled in user code

How do I set the image source from code ?

È stato utile?

Soluzione

What about implementing the following static class:

using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

// I don't know your namespace but put it in the same namespace as your code above
// (or reference this namespace in your code above)
namespace MyNamespace
{
  public static class Helper
  {
    public static Image CreateImage(string fileName, int desiredPixelWidth)
    {
        Image myImage = new Image();
        //set image source
        myImage.Source = CreateSource(fileName);
        myImage.Width = desiredPixelWidth;

        return myImage;
    }

    public static BitmapImage CreateSource(string fileName)
    {
        var file = new FileInfo(fileName);

        System.Drawing.Image im = System.Drawing.Image.FromFile(file.FullName);

        int actualPixelWidth = im.Width;
        Uri fileUri = new Uri(file.FullName);
        // Create source
        BitmapImage myBitmapImage = new BitmapImage();

        // BitmapImage.UriSource must be in a BeginInit/EndInit block
        myBitmapImage.BeginInit();
        myBitmapImage.UriSource = fileUri;

        // To save significant application memory, set the DecodePixelWidth or   
        // DecodePixelHeight of the BitmapImage value of the image source to the desired  
        // height or width of the rendered image. If you don't do this, the application will  
        // cache the image as though it were rendered as its normal size rather then just  
        // the size that is displayed. 
        // Note: In order to preserve aspect ratio, set DecodePixelWidth 
        // or DecodePixelHeight but not both.
        myBitmapImage.DecodePixelWidth = actualPixelWidth;
        myBitmapImage.EndInit();

        return myBitmapImage;
    }
  }
}

Then in your source code you do the following

private void zmenObrazek()
{
   // do you need the first forward slash?
   // (I assume "Content" is a folder in the bin directory)
   obr_0_1.Source = Helper.CreateSource("Content/Obrazky/half-life.png");       
}

Is the image above (half-life.png) found in the bin folder under Content/Obrazky?

If not you may also want to change the image property, Copy to Output Directory, to "Copy always" (when you add an image to project folder the default for this property is "Do not copy").

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top