public async Task UploadFile(Windows.Storage.StorageFile file) { var fileName = file.Name; using (Windows.Storage.Streams.IRandomAccessStreamWithContentType stream = await file.OpenReadAsync()) await deviceClient.UploadToBlobAsync(fileName, stream.AsStream()); }
protected async static Task <Windows.Storage.Streams.IRandomAccessStreamWithContentType> GetFileReaderStreamAsync(Windows.Storage.StorageFile fle) { Windows.Storage.Streams.IRandomAccessStreamWithContentType strm = null; strm = await fle.OpenReadAsync(); return(strm); }
public void ShowPage() {//To render PDF page, finally to the image control. //Calculate render size. CalcRenderSize(); PixelSource bitmap = new PixelSource(); bitmap.Width = (int)m_iRenderAreaSizeX; bitmap.Height = (int)m_iRenderAreaSizeY; Windows.Storage.Streams.IRandomAccessStreamWithContentType stream = m_SDKDocument.RenderPageAsync(bitmap, m_iStartX, m_iStartY, m_iRenderAreaSizeX, m_iRenderAreaSizeY, m_iRotation).GetResults(); if (stream == null) { //ShowErrorLog("Error: Fail to render page.", ref new UICommandInvokedHandler(this, &demo_view::renderPage::ReturnCommandInvokedHandler),true, FSCRT_ERRCODE_ERROR); return; } Windows.UI.Xaml.Media.Imaging.BitmapImage bmpImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); bmpImage.SetSource(stream); image.Width = (int)m_iRenderAreaSizeX; image.Height = (int)m_iRenderAreaSizeY; image.Source = bmpImage; }
/* * public override Stream GetWriterStream( string path ) * { * return null; * * /* * byte[] data = Encoding.Unicode.GetBytes(content); * * var folder = ApplicationData.Current.LocalFolder; * var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); * * using (var s = await file.OpenStreamForWriteAsync()) * { * await s.WriteAsync(data, 0, data.Length); * } * * } */ public override System.IO.Stream GetReaderStream(string path, bool bForPreview) { //FAILS... /* * if( bForPreview ) * { * MessageBox.Show( path ); * * StorageFile fle = GetFileObject( path ); * if( fle == null ) * { * MessageBox.Show( "fle is null" ); * return null; * } * * StorageItemThumbnail tn = null; * * //protected async static Task<StorageItemThumbnail> GetFileThumbnailAsync( StorageFile fle ) * * MessageBox.Show( "Before call." ); * * var taskHlp1 = Task.Run(async () => { tn = await GetFileThumbnailAsync(fle); }); * taskHlp1.Wait(); * * if( tn == null ) * { * MessageBox.Show( "tn is null" ); * return null; * } * * MessageBox.Show( "Before AsStream." ); * * Stream strm = tn.AsStream(); * if( strm == null ) * { * MessageBox.Show( "strm is null" ); * return null; * } * * MessageBox.Show( "Stream length: " + strm.Length.ToString() ); * * return strm; * * /* * InMemoryRandomAccessStream win8Stream = GetData(); // Get a data stream from somewhere. * System.IO.Stream inputStream = win8Stream.AsStream() * * For converting from .NET streams -> WinRT streams: * * Windows.Storage.Streams.IInputStream inStream = stream.AsInputStream(); * Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream(); * * // * // // * // * StorageFile storageFile = * await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri); * * var randomAccessStream = await storageFile.OpenReadAsync(); * Stream stream = randomAccessStream.AsStreamForRead(); * * * //return tn.AsStream(); * //return tn.GetInputStreamAt( 0 ).AsStreamForRead(); * } * else */ { //WORKS FINE!!! /* * int iPos = path.LastIndexOf( '\\' ); * if( iPos < 0 ) * return null; * * StorageFolder fldr = GetFolderObject( path.Substring( 0, iPos ) ); * if( fldr == null ) * return null; * * Stream strm = null; * * var taskHlp1 = Task.Run(async () => { strm = await GetFileReaderStreamAsync(fldr, path.Substring( iPos + 1 )); }); * taskHlp1.Wait(); * * return strm; */ Windows.Storage.StorageFile fle = GetFileObject(path); if (fle == null) { MessageBox.Show("fle is null"); return(null); } Windows.Storage.Streams.IRandomAccessStreamWithContentType strm = null; var taskHlp1 = Task.Run(async() => { strm = await GetFileReaderStreamAsync(fle); }); taskHlp1.Wait(); if (strm == null) { return(null); } return(strm.AsStreamForRead()); } }
public async void Run(IBackgroundTaskInstance taskInstance) { var deferral = taskInstance.GetDeferral(); try { Task.Run(async() => { string myConnectionDevice = "HostName=gabp2017.azure-devices.net;DeviceId=TestDevice;SharedAccessKey=3bG0qc+rxpaIyNT8ckCzM1BWJFRav+29mfGimdM7kig="; DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(myConnectionDevice); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Images/GAB.png")); var fileName = file.Name; using (Windows.Storage.Streams.IRandomAccessStreamWithContentType stream = await file.OpenReadAsync()) await deviceClient.UploadToBlobAsync(fileName, stream.AsStream()); }).Wait(); } catch (Exception ex) { var a = ex; } }
private async void BackgroundInit() { // 这是之所以这么纠结,只初始化了一个stream,是因为这里OpenReadAsync只能做异步操作 // 异步的下一行是在另一个线程,而在App.cs里还不能通过Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher; // 来拿到UI线程的Dispatcher // 目前的UI线程Dispatcher是直到MainPage构造了之后才有的 // MainPage构造时拿到这个stream,然后用这个stream去构造他自己的Image Uri imageUri = new Uri(CareConstDefine.MainPageBackgroundURI); var rass = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(imageUri); MainPageBackgroundImageStream = await rass.OpenReadAsync(); }