简体   繁体   English

以字节为单位获取资源[] C#

[英]Get Resource as byte[] C#

I'm trying to get an image in the resources as a byte[] for insertion into a database. 我正在尝试将资源中的图像作为byte[]插入数据库中。 The resource is at Resources/CatSeal and is a file called index.jpg. 该资源位于Resources / CatSeal中,并且是一个名为index.jpg的文件。

I've looked at this question , but I'm still having trouble. 我已经看过这个问题 ,但仍然遇到麻烦。 I'm getting a NullReferenceException on the indicated line. 我在指示的行上收到了NullReferenceException。 My namespace is DatabaseConnectionTests . 我的命名空间是DatabaseConnectionTests According to this documentation , under "Access Resources" it should follow this format, which I believe I'm doing: 根据此文档 ,在“访问资源”下,它应采用这种格式,我相信我正在这样做:

MyNameSpace.MyImage.bmp MyNameSpace.MyImage.bmp

Here's my code: 这是我的代码:

Stream sourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DatabaseConnectionTests.index.jpg");
using (var memoryStream = new MemoryStream())
{
    sourceStream.CopyTo(memoryStream); // NullReferenceException here
    seal.SealerImage = memoryStream.ToArray();
}
sealDatabaseOperations.Insert(seal);

How can I resolve this so that my resource image is loaded to a byte[] ? 如何解决此问题,以便将资源映像加载到byte[] Thanks in advance. 提前致谢。

Looks like it's not finding your resource. 似乎找不到您的资源。

Try: "DatabaseConnectionTests.Resources.index.jpg" 尝试:“ DatabaseConnectionTests.Resources.index.jpg”

Set a breakpoint in a class in the same assembly and evaluate this: 在同一程序集中的一个类中设置一个断点,并对此进行评估:

this.GetType().Assembly.GetManifestResourceNames()

That will list all resource names avail for that assembly. 这将列出该程序集可用的所有资源名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM