我正在使用C#和WPF。我想在運行時添加一個PNG和JPG到圖像源,但我得到的是說,一個例外:如何添加PNG和JPG在runtıme
Can not implicitly add convert type string to Sytem.Windows.Media.imageSource
using System.IO; //for : input - output
using Microsoft.Win32; //For : OpenFileDialog/SaveFileDialog
using System.Windows.Media.Imaging; //For : BitmapImage etc etc
<Image x:Name="img" Margin="9,13.5,6,0.5" Source="Laugh.ico">
private void ac(object sender, RoutedEventArgs args)
{
OpenFileDialog dlg = new OpenFileDialog();
// Configure open file dialog box
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".PNG"; // Default file extension
dlg.Filter = " (.PNG)|*.PNG"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
img.Source=filename;
}
}
這項工作極大的THX Zortkun THX這麼多;) – 2012-02-15 18:56:41