Xamarin.Forms:如何将Resources中的图像加载到字节数组中? [英] Xamarin.Forms: How to load an image from Resources into a byte-array?

查看:897
本文介绍了Xamarin.Forms:如何将Resources中的图像加载到字节数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(希望)简单的问题(我没有找到答案,这符合我的所有搜索)。

I have a (hopefully) simple question (I don’t have found an answer, that fit’s by all of my searches).

我与Xamarin合作。表格1.4.1-Pre-1。
在我的应用程序中,我有:

I work with Xamarin.Forms 1.4.1-Pre-1. In my app, I have:

byte[] AvatarErfassung; // Bytearray, to later update the webservice
var Avatar = new Image { HeightRequest = 71, WidthRequest = 61, HorizontalOptions = LayoutOptions.Start };
Avatar.Source = "SymbolMann.jpg";  

图片SymbolMann.jpg包含在项目资源中 (在每个项目中)并在页面上显示(没有问题)。

我现在想把图像放在一个字节数组中,然后发送到我们的web服务。
我没有发现任何方式访问图像SymbolMann.jpg(以字节数组加载)或使用(但是)Avatar.Source因此。

where the image "SymbolMann.jpg" is included as project-resource (in each project) and showed on a page (without problems).
I now want to put the image in a byte-array to send it to our webservice. I don't have found any way to access the image "SymbolMann.jpg" (to load it in a byte-array) or to use (however) the Avatar.Source therefore.

问题:

如何将图像SymbolMann.jpg放入字节数组AvatarErfassung?

感谢您的每一个答案

您好Dimitris

Hi Dimitris

谢谢对于你的(快速)重播。

正如我写的,我使用Xamarin。表格

图像存储为资源
in \ Resources \Drawable \(适用于Android),\ Resources \(适用于iOS)和根目录(适用于WP)。
我必须加载图像内容页面。

Thanks for your (fast) replay.
As I wrote, I work with Xamarin.Forms.
The images are stored as resources: in \Resources\Drawable\ (for Android), \Resources\ (for iOS) and in the root (for WP). I have to load the image on a content-page.

如果我超越你的代码,就行了:

If I overtake your code, to the line:

var assembly = this.GetType().GetTypeInfo().Assembly;

我有错误信息(翻译成英文):

系统.Type不包含GetTypeInfo方法的定义(使用丢失?)

I have the error-message (translated to English):
"System.Type don’t contain a definition for GetTypeInfo method (is a using missing?)"

我是否必须添加特定的使用?

您写道:
//您可以将this.GetType()替换为typeof(MyType),其中MyType是程序集中的任何类型。

You write: // you can replace "this.GetType()" with "typeof(MyType)", where MyType is any type in your assembly.

我需要输入什么类型?

= typeof(????)。GetTypeInfo()。汇编:

感谢您的回复。

更新#2:

Firts,感谢您的耐心......

Firts, thanks for your patience…

不幸的是在 VS2013 中,我找不到任何显示缺少使用功能的解决方案 - 但是我发现缺少使用我的自我:-)

与此同时,我使用System.Reflection添加了
;

Unfortunately in VS2013, I don’t find any function "resolve" that shows me the missing using - but I have find out the missing using my self :-)
In the meantime I have added using System.Reflection;

现在,行:
var assembly = this.GetType() .GetTypeInfo()。汇编;

Now, the line: var assembly = this.GetType().GetTypeInfo().Assembly;

解析并编译时没有错误

此外,我更改了类型从Android资源到嵌入式资源的.jpg。

应用程序然后崩溃
long length = s.Length;
因为似乎s为空(我认为找不到jpg)

Further, I have changed the type of the .jpg from "Android Resource" to "Embedded Ressource".
The App then crashes in the line: long length = s.Length; as it seems that s is null (I think the jpg is not found)

此外 - 如果我将类型更改为嵌入式资源 - Xamarin.Forms不再找到图像(Avatar.Source =SymbolMann.jpg;)

Further - if I change the type to "Embedded Ressource" - the image is not found any more by Xamarin.Forms (Avatar.Source = "SymbolMann.jpg";)

如果我将.jpg的类型更改为编译我无法编译应用程序。

错误消息:
命名空间不能直接包含字段或方法等成员。

If I change the type of the .jpg to "Compile" I can’t compile the app.
Error-message: A namespace can’t contain directly members like fields or methods.

那么...资源的正确类型是什么(因此可以使用assembly.GetManifestResourceStream()加载?

我是否必须在文件名中添加一些东西(SymbolMann.jpg)以便找到它?

如何更改Avatar.Source =SymbolMann.jpg以便找到它(如果我将类型从Android资源更改为其他任何内容)?

再次满足我的需求:

在注册页面上,默认图像(符号)在标准Xamarin.Forms图像(avatar.source =SymbolMann.jpg/SymbolFrau.jpg)中显示为男人和女人的化身。

.jpg存储在每个项目(Android,iOS和WP)的标准目录,可以在没有图像对象问题的情况下访问它们。

用户可以使用其中一个默认图像或通过mediapicker加载另一个。

如果用户点击注册按钮,我必须从Image创建一个字节数组,通过json将其发送到我们的web服务。

如果用户选择另一个通过mediapicker的图像,我 >访问流,问题是,从默认图像(在每个平台上)成为一个字节数组。

Once again my needs:
On a registration-page, default-images (symbols) are showed as avatar for man and woman in a standard-Xamarin.Forms image (avatar.source = "SymbolMann.jpg" / "SymbolFrau.jpg").
The .jpg’s are stored in the standard-directories for each project (Android, iOS and WP) where the are accessible without problems from the image-object.
The user then can take one of the default-images or load another one over mediapicker.
If the user the tap the button "Register", I have to create a byte-array from the Image to send it via json to our webservice.
If the user select another image via mediapicker, I have access to the stream, the problem is, to become a byte-array from the default-images (in every platform).

再次感谢您的帮助...

Once again thanks for your help...

推荐答案

您可以通过阅读资源流来轻松完成此操作汇编:

You can easily do this, by reading the resource stream from the assembly:

var assembly = this.GetType().GetTypeInfo().Assembly; // you can replace "this.GetType()" with "typeof(MyType)", where MyType is any type in your assembly.
byte[] buffer;
using (Stream s = assembly.GetManifestResourceStream("SymbolMann.jpg"))
{
    long length = s.Length;
    buffer = new byte[length];
    s.Read(buffer, 0, (int)length);
}
// Do something with buffer

编辑:

要获得包含项目嵌入资源的程序集,您需要调用对于该程序集中包含的类型的GetTypeInfo 方法。因此, typeof(X),其中X是程序集中的任何类型。例如,如果您使用名为 MyCustomPage 的自定义页面:

To get the assembly that holds the embedded resources of your project you need to call the GetTypeInfo method on a type included in that assembly. Hence, typeof(X), where "X" is any type in your assembly. For example, if you are using a custom page called MyCustomPage:

public class MyCustomPage : ContentPage {}

...这就是你要传递的内容: typeof(MyCustomPage)

... this is what you would pass: typeof(MyCustomPage)

你需要任何类型的实例输入(这是什么 typeof 关键字基本上返回。。另一种方法是对包含在其中的任何对象调用 GetType()方法你的项目。

You need any instance of type Type (this is what the typeof keyword returns basically). The alternative is to call the GetType() method on any object that is included in your project.

请注意,如果在类型上调用 typeof(X),则无法获得正确的程序集这不包含在您的项目中。例如。调用 typeof(ContentPage).GetTypeInfo()。Assembly ,将返回 ContentPage 类型所在的程序集。是包含您资源的那个。我希望这不会令人困惑。如果是,请告诉我。

Note that you will not be able to get the correct assembly if you call typeof(X) on a type that is not included in your project. Eg. calling typeof(ContentPage).GetTypeInfo().Assembly, would return the assembly that the ContentPage type resides in. Which is not the one that includes your resources. I hope this is not confusing. If it is, please let me know.

现在, GetTypeInfo 方法是一种扩展方法,包括在 System.Reflection 命名空间。当Xamarin Studio中的文本编辑器无法识别某些内容时,它会用红色突出显示它。右键单击它将在上下文菜单中为您提供Resolve选项:

Now, the GetTypeInfo method is an extension method included in the System.Reflection namespace. When the text editor in Xamarin Studio does not recognize something, it highlights it with red. Right-clicking on that will give you a Resolve option in the context menu:

如果可以解析类型或方法,它将显示该选项。

If the type or method can be resolved, it will show that option.

有关Xamarin表格中图片的更多信息此处

More on images in Xamarin Forms here.

这篇关于Xamarin.Forms:如何将Resources中的图像加载到字节数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆