返IRandomAccessStreamReference的图像

返IRandomAccessStreamReference的图像

本文介绍了Xamarin表单-往返IRandomAccessStreamReference的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于个人需要,对于 Xamarin.Forms.Map 控件,我需要创建一个CustomPin扩展名. UWP零件(PCL项目)

For personal needs, for the Xamarin.Forms.Map control, I need to create a CustomPin extension. UWP part (PCL project)

我创建一个像这样的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的大小.

However, by this way, I can't set the Image's size.

然后,我想使用PCL部件中的Image,调整其大小并将其转换为IRandomAccessStreamReference.为了实现它,我需要将我的Image转换为流,但是我找不到使它工作的方法><

I then want to use an Image from my PCL part, resize it and convert it into a IRandomAccessStreamReference. To realize it, I need to convert my Image into a stream, but I can't find the way to make it works ><

所需功能示例:

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

注意:Image参数 img Xamarin.Forms.Image

那么首先可以吗?如果是,那么感谢您提供的任何帮助..我已经在搜索有关如何调整MapIcon大小的信息,并且不可能直接从类[MapIcon]中进行.( https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls .maps.mapicon.aspx )

So first, is it possible? If yes, then thank for any help which could help me.. I already search about how to resize the MapIcon and it's not possible directly from the class [MapIcon].(https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx)

感谢帮助!

推荐答案

您是对的.我们无法调整 MapIcon的大小直接,因为它不提供此类属性或方法. MapIcon的大小主要由 MapIcon.Image 属性.而且我们可以设置图像的大小,而无需使用 Xamarin.Forms.Image .

You are right. We can't resize the MapIcon directly as it doesn't provide such properties or methods. MapIcon's size is mostly controlled by the size of image which is set by MapIcon.Image property. And we can set this image's size without using Xamarin.Forms.Image.

要设置此图像的大小,我们可以利用 BitmapDecoder类 BitmapEncoder类 BitmapTransform类 ,如下所示:

To set this image's size, we can take advantage of BitmapDecoder class, BitmapEncoder class and BitmapTransform class like following:

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);
    }
}

然后我们可以使用此方法先创建一个调整大小的图像流引用,然后将其设置为MapIconImage,例如:

And then we can use this method to create a resized image stream reference first and then set it as 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)
});

这篇关于Xamarin表单-往返IRandomAccessStreamReference的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:25