WPF – 代码动态更换按钮的背景图片
原创文章,作者:StubbornHuang,如若转载,请注明出处:《WPF – 代码动态更换按钮的背景图片》https://www.stubbornhuang.com/857/
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;
当前分类随机文章推荐
- WPF - 代码动态更换按钮的背景图片
- mciSendCommand在Win10上奔溃的问题解决/循环播放音乐
- Windows - 虚拟按键Virtual-Key Codes大全
- Windows平台录音类封装:AudioRecordWindows
- Windows 批处理文件bat执行完自动退出cmd黑窗口
- PlaySound 播放内存中的音频数据
- Visual Studio - 将程序的日志输出到Visual Studio即时窗口
- 用MFC写一个简易的恶作剧QQ好友的聊天轰炸机
全站随机文章推荐
- Centos7 - 防火墙操作以及开启和关闭特定端口
- 姿态估计之COCO数据集骨骼关节keypoint标注对应
- Python:UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 12305,以及中文乱码的解决方案
- 资源下载 - OpenGL着色语言PDF中文高清版下载
- WordPress - 应对暴力破解登录和规避DDOS的几种方法
- C++ - vector存储动态指针时正确释放内存
- WordPress - 在每一个文章内容末尾都加一个作者信息展示框
- 矩阵 - 行主序矩阵与列主序矩阵
- Duilib - 设置窗体阴影
- 左手坐标系与右手坐标系