FileImageSource から System.IO.Stream に変換する方法 | Xamarin.Forms
- 2017/04/15
- 13:23
今回は画像処理で、取得した FileImageSource を System.IO.Stream に変換する方法をご紹介いたします。最終的には Byte 配列に変換し、Byte 配列を Image コントロールにバインドするための布石となるソースとなります。
前提条件
・Windows10
・Visual Studio 2015 Community Update3
・Xamarin 4.3.0.784 (NuGet Xamarin.Forms 2.3.4.224)
・macOS Sierra 10.12.4 / Xcode8.3.1 / Xamarin.iOS 10.4.0.123
1.System.IO.Streamに変換するソース
FileImageSource から Stream に変換する関数を作成します。PCLに記述します。
ImgConverter.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Xamarin.Forms;
public static class ImgConverter
{
public static Stream GetStreamFromImageSource(ImageSource source)
{
StreamImageSource streamImageSource = (StreamImageSource)source;
System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
Task<Stream> task = streamImageSource.Stream(cancellationToken);
Stream stream = task.Result;
return stream;
}
}
2.使用例
任意のxamlページ等に記述します。TestPage.xaml.cs
using Xamarin.Forms;
public partial class TestPage : ContentPage
{
void OnConvertButtonClick
{
//ファイルからImageSourceを取得する
ImageSource imgSource = ImageSource.FromResource(@"ソリューション名.Resources.Images.サンプル.png");
//ImageSourceからStreamを取得する
Stream stream = ImgConverter.GetStreamFromImageSource(imgSource);
}
}
次回はこの Stream に変換した変数を Byte 配列に変換する方法についてご紹介いたします。
http://itblogdsi.blog.fc2.com/blog-entry-141.html
最後までお読みいただきありがとうございます。
当ブログの内容をまとめた Xamarin逆引きメニュー は以下のURLからご覧になれます。
http://itblogdsi.blog.fc2.com/blog-entry-81.html
- 関連記事
-
- バイト配列(Byte[])の画像データをImageコントロールにバインドする方法 | Xamarin.Forms
- System.IO.Streamをバイト配列(Byte[])に変換する方法 | Xamarin.Forms
- FileImageSource から System.IO.Stream に変換する方法 | Xamarin.Forms
- No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version'). のエラーが出力される | Xamarin.Forms
- iOSやAndroidなどデバイス毎に処理を分岐する方法 | Xamarin.Forms
System.IO.Streamをバイト配列(Byte[])に変換する方法 | Xamarin.Forms ホーム
No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version'). のエラーが出力される | Xamarin.Forms