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;
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:WPF – 代码动态更换按钮的背景图片
原文链接:https://www.stubbornhuang.com/857/
发布于:2020年06月02日 11:10:21
修改于:2023年06月26日 22:21:44
当前分类随机文章推荐
- Visual Studio - 将程序的日志输出到Visual Studio即时窗口 阅读5560次,点赞1次
- WPF - 代码动态更换按钮的背景图片 阅读3557次,点赞0次
- Gdi+ - 将OpenCV Mat转换为Gdi+ Bitmap 阅读2590次,点赞0次
- 用MFC写一个简易的恶作剧QQ好友的聊天轰炸机 阅读2760次,点赞1次
- Windows - GDI/GDI+区别、用法总结 阅读454次,点赞0次
- C++ - Windows系统使用C++切换音频默认输出设备 阅读75次,点赞0次
- PlaySound 播放内存中的音频数据 阅读3363次,点赞0次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读1382次,点赞0次
- mciSendCommand在Win10上奔溃的问题解决/循环播放音乐 阅读3276次,点赞0次
- Windows安装GCC/G++编译器教程 阅读2458次,点赞0次
全站随机文章推荐
- 资源下载 - Go语言核心编程李文塔 高清带书签版pdf下载 阅读7378次,点赞2次
- 常用的VSCode插件备忘 阅读254次,点赞0次
- 资源分享 - Game Physics Cookbook 英文高清PDF下载 阅读1969次,点赞0次
- 资源分享 - WebGL Gems - Learn How To Create 3D Worlds And Games For Modern Web Browsers, First Edition 英文高清PDF下载 阅读1486次,点赞0次
- 资源分享 - Digital Image Processing , Third Edition 英文高清PDF下载 阅读2231次,点赞0次
- WordPress - 异步发送邮件,解决发送邮件阻塞耗时的问题 阅读502次,点赞0次
- C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型 阅读4272次,点赞0次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第3章 Raster Images中文翻译 阅读2706次,点赞3次
- 深度学习 - 以一个极简单的中英文翻译Demo彻底理解Transformer 阅读1107次,点赞0次
- OnnxRuntime - Linux配置OnnxRuntime开发环境 阅读66次,点赞0次
评论
169