2017-01-17 20 views
0

私の目標は、各プロジェクトのイメージフォルダに保存された埋め込みイメージをストリームし、バイト配列に変換し、PCLStorageを使用してデバイスのローカルストレージファイルシステムに保存することです。私が理解できない部分は、埋め込まれた画像からストリーミングする方法です。Xamarinはイメージをストリームに変換します

var embeddedImage = new Image { Aspect = Aspect.AspectFit }; 
embeddedImage.Source = ImageSource.FromResource("applicationIcon.png"); 

その後、私はパスを持っていた場合、私はそれをストリーミングすることができ、または私はストリームを持っていた場合、私はbyte []に変換できます。ファイルソースが見つからないため、以下のコードは機能しません(明らかに)。

string localFileUri = string.Empty; 

// Get hold of the file system. 
IFolder localFolder = FileSystem.Current.LocalStorage; 

IFile file = await FileSystem.Current.GetFileFromPathAsync("applicationIcon.png"); 

using (Stream stream = await file.OpenAsync(FileAccess.Read)) 
{ 
    using (var ms = new MemoryStream()) 
    { 
     var byteArray = ms.ToArray(); 

     var storeragePath = await iStorageService.SaveBinaryObjectToStorageAsync(string.Format(FileNames.ApplicationIcon, app.ApplicationId), byteArray); 
     app.IconURLLocal = storeragePath; 
    } 
} 

唯一のオプションは、何らかの種類のリソースロケータを使用して、追加するプロジェクトの種類ごとにそのコードを維持するように思われます。それほどエレガントではない。別の方法がありますか?

+0

埋め込みリソースを探しています。次に、Assembly.GetManifestResourceStreamメソッドを使用してストリームにアクセスします。 –

+0

また、イメージのビルドアクションが埋め込みリソースであることを確認する必要があります。 –

+0

deckertron_9000ありがとうございました。それは私にそれを理解する正しい道に置いた! –

答えて

1

まず第一に、それを考え出すに正しい道に私を置くためdeckertron_9000をお願い致します、そして第二に、これらの二つのリンク:最後に

http://www.itgo.me/a/3956119637998661919/xamarin-forms-how-to-load-an-image-from-resources-into-a-byte-array

Xamarin Forms: How to use Embedded Resources in PCL Project for Image

、これは何ですか私のために働いた:

私は最初に自分のPCLのフォルダに画像を追加しました。その後、イメージのビルドアクションを組み込みリソースに変更しました。

第2に、System.Reflectionを使用して追加しました。

第三に、このコードは私のために働いていた:それはあなたのような音

string imagePath = "NameOfProject.Assets.applicationIcon.png"; 
Assembly assembly = typeof(NameOfClass).GetTypeInfo().Assembly; 

byte[] buffer; 
using (Stream stream = assembly.GetManifestResourceStream(imagePath)) 
{ 
    long length = stream.Length; 
    buffer = new byte[length]; 
    stream.Read(buffer, 0, (int)length); 

    var storeragePath = await iStorageService.SaveBinaryObjectToStorageAsync(string.Format(FileNames.ApplicationIcon, app.ApplicationId), buffer); 
    app.IconURLLocal = storeragePath; 
} 
0
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) 
     { 
      await App.CurrentApp.MainPage.DisplayAlert("No Camera", ":(No camera available.", "OK"); 
      return; 
     } 

     _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions 
     { 
      Directory = "Sample", 
      Name = "test.jpg" 
     }); 

     if (_mediaFile == null) 
      return; 

     //ViewModel.StoreImageUrl(file.Path); 

     await App.CurrentApp.MainPage.DisplayAlert("Snap", "Your photo have been added to your Items collection.", "OK"); 

     ImageSource = ImageSource.FromStream(() => 
     { 
      var stream = _mediaFile.GetStream(); 
      _mediaFile.Dispose(); 
      return stream; 
     }); 
+0

ありがとうVimal。 CrossMediaプラグインを使用すると、パスがあれば動作します。私の場合はパスがなく、Assemblyクラスを使用しなければなりませんでした。 –

関連する問題