本文作者: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平台录音类封装:AudioRecordWindows 阅读3798次,点赞0次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读903次,点赞0次
- 用MFC写一个简易的恶作剧QQ好友的聊天轰炸机 阅读2255次,点赞1次
- Visual Studio - 将程序的日志输出到Visual Studio即时窗口 阅读4726次,点赞1次
- WPF - 代码动态更换按钮的背景图片 阅读3018次,点赞0次
- mciSendCommand在Win10上奔溃的问题解决/循环播放音乐 阅读2820次,点赞0次
- Windows 批处理文件bat执行完自动退出cmd黑窗口 阅读4666次,点赞0次
- PlaySound 播放内存中的音频数据 阅读2938次,点赞0次
- Windows - 虚拟按键Virtual-Key Codes大全 阅读3311次,点赞0次
- Gdi+ - 将OpenCV Mat转换为Gdi+ Bitmap 阅读1903次,点赞0次
全站随机文章推荐
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读2964次,点赞0次
- OpenCV - 将图片/视频转换为深度学习模型输入格式,BGR通道转RGB,图片归一化,HWC转CHW 阅读3935次,点赞0次
- 工具网站推荐 - HDR高动态范围图像下载地址 阅读2521次,点赞0次
- 资源分享 - Game AI Pro 2 - Collected Wisdom of Game AI Professionals 英文高清PDF下载 阅读1653次,点赞0次
- 资源分享 - C++大学教程(第九版) 中文高清PDF下载 阅读4178次,点赞1次
- Pytorch - 使用torch.onnx.export将Pytorch模型导出为ONNX模型 阅读6009次,点赞0次
- Unity - Color32[]转IntPtr 阅读2869次,点赞1次
- 计算机图形学 - 3D数学入门之坐标系 阅读947次,点赞0次
- C++11 - 快速学会正则表达式 阅读1253次,点赞2次
- 资源分享 - GPU Pro 360 - Guide to Shadows 英文高清PDF下载 阅读2242次,点赞0次
评论
167