private async void UploadBlob(CloudBlobContainer container, StorageFile file, string fileName, int fileId, long fileSize) { using (var fileStream = await file.OpenStreamForReadAsync()) { // Retrieve reference to a blob CloudBlockBlob blob = container.GetBlockBlobReference(fileName); AzureProgressStream progressStream = new AzureProgressStream(fileStream); progressStream.ProgressChanged += pstream_ProgressChanged; progressStream.SetLength(fileSize); progressStream.FileId = fileId; IInputStream inputStream = WindowsRuntimeStreamExtensions.AsInputStream(progressStream); await blob.UploadFromStreamAsync(inputStream); } }
internal static ImageSource ToImageSource(this byte[] rawImageBytes) { if (rawImageBytes == null) { return(null); } BitmapImage img = null; try { using (MemoryStream stream = new MemoryStream(rawImageBytes)) { stream.Seek(0L, SeekOrigin.Begin); img = new BitmapImage(); InMemoryRandomAccessStream streamSource = new InMemoryRandomAccessStream(); IOutputStream outputStreamAt = streamSource.GetOutputStreamAt(0L); WindowsRuntimeSystemExtensions.AsTask <ulong, ulong>(RandomAccessStream.CopyAsync(WindowsRuntimeStreamExtensions.AsInputStream(stream), outputStreamAt)).Wait(); img.SetSource(streamSource); } } catch { } return(img); }
/// <summary> /// MBAASからPDFをダウンロードしてPictureBoxに画像として表示する。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ButtonDownload_Click(object sender, EventArgs e) { using (HttpClient httpClient = new HttpClient()) { // HTTPリクエスト作成 HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, REST_API_DOWNLOAD_URL); // HTTPヘッダの設定(必要に応じて) //httpRequestMessage.Headers.Add(@"key", value); // Basic認証(必要に応じて) //string username = @"username"; //string password = @"password"; //string authorizationParam = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", username, password))); //httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", authorizationParam); Console.WriteLine("<HTTP request Header>-----------------------------------------------------------"); Console.WriteLine(httpRequestMessage.Headers.ToString()); Console.WriteLine("--------------------------------------------------------------------------------"); // HTTPリクエスト送信(非同期) HttpResponseMessage httpResponseMessage = null; try { httpResponseMessage = await httpClient.SendAsync(httpRequestMessage); } catch (Exception exception) { // 通信エラー string message = String.Format("通信エラー : {0}", exception.Message); Console.WriteLine(exception.StackTrace); Console.WriteLine(message); MessageBox.Show(message); return; } // HTTPステータスチェック if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { // 成功 Console.WriteLine("ダウンロード成功"); } else { // 失敗 string message = String.Format("ダウンロードエラー : {0}", httpResponseMessage.ReasonPhrase); Console.WriteLine(message); MessageBox.Show(message); return; } Console.WriteLine("<HTTP request Header>-----------------------------------------------------------"); Console.WriteLine(httpResponseMessage.Headers.ToString()); Console.WriteLine("--------------------------------------------------------------------------------"); try { // HTTPレスポンスからPDFデータを取得 using (Stream pdfStream = await httpResponseMessage.Content.ReadAsStreamAsync()) { // .NETストリームをWindowsランタイムストリームに変換 IInputStream inputStream = WindowsRuntimeStreamExtensions.AsInputStream(pdfStream); // ストリームからPDFオブジェクト作成 PdfDocument pdfDocument = await PdfDocument.LoadFromStreamAsync((IRandomAccessStream)inputStream); // PDFの1ページ目をストリームにレンダリング InMemoryRandomAccessStream inMemoryRandomAccessStream = new InMemoryRandomAccessStream(); using (PdfPage pdfPage = pdfDocument.GetPage(0)) { await pdfPage.RenderToStreamAsync(inMemoryRandomAccessStream); } // ストリームからビットマップオブジェクト作成 Bitmap bitmap = new Bitmap(inMemoryRandomAccessStream.AsStreamForRead()); // PixtureBoxにビットマップを描画 pictureBox.Width = bitmap.Width; pictureBox.Height = bitmap.Height; pictureBox.Image = bitmap; } } catch (Exception exception) { // PDF変換失敗 string message = String.Format("PDFの変換に失敗 : {0}", exception.Message); Console.WriteLine(exception.StackTrace); Console.WriteLine(message); MessageBox.Show(message); } } }