Xamarin 表单 - 图片 To/From IRandomAccessStreamReference
Xamarin Forms - Image To/From IRandomAccessStreamReference
出于个人需要,对于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
的大小。
然后我想使用 PCL 部分中的 Image
,调整它的大小并将其转换为 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;
}
注: Image
参数img是一个Xamarin.Forms.Image
那么首先,有可能吗?如果是,那么感谢任何可以帮助我的帮助。我已经搜索了如何调整 MapIcon 的大小,并且不可能直接从 class [MapIcon].(https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx)
感谢帮助!
你是对的。我们无法调整 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 属性 的大小。我们可以在不使用 Xamarin.Forms.Image.
的情况下设置此图像的大小
要设置此图像的大小,我们可以利用 BitmapDecoder class, BitmapEncoder class and BitmapTransform class,如下所示:
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
的 Image
,例如:
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.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
的大小。
然后我想使用 PCL 部分中的 Image
,调整它的大小并将其转换为 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;
}
注: Image
参数img是一个Xamarin.Forms.Image
那么首先,有可能吗?如果是,那么感谢任何可以帮助我的帮助。我已经搜索了如何调整 MapIcon 的大小,并且不可能直接从 class [MapIcon].(https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.maps.mapicon.aspx)
感谢帮助!
你是对的。我们无法调整 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 属性 的大小。我们可以在不使用 Xamarin.Forms.Image.
的情况下设置此图像的大小要设置此图像的大小,我们可以利用 BitmapDecoder class, BitmapEncoder class and BitmapTransform class,如下所示:
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
的 Image
,例如:
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)
});