本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:WPF – 代码动态更换按钮的背景图片
原文链接:https://www.stubbornhuang.com/857/
发布于:2020年06月02日 11:10:21
修改于:2020年06月02日 11:10:21

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;
当前分类随机文章推荐
- Windows安装GCC/G++编译器教程 阅读51次,点赞0次
- WPF - 代码动态更换按钮的背景图片 阅读2267次,点赞0次
- Windows 批处理文件bat执行完自动退出cmd黑窗口 阅读3713次,点赞0次
- 用MFC写一个简易的恶作剧QQ好友的聊天轰炸机 阅读1508次,点赞1次
- Windows平台录音类封装:AudioRecordWindows 阅读3050次,点赞0次
- Windows - 虚拟按键Virtual-Key Codes大全 阅读2235次,点赞0次
- Gdi+ - 将OpenCV Mat转换为Gdi+ Bitmap 阅读1116次,点赞0次
- Visual Studio - 将程序的日志输出到Visual Studio即时窗口 阅读3528次,点赞1次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读189次,点赞0次
- mciSendCommand在Win10上奔溃的问题解决/循环播放音乐 阅读2104次,点赞0次
全站随机文章推荐
- C++ - 使用Crypto++/CryptoPP加解密库对字符串或者文件进行AES256加密 阅读926次,点赞1次
- C++ 11 - final关键字简要介绍 阅读1361次,点赞0次
- C++ - Jni中的GetByteArrayElements和GetByteArrayRegion的区别和使用示例 阅读569次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与float的相互转换 阅读391次,点赞0次
- OpenCV - 使用findContours()查找图片轮廓线,并将轮廓线坐标点输出 阅读3868次,点赞0次
- 资源分享 - OpenGL Programming Guide (Eighth Edition) OpenGL红宝书英文第8版 英文高清PDF下载 阅读1064次,点赞0次
- Google Adsense - 被暂停付款,出现我们此时无法处理您的请求,对于给您带来的不便之处,我们深表歉意的问题 阅读952次,点赞0次
- WordPress - 支持用户注册时使用中文名 阅读1307次,点赞0次
- 资源分享 - 鸟哥的Linux私房菜-基础学习篇(第四版)带书签PDF下载 阅读2277次,点赞0次
- WordPress - admin_url()函数,获取网站管理后台url链接 阅读297次,点赞0次
评论
147