Xamarin Forms - 图像传入/传出 IRandomAccessStreamReference

2023-12-26

为了个人的需要,为了Xamarin.Forms.Map控制,我需要创建一个CustomPin扩大。UWP部分(PCL项目)

我创建一个MapIcon喜欢它:

nativeMap.MapElements.Add(new MapIcon()
{
    Title = pin.Name,
    Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Pin/customicon.png")),
    Location = new Geopoint(new BasicGeoposition() { Latitude = pin.Position.Latitude, Longitude = pin.Position.Longitude }),
    NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0)
});

但是,通过这种方式,我无法设置Image的尺寸。

然后我想用一个Image从我的 PCL 部分,调整其大小并将其转换为IRandomAccessStreamReference。为了实现这一点,我需要转换我的Image进入流,但我找不到让它工作的方法>

所需功能示例:

private IRandomAccessStreamReference ImageToIRandomAccessStreamReference(Image image)
{
    //Here I can set the size of my Image

    //I convert it into a stream
    IRandomAccessStreamReference irasr = RandomAccessStreamReference.CreateFromStream(/* img? */);

    //irasr is then created from img

    //I return the IRandomAccessStreamReference needed by the MapIcon element
    return irasr;
}

Note: The Image范围img is a Xamarin.Forms.Image

那么首先,这可能吗?如果是,那么感谢任何可以帮助我的帮助。我已经搜索了如何调整 MapIcon 的大小,但不可能直接从类 [MapIcon] 中直接进行操作。(https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx)

感谢您的帮助!


你是对的。我们无法调整大小MapIcon https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx直接,因为它不提供此类属性或方法。 MapIcon 的大小主要由图像的大小控制,图像的大小由地图图标.Image https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.image.aspx财产。我们可以设置该图像的大小而不使用Xamarin.Forms.Image.

要设置该图像的大小,我们可以利用位图解码器类 https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.graphics.imaging.bitmapdecoder.aspx, 位图编码器类 https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.graphics.imaging.bitmapencoder.aspx and 位图变换类 https://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.imaging.bitmaptransform.aspx像下面这样:

private async System.Threading.Tasks.Task<RandomAccessStreamReference> ResizeImage(StorageFile imageFile, uint scaledWidth, uint scaledHeight)
{
    using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
    {
        var decoder = await BitmapDecoder.CreateAsync(fileStream);

        //create a RandomAccessStream as output stream
        var memStream = new InMemoryRandomAccessStream();

        //creates a new BitmapEncoder and initializes it using data from an existing BitmapDecoder
        BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);

        //resize the image
        encoder.BitmapTransform.ScaledWidth = scaledWidth;
        encoder.BitmapTransform.ScaledHeight = scaledHeight;

        //commits and flushes all of the image data
        await encoder.FlushAsync();

        //return the output stream as RandomAccessStreamReference
        return RandomAccessStreamReference.CreateFromStream(memStream);
    }
}

然后我们可以使用这个方法先创建一个调整大小的图像流引用,然后将其设置为MapIcon's Image like:

var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Pin/customicon.png"));
var imageReference = await ResizeImage(file, 64, 64);

nativeMap.MapElements.Add(new MapIcon()
{
    Title = pin.Name,
    Image = imageReference,
    Location = new Geopoint(new BasicGeoposition() { Latitude = pin.Position.Latitude, Longitude = pin.Position.Longitude }),
    NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0)
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin Forms - 图像传入/传出 IRandomAccessStreamReference 的相关文章

随机推荐

  • 如何确定Microsoft Edge是否是默认浏览器?

    是否有可靠的编程方式来确定 Microsoft Edge 是默认浏览器 我知道一种选择是使用IApplicationAssociationRegistration QueryCurrentDefault https msdn microso
  • 如何在 Laravel 9 Validation 中实现上述验证?

    我正在尝试实现某些 Laravel 验证标准 但我很难实现这些标准 请同样帮助我 我正在使用 laravel 9 和 PHP 8 1 描述 我有一个 API 其中包含以下提到的输入 A 公司名称 必填b company number 根据条
  • 使用 telegram bot API 创建新组

    如何使用 Telegram 中的机器人创建新群组 据我所知 机器人本身无法做到这一点 那么是否有可能将机器人实现为常规程序user How Update 这就是我要的http t me polyglossia http t me polyg
  • 为数据网格行创建上下文菜单

    我有一个可能有很多行的数据网格 当用户右键单击其中一行时 我需要为每一行显示一个上下文菜单 并在用户单击该选项时执行一个操作 相同的操作 但根据当前选定的行不同的数据项 对此最好的策略是什么 我担心每一行的 ContextMenu 都太过分
  • preRenderView 禁用 ajax

  • typedef struct 与 Object - 优点

    我计划定义一个类 其属性中包含 x y 网格的坐标 但是 我不确定实现此设计的 最佳 方法 这是一个很简单的问题 我只想正确地做并有一个理由 一种解决方案是拥有两个属性 类型为 int 一个用于 x 一个用于 y 在对象内 另一种是定义一个
  • 在 Django 中获取自己的应用程序

    有没有一种方法可以获取属于 Django 项目本身的应用程序列表 忽略安装的应用程序 pip 换句话说 我可以排除安装的应用程序吗 pip from settings INSTALLED APPS 您可以使用以下方式获取所有 django
  • app.listen() 和 app.get() 如何在express和hapi上工作

    使用 http 节点模块 仅限本机模块 我如何重新创建 app listen 和 app get 使用带有构造函数的 http 模块 var app function opts this token opts token app proto
  • 如何更改azure函数的python版本

    当我发布我的天蓝色云函数时 我收到消息 本地 python 版本 3 9 7 与部署的 Function App 的预期版本不同 这可能会导致 Azure Functions 中出现 ModuleNotFound 错误 请创建版本 3 9
  • 重定向路由严格斜杠

    我尝试对 webapp2 重定向路由使用严格斜杠 但收到此值错误 ValueError Routes with strict slash must have a name 这是我的路线之一的示例 RedirectRoute r handle
  • Perl 6 的 shell() 使用哪个 shell?

    Perl 6 的shell https docs perl6 org routine shell向 shell 发送命令 但没有说明那是什么 我一直得到bash在我的机器上 但我不知道我是否可以信赖它 perl6 e shell Q ech
  • 返回对象的保留/释放

    我是 Objective C 的新手 所以这可能是一个愚蠢的问题 我不禁看到 ObjC 和 Microsoft 的 COM 在内存管理方面的相似之处 AddRef Release vs retain release 在 COM 环境中 或多
  • 从推送通知打开 Android 应用程序

    有一个小问题一直困扰着我 我已将应用程序设置为接收来自 Urban Airship 的推送通知 一切正常 但是当我点击通知中心中的通知时 没有任何反应 我希望我的应用程序在用户点击推送通知时打开 我该怎么做才能实现此目的 一如既往 我们非常
  • 在执行繁重的 JavaScript 处理时强制 HTML5 画布重绘?

    这个问题与这位较老的 https stackoverflow com questions 1209689 how can i force the browser to redraw while my script is doing some
  • SQL SELECT INSERT INTO 生成唯一 ID

    我试图选择一个数据表并将该数据插入到另一个具有相似列名的文件中 它本质上是重复的数据 当前语法如下 INSERT INTO TABLE1 id id2 col1 col2 SELECT similiarId similiarId2 simi
  • google-services.json 是否安全,不会受到黑客攻击?

    如果黑客反编译了我的 APK 他是否能够从此文件中看到我的 API 密钥 我不担心我的源代码存储库 我只是担心黑客能够以某种方式从我的 APK 中看到这个 API 密钥 我正在尝试加密该文件并在运行时解密它 但遇到一些问题 谷歌插件的设置方
  • 保持 MaxPermSize 小有什么好处吗?

    假设是 64 位 JVM 保持 MaxPermSize 小有什么显着的好处吗 这是在频繁重新部署且存在类加载器泄漏的 Java EE 应用程序的上下文中 作为中期解决方法 将 MaxPermSize 提高到一个荒谬的值似乎非常合理 只要它不
  • 删除具有上次修改日期条件的多个 s3 存储桶文件

    如何删除多个 S3 文件上一次更改日期条件 我在 s3 上有这个文件夹结构 dentca lab dev sample 2019 03 13 file1 最后修改时间 2019 年 3 月 13 日下午 2 34 06 GMT 0700 f
  • 单击 jqGrid 中的行获取列的值

    我在用Asp Net C 在我正在使用的页面之一中jqGrid显示用户列表Admin The jqGrid包含以下列 用户代码 名 中间名字 姓 Email 这是我的标记
  • Xamarin Forms - 图像传入/传出 IRandomAccessStreamReference

    为了个人的需要 为了Xamarin Forms Map控制 我需要创建一个CustomPin扩大 UWP部分 PCL项目 我创建一个MapIcon喜欢它 nativeMap MapElements Add new MapIcon Title