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

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

问题描述

我有一个(希望如此)简单的问题(我没有找到适合我所有搜索的答案).

我使用 Xamarin.Forms 1.4.1-Pre-1.在我的应用中,我有:

byte[] AvatarErfassung;//Bytearray,稍后更新网络服务var Avatar = new Image { HeightRequest = 71, WidthRequest = 61, Horizo​​ntalOptions = LayoutOptions.Start };Avatar.Source = "SymbolMann.jpg";

其中图像SymbolMann.jpg"作为项目资源包含(在每个项目中)并显示在页面上(没有问题).
我现在想将图像放入字节数组中以将其发送到我们的网络服务.我没有找到任何方法来访问图像SymbolMann.jpg"(将其加载到字节数组中)或因此使用(但是)Avatar.Source.

问题:
如何将图像SymbolMann.jpg"放入字节数组AvatarErfassung"中?

感谢您的每一个回答

嗨迪米特里斯

感谢您的(快速)重播.
正如我写的那样,我使用 Xamarin.表单.
图像存储为资源:在ResourcesDrawable(适用于Android)、Resources(适用于iOS)和根目录中(适用于WP).我必须在内容页面上加载图像.

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

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

我有错误信息(翻译成英文):
System.Type 不包含 GetTypeInfo 方法的定义(是否缺少 using?)"

我必须添加特定的用途吗?

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

我必须放置什么类型的东西?

= typeof(????).GetTypeInfo().Assembly:

感谢您的进一步回复.

更新#2:

首先,感谢您的耐心等待……

不幸的是,在 VS2013 中,我没有找到任何resolve"函数来显示缺失的 using - 但我已经使用我自己找到了缺失的 :-)
同时我添加了使用 System.Reflection;

现在,该行:var assembly = this.GetType().GetTypeInfo().Assembly;

被解析和编译没有错误

此外,我已将 .jpg 的类型从Android 资源"更改为嵌入式资源".
然后应用程序崩溃:长长度 = s.Length;因为似乎 s 为空(我认为 jpg 未找到)

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

如果我将 .jpg 的类型更改为编译",我将无法编译应用程序.
错误信息:命名空间不能直接包含成员,如字段或方法.

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

我是否必须在文件名 (SymbolMann.jpg) 中添加一些内容才能找到它?

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

再次说明我的需求:
在注册页面上,默认图像(符号)在标准 Xamarin.Forms 图像(avatar.source = SymbolMann.jpg"/SymbolFrau.jpg")中显示为男性和女性的头像.
.jpg 存储在每个项目(Android、iOS 和 WP)的标准目录中,可以从图像对象访问,而不会出现问题.
然后,用户可以使用默认图像之一或通过 mediapicker 加载另一个.
如果用户点击注册"按钮,我必须从图像创建一个字节数组,以通过 json 将其发送到我们的网络服务.
如果用户通过 mediapicker 选择另一个图像,我有权访问流,问题是,从默认图像(在每个平台中)变成一个字节数组.

再次感谢您的帮助...

解决方案

通过从程序集中读取资源流,您可以轻松地做到这一点:

var assembly = this.GetType().GetTypeInfo().Assembly;//您可以将this.GetType()"替换为typeof(MyType)",其中 MyType 是程序集中的任何类型.字节[]缓冲区;使用 (Stream s = assembly.GetManifestResourceStream("SymbolMann.jpg")){长长度 = s.Length;缓冲区=新字节[长度];s.Read(buffer, 0, (int)length);}//用缓冲区做一些事情

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

公共类 MyCustomPage : ContentPage {}

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

您需要任何 Type 类型的实例(这就是 typeof 关键字返回的基本内容).另一种方法是在项目中包含的任何对象上调用 GetType() 方法.

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

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

如果可以解析类型或方法,则会显示该选项.

此处了解有关 Xamarin Forms 中图像的更多信息.>

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

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";  

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.

Question:
How to get the image "SymbolMann.jpg" into the byte-array "AvatarErfassung" ?

Thanks for every answer

Hi Dimitris

Thanks for your (fast) replay.
As I wrote, I work with Xamarin.Forms.
The images are stored as resources: in ResourcesDrawable (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;

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

Do I have to add a specific using?

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

What do I have to place as type?

= typeof(????).GetTypeInfo().Assembly:

Thanks for a further reply.

Update #2:

Firts, thanks for your patience…

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;

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

is resolved and compiled without error

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)

Further - if I change the type to "Embedded Ressource" - the image is not found any more by Xamarin.Forms (Avatar.Source = "SymbolMann.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.

So… what is the correct type to the resource (so that it can be loaded with assembly.GetManifestResourceStream()?

Do I have to add something to the filename (SymbolMann.jpg) so that it will be found?

How do I have to change Avatar.Source = "SymbolMann.jpg" so that it is found (if I change the type from "Android Resource" to anything else)?

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

EDIT:

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 {}

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

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.

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.

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.

More on images in Xamarin Forms here.

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

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