1 相對路徑
將在項目exe路徑Resourc目錄下尋找圖片
ImageBrush brush1 = new ImageBrush();
string path = @"Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Relative));
brush1.ImageSource = img;
Testbutton.Background = brush1;
2 絕對路徑方式
2.1 如果圖片沒有導入到項目中
ImageBrush brush1 = new ImageBrush();
string path = @"F:/Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
brush1.ImageSource = img;
Testbutton.Background = brush1;
2.2 如果圖片已經導入到項目中
使用@"pack://application:標誌爲當前項目
ImageBrush brush1 = new ImageBrush();
string path = @"pack://application:,,,/Resource/icon-提交.png";
BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
brush1.ImageSource = img;
Testbutton.Background = brush1;
如果有興趣,可以訪問我的個人站:http://www.stubbornhuang.com