bil*_*lal 3 c# embedded-resource winforms
我正在用C#开发一个窗口表单.在我的窗口中有一个图片框.我想如果用户没有选择图像,那么默认图像将被加载到图片框中,该图片框保存在我的项目文件夹中.
提前致谢
我想您想知道如何从项目文件夹中获取图片吗?
首先将图片添加到项目中(添加现有项目)并将Build Action设置为Embedded Resource:

那么下面的代码就可以了:
private void SetPicture()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var imgStream = assembly.GetManifestResourceStream("DataGrid.TestImage.jpg"))
{
var img = new Bitmap(imgStream);
Picturebox.Image = img;
}
}
Run Code Online (Sandbox Code Playgroud)
其中"DataGrid"是我的项目名称(您必须自己插入),"TestImage.jpg"是您的图像的名称(如果您将其放入文件夹,您可能也必须提供foldername).Picturebox是一个PictureBox-Control我将图像设置为.