c# - 将文件添加到属性后查找文件路径

标签 c# image visual-studio-2013

将文件添加到属性后尝试查找文件的路径效果不佳。在我的属性中,该文件如下所示:

internal static System.Drawing.Bitmap one {
        get {
            object obj = ResourceManager.GetObject("one", resourceCulture);
            return ((System.Drawing.Bitmap)(obj));
        }
    }

如何找到该文件的路径? 然后我尝试像这样使用它:

System.Reflection.Assembly thisExe;
        thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream file =
            thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
        this.pictureBox1.Image = Image.FromStream(file);

当我运行此代码时:

System.Reflection.Assembly thisExe; 
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();
string list = "";

// Build the string of resources.
foreach (string resource in resources)
list += resource + "\r\n";

它为我提供了以下路径:“WFA1.Form1.resources”和“WFA1.Properties.Resources.resources”

可以添加我的资源已嵌入。

如果您需要更多信息,请告诉我。

所以我想要的是我的文件的路径,或者有关如何找到该路径的信息。环顾四周后,他们说这应该可行:

System.IO.Stream file =
            thisExe.GetManifestResourceStream("[WindowsFormsApplication1.Form1.resources].[a.jpg]");

IE:

System.IO.Stream file =
            thisExe.GetManifestResourceStream("[Namespace].[file and extension]");

所以看来我的命名空间是错误的,因为它仍然在这一行返回 null:

this.pictureBox1.Image = Image.FromStream(file);

最佳答案

我已经看过这个,我不相信有一种方法可以在不进行一些认真的字符串构建/操作的情况下获取路径。这当然可能会导致更大的问题。

我认为这段代码是不够的:

var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.one);
pictureBox1.Image = bmp;

而不是这个:

var thisExe = Assembly.GetExecutingAssembly();
var file = thisExe.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one");
if (file != null) pictureBox1.Image = Image.FromStream(file);

选项 2:

var assembly = System.Reflection.Assembly.GetExecutingAssembly(); 
var stream =  assembly.GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.one.jpg");
var tempPath = Path.GetTempPath(); 
File.Save(stream, tempPath);

关于c# - 将文件添加到属性后查找文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896192/

相关文章:

c# - C#-如何删除两个计时器之间的间隔

tfs - 将 TFS 工作区从 "Server"转换为 "Local"失败并出现错误 "Not a valid Win32 FileTime"

winforms - Visual Studio 2013不会显示Winforms,错误 'A reference to the component '系统'项目中已存在'

c++ - 从盘符获取 NTFS 卷 GUID

c# - 如何在 C# 中使用 Expression.IfThenElse

c# - 如何在 UWP 应用程序中打印 pdf 文件后减少内存消耗

css - 制作图像以适合其父尺寸

android - 如何在 sqlite-android 中存储和检索图像

java - 带布局管理器的 PaintComponent

c# - 将 TimeSpan 值四舍五入为第一个整数