2016-06-22 2 views
1

StreamImageSource를 Byte로 만들려고합니다. "imgPicked"라는 내 이미지는 로그에서 실행할 때 StreamImageSource입니다. 나는 그것을 Byte []로 바꾸고 싶지만 어떻게하는지 잘 모르겠습니다.StreamImageSource를 byte []로 만들려면 어떻게해야합니까?

이것은 내가 가지고있는 코드는 다음과 같습니다

내가 바이트 []에 내 StreamImageSource (imgPicked)을 설정하려면 어떻게
private async void btnPickPicture_Clicked (object sender, EventArgs e) 
{ 
    await cameraOps.SelectPicture(); 
    var file = await cameraOps.SelectPicture(); 
    imgPicked.Source = ImageSource.FromStream(() => file.Source); 

    System.Diagnostics.Debug.WriteLine (imgPicked.Source); 
    //imgPicked is an StreamImageSource 

} 

?

byte[] data = File.ReadAll(imgPicked.Source); 

하지만 "파일"을 찾을 수 없습니다 :

이것은 인터넷 검색 후 지금까지 가지고있는 것입니다. 어셈블리가 누락되었거나 작성자가 "파일"의 상속 대상을 언급하지 않았습니다 (링크 참조) : Is there a cross-platform solution to ImageSource to byte[]?

+0

는'StreamImageSource' 처리를위한 \t'CreateImageWorker'이있는 .. IImageProvider2''에서 상속 ... 당신은 무엇을 시도? REF : https://msdn.microsoft.com/en-us/library/lumia.imaging.streamimagesource.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 허락하지 않습니다. , 나는 지금 당장 대답을 모른다. – Pogrindis

+0

필자는 이것을 가지고 있습니다 :'byte [] data = File.ReadAll (imgPicked.Source);'약간의 googeling 후에는 "File"을 찾을 수 없습니다. 이것은 내가 따라왔다 게시물 : http://stackoverflow.com/questions/27532462/is-there-a-cross-platform-solution-to-imagesource-to-byte – medvedo

+0

[ImageSource를 변환하는 방법의 중복 가능한 to byte array?] (https://stackoverflow.com/questions/26814426/how-to-convert-imagesource-to-byte-array) –

답변

0

그렇지 않습니다. StreamImageSource를 byte []로 변환 할 수 없습니다. 이전에 제공 한 코드를 사용하여 소스 스트림을 사용하여 byte []를 만들 수 있습니다.

await cameraOps.SelectPicture(); 
var file = await cameraOps.SelectPicture(); 

// use the file.Source stream to create a StreamImageSource 
imgPicked.Source = ImageSource.FromStream(() => file.Source); 

// use the file.Source stream to create a byte[] 
byte[] imgData = ReadStream(file.Source); 

public byte[] ReadStream(Stream input) 
    { 
     byte[] buffer = new byte[16*1024]; 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      int read; 
      while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       ms.Write(buffer, 0, read); 
      } 
      return ms.ToArray(); 
     } 

    } 
+0

작동합니다. 와우, 바이트를 내 DB로 보낼 ​​때. 자 마린은 미치광이처럼 느껴지기 시작합니다. 이미지를 업로드하려고했기 때문입니까? – medvedo

+0

예, 잠재적으로 큰 파일을 업로드하고 있습니다. 이미지의 크기를 조정하거나 스레드에서 업로드를 수행하십시오 (모두 바람직 함 모두) – Jason

+0

Ok! 내가 그것을 보내는 viewmodel이나 contentpage에서 크기를 조정합니까? – medvedo

관련 문제