2012-09-01 61 views
-1

如何更改image1的WPF中的圖像源?C#圖像變化源

<Image 
      Visibility="Visible" 
      Name="image1" 
      Source="/Panorama-Kocka;component/slike/kocka.PNG" 
      Grid.Column="1" 
      Grid.Row="0" 
      Stretch="Fill" 
      /> 
+1

您的圖片有一個名字:'image1'。所以你會做'image1.Source =「C:\ my \ new \ image.png」;',或者類似的東西。這一切都取決於你想要做什麼。 – Jensen

+0

我想在c#中更改圖像源代碼 – dariodsa

回答

1

在您遇到同樣的問題並做了一些閱讀之後,我發現瞭解決方案 - Pack URIs。

我確實在以下代碼:應用後

Authority: application:/// 

Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format: AssemblyShortName[;Version][;PublicKey];component/Path 
    AssemblyShortName: the short name for the referenced assembly. 
    ;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded. 
    ;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded. 
    ;component: specifies that the assembly being referred to is referenced from the local assembly. 
    /Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder. 

三個斜線:

Image finalImage = new Image(); 
finalImage.Width = 80; 
... 
BitmapImage logo = new BitmapImage() 
logo.BeginInit(); 
logo.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/logo.png"); 
logo.EndInit(); 
... 
finalImage.Source = logo; 

的URI被分解成多個部分

Note: The authority component of a pack URI is an embedded URI that points to a package and must conform to RFC 2396. Additionally, the "/" character must be replaced with the "," character, and reserved characters such as "%" and "?" must be escaped. See the OPC for details. 
:已用逗號來代替

當然,請確保您將圖像上的構建操作設置爲Resource。