在的.resx商店形象为字节[],而不是位图 [英] Store Image in .resx as byte[] rather than Bitmap
本文介绍了在的.resx商店形象为字节[],而不是位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
略愚蠢的,但...
有没有一种方法,以防止Visual Studio的治疗中的.resx .jpg文件作为位图,这样我可以访问?一个byte []属性的资源,而不是
我只是想:
字节[] = MYIMAGE My.Resources.MyImage;
解决方案
尝试使用嵌入的资源,而不是
因此,可以说你有一个JPGFoo.jpg,在ClassLibrary1的。将生成操作为嵌入的资源。
然后使用此代码来获取字节
字节[] GetBytes会()
{
VAR总成=的GetType()大会。使用
(VAR流= assembly.GetManifestResourceStream(ClassLibrary1.Foo.jpg))
{
变种缓冲=新的字节[stream.Length]
stream.Read(缓冲液,0,(int)的stream.Length);
返回的缓冲区;
}
}
或者,或者,如果你想要一个更重可使用的方法
字节[] GetBytes会(字符串资源名称)
{
VAR总成=的GetType()。部件;
VAR fullResourceName = string.Concat(assembly.GetName()名称,资源名称。);使用
(VAR流= assembly.GetManifestResourceStream(fullResourceName))
{
变种缓冲=新的字节[stream.Length]
stream.Read(缓冲液,0,(int)的stream.Length);
返回的缓冲区;
}
}
和呼叫
字节VAR = GetBytes会(Foo.jpg);
Slightly daft, but...
Is there a way to prevent Visual Studio treating a .jpg file in a .resx as a Bitmap so that I can access a byte[] property for the resource instead?
I just want:
byte[] myImage = My.Resources.MyImage;
解决方案
Try using an "Embedded Resource" instead
So lets say you have a jpg "Foo.jpg" in ClassLibrary1. Set the "Build Action" to "Embedded Resource".
Then use this code to get the bytes
byte[] GetBytes()
{
var assembly = GetType().Assembly;
using (var stream = assembly.GetManifestResourceStream("ClassLibrary1.Foo.jpg"))
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int) stream.Length);
return buffer;
}
}
Or, alternatively, if you want a more re-usable method
byte[] GetBytes(string resourceName)
{
var assembly = GetType().Assembly;
var fullResourceName = string.Concat(assembly.GetName().Name, ".", resourceName);
using (var stream = assembly.GetManifestResourceStream(fullResourceName))
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int) stream.Length);
return buffer;
}
}
and call
var bytes = GetBytes("Foo.jpg");
这篇关于在的.resx商店形象为字节[],而不是位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文