【问题标题】:Get image from Drawable folder in bitmap从位图中的 Drawable 文件夹中获取图像
【发布时间】:2021-05-30 06:27:01
【问题描述】:

如何从 BitmapFactory.DecodeResource 的可绘制文件夹中获取图像。 我试过这个。

img 名称图像保存在可绘制文件夹中然后这个

 Bitmap originalImage = BitmapFactory.DecodeResource(Resource.Drawable.img,0);

但它会产生错误

“无法从 int 转换为 Android.Content.Res.Resources”

【问题讨论】:

标签: xamarin xamarin.forms bitmap xamarin.android


【解决方案1】:

位图图像 = BitmapFactory.DecodeResource(this.Resources,Resource.Drawable.blankImage);

第一个参数返回当前上下文的资源,然后是您的图像资源链接(应该位于“资源”文件夹中)

【讨论】:

  • 请在您的答案中添加一些解释,以便其他人可以从中学习
  • OP 使用了不正确的参数。第一个参数返回当前上下文的资源,然后是您的图像资源链接(应该位于“资源”文件夹中)。
  • 请通过编辑将所有说明 添加到您的答案
  • 已添加。
【解决方案2】:

我能够获取位图解码资源的可绘制图像。 我使用了这个代码。

string imagefileName = "imgimge.png";
                        // Remove the file extention from the image filename
                        imagefileName = imagefileName.Replace(".jpg", "").Replace(".png", "");

                        // Retrieving the local Resource ID from the name
                        int id = (int)typeof(Resource.Drawable).GetField(imagefileName).GetValue(null);

                        // Converting Drawable Resource to Bitmap
                        var originalImage = BitmapFactory.DecodeResource(Xamarin.Forms.Forms.Context.Resources, id);

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多