Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ResourceManager和C将PNG作为字节[]获取#_C#_Png_Bytearray_Resourcemanager - Fatal编程技术网

C# 如何使用ResourceManager和C将PNG作为字节[]获取#

C# 如何使用ResourceManager和C将PNG作为字节[]获取#,c#,png,bytearray,resourcemanager,C#,Png,Bytearray,Resourcemanager,我将一个PNG文件导入到Visual Studio 2013中。我们正在使用的MIME邮件库使用一个需要字节数组参数的函数向HTML邮件添加视觉效果。如何将ResourceManager返回的对象放入字节数组 ResourceManager rm; rm = new ResourceManager("Foo.Properties.Resources", typeof(MYFORM).Assembly); var obj = rm.GetObject("Logo"); 当我尝试使用.GetStr

我将一个PNG文件导入到Visual Studio 2013中。我们正在使用的MIME邮件库使用一个需要字节数组参数的函数向HTML邮件添加视觉效果。如何将ResourceManager返回的对象放入字节数组

ResourceManager rm;
rm = new ResourceManager("Foo.Properties.Resources", typeof(MYFORM).Assembly);
var obj = rm.GetObject("Logo");

当我尝试使用
.GetStream
方法时,错误表明对象不是流,而改为使用
.GetObject

如果文件是图像,则GetObject将返回System.Drawing.Image对象

Image img = (Image)rm.GetObject("Logo")
使用图像对象,您可以直接将其保存到任何System.IO.Stream对象

MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Png);
现在,您可以使用Stream.ToArray创建字节的副本

byte[] bytes = stream.ToArray();
或者直接保存到文件中

img.Save(Application.StartupPath + "/testImage.jpg")
不要忘记关闭任何使用过的流

Stream.Close();

如果文件是图像,GetObject将返回System.Drawing.Image对象

Image img = (Image)rm.GetObject("Logo")
使用图像对象,您可以直接将其保存到任何System.IO.Stream对象

MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Png);
现在,您可以使用Stream.ToArray创建字节的副本

byte[] bytes = stream.ToArray();
或者直接保存到文件中

img.Save(Application.StartupPath + "/testImage.jpg")
不要忘记关闭任何使用过的流

Stream.Close();

PNG资源将作为
图像
对象嵌入。如果要将其转换为字节[],则必须使用Image.Save()将其转换为MemoryStream。或者在将文件添加为资源之前重命名该文件,以便VS不知道它是图像。PNG资源将作为
图像
对象嵌入。如果要将其转换为字节[],则必须使用Image.Save()将其转换为MemoryStream。或者在将文件添加为资源之前重命名该文件,以便VS不知道它是图像。