gpt4 book ai didi

c# - 如何在 C# 中获取资源作为字节数组?

转载 作者:行者123 更新时间:2023-11-30 19:10:46 28 4
gpt4 key购买 nike

我从 Project settings -> Resources 添加图像到我的 c# 项目
如何在运行时获取此图像?
我试试这个:

public byte[] GetResource(string ResourceName)
{
System.Reflection.Assembly asm = Assembly.GetEntryAssembly();

// list all resources in assembly - for test
string[] names = asm.GetManifestResourceNames(); //even here my TestImg.png is not presented

System.IO.Stream stream = asm.GetManifestResourceStream(ResourceName); //this return null of course

byte[] data = new byte[stream.Length];

stream.Read(data, 0, (int)stream.Length);

return data;
}

我这样调用这个函数:

byte[] data = GetResource("TestImg.png");

但是我在项目资源管理器的 Resources 文件夹中看到了我的图像。
enter image description here

谁能告诉我哪里出了问题?
enter image description here

最佳答案

您需要将文件 TestIng.png 设置为“嵌入式资源”。资源名称将是 Resources/TestIng.png

关于c# - 如何在 C# 中获取资源作为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743963/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com