c# - 如何使用资源中的图像?

标签 c# wpf rectangles

<分区>

我想使用程序资源中的图像路径,而不是使用 Windows 中的完整路径。

tlo.Fill = new ImageBrush
{
  ImageSource = new BitmapImage(new Uri(@"D:\Willie\Documents\ColorTester\ColorTester\Resources\1.jpg", UriKind.Absolute))
};

我想使用这样的路径:

ImageSource = new BitmapImage(new Uri("ColorTester.Resources.1.jpg"));

谁能帮我解决这个问题,因为当我尝试使用它时,visual studio 抛出异常“System.UriFormatException”?

最佳答案

我不知道你的图像的正确路径,如果你的项目下有一个名为 Resources 的文件夹并且图像在那里,你可以按如下方式解决它们:

ImageSource = new BitmapImage(new Uri("/Resources/1.jpg", UriKind.Relative));

如果图像在 UserControl 中,而您想在另一个项目中使用它,则可以这样处理:

ImageSource = new BitmapImage(new Uri("pack://application:,,,/{YourAssemblyWhereResourceIsLocated};component/Resources/1.jpg"));

有用的链接:

关于c# - 如何使用资源中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823353/

相关文章:

c# - 数据网格列标题按钮

python - 在pygame中旋转一个矩形(不是图像)

c# - ASP.NET 核心 : Generate Razor Pages for all models

c# - WCF 序列化错误 - 'System.Net.Mail.MailAddress' 无法序列化

c# - 使用两个图像创建按钮样式

c# - 了解 WPF 控件样式和模板

c# - 在工作时间/工作日内每 5 分钟调用一次方法 - Quartz.net

c# - LINQ Select 语句中的条件数学

java - 绘制不同形状java时JColorChooser颜色出现问题

javascript - 在 paper.js 中调整矩形 "onResize"的大小