当前位置: 首页>>代码示例>>C#>>正文


C# MemoryStream.AsRandomAccessStream方法代码示例

本文整理汇总了C#中System.IO.MemoryStream.AsRandomAccessStream方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.AsRandomAccessStream方法的具体用法?C# MemoryStream.AsRandomAccessStream怎么用?C# MemoryStream.AsRandomAccessStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.MemoryStream的用法示例。


在下文中一共展示了MemoryStream.AsRandomAccessStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OCRAsync

        async Task<string> OCRAsync(byte[] buffer, uint width, uint height)
        {
            var bitmap = new WriteableBitmap((int)width, (int)height);

            var memoryStream = new MemoryStream(buffer);
            await bitmap.SetSourceAsync(memoryStream.AsRandomAccessStream());

            if (bitmap.PixelHeight < 40 ||
                bitmap.PixelHeight > 2600 ||
                bitmap.PixelWidth < 40 ||
                bitmap.PixelWidth > 2600)
                bitmap = await ResizeImage(bitmap, (uint)(bitmap.PixelWidth * .7), (uint)(bitmap.PixelHeight * .7));

            var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, bitmap.PixelBuffer.ToArray());

            if (ocrResult.Lines != null)
            {
                var extractedText = new StringBuilder();

                foreach (var line in ocrResult.Lines)
                {
                    foreach (var word in line.Words)
                        extractedText.Append(word.Text + " ");
                    extractedText.Append(Environment.NewLine);
                }

                return extractedText.ToString();
            }

            return null;
        }
开发者ID:SamirHafez,项目名称:Nutrition,代码行数:31,代码来源:WPOCRService.cs

示例2: GetUserThumbnailPhotoAsync

        /// <summary>
        /// Get the user's photo.
        /// </summary>
        /// <param name="user">The target user.</param>
        /// <returns></returns>
        public async Task<BitmapImage> GetUserThumbnailPhotoAsync(IUser user)
        {
            BitmapImage bitmap = null;
            try
            {
                // The using statement ensures that Dispose is called even if an 
                // exception occurs while you are calling methods on the object.
                using (var dssr = await user.ThumbnailPhoto.DownloadAsync())
                using (var stream = dssr.Stream)
                using (var memStream = new MemoryStream())
                {
                    await stream.CopyToAsync(memStream);
                    memStream.Seek(0, SeekOrigin.Begin);
                    bitmap = new BitmapImage();
                    await bitmap.SetSourceAsync(memStream.AsRandomAccessStream());
                }

            }
            catch(ODataException)
            {
                // Something went wrong retrieving the thumbnail photo, so set the bitmap to a default image
                bitmap = new BitmapImage(new Uri("ms-appx:///assets/UserDefaultSignedIn.png", UriKind.RelativeOrAbsolute));
            }

            return bitmap;
        }
开发者ID:delacruzjayveejoshua920,项目名称:ICNG-App-for-Windows-8.1-and-Windows-Phone-8.1-,代码行数:31,代码来源:UserOperations.cs

示例3: Convert

        public object Convert(object value, Type typeName, object parameter, string language)
#endif
        {
            if (value is byte[])
            {
                var array = (byte[])value;
                if (array.Length > 0)
                {
                    try
                    {
                        var stream = new MemoryStream();
                        stream.Write(array, 0, array.Length);

                        var img = new BitmapImage();
#if WINRT
                        img.SetSource(stream.AsRandomAccessStream());
#elif WPF
                        img.StreamSource = stream;
#else
                        img.SetSource(stream);
#endif
                        return img;
                    }
                    catch { }
                }
            }
            return null; 
        }
开发者ID:RareNCool,项目名称:MyToolkit,代码行数:28,代码来源:ByteArrayToImageConverter.cs

示例4: GetThumbnailImage

        public async Task<IRandomAccessStream> GetThumbnailImage(IRandomAccessStream stream, int width, int height, bool smartCropping = true)
        {
            var response = await _client.GetThumbnailAsync(stream.AsStreamForRead(), width, height, smartCropping);

            var responseStream = new MemoryStream(response);
            return responseStream.AsRandomAccessStream();
        }
开发者ID:evgri243,项目名称:pubic-demos,代码行数:7,代码来源:VisionApi.cs

示例5: GetPhotoAsync

    public async Task<BitmapImage> GetPhotoAsync(string photoUrl, string token) {

      using (var client = new HttpClient()) {
        try {
          var request = new HttpRequestMessage(HttpMethod.Get, new Uri(photoUrl));
          BitmapImage bitmap = null;

          request.Headers.Add("Authorization", "Bearer " + token);

          var response = await client.SendAsync(request);

          var stream = await response.Content.ReadAsStreamAsync();
          if (response.IsSuccessStatusCode) {

            using (var memStream = new MemoryStream()) {
              await stream.CopyToAsync(memStream);
              memStream.Seek(0, SeekOrigin.Begin);
              bitmap = new BitmapImage();
              await bitmap.SetSourceAsync(memStream.AsRandomAccessStream());
            }
            return bitmap;
          } else {
            Debug.WriteLine("Unable to find an image at this endpoint.");
            bitmap = new BitmapImage(new Uri("ms-appx:///assets/UserDefault.png", UriKind.RelativeOrAbsolute));
            return bitmap;
          }

        } catch (Exception e) {
          Debug.WriteLine("Could not get the thumbnail photo: " + e.Message);
          return null;
        }
      }

    }
开发者ID:rarera,项目名称:TrainingContent,代码行数:34,代码来源:UserOperations.cs

示例6: ProcessImageAsync

        private async Task ProcessImageAsync()
        {
            var bitmapImage = new BitmapImage();

            try
            {
                using (var client = new HttpClient())
                {
                    using (var stream = await client.GetStreamAsync(new Uri(BlobUrl)))
                    {
                        var memoryStream = new MemoryStream();
                        await stream.CopyToAsync(memoryStream);
                        memoryStream.Position = 0;
                        bitmapImage.SetSource(memoryStream.AsRandomAccessStream());
                    }
                }
            }
            catch (Exception ex)
            { }
            //InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
            //var httpClient = new HttpClient();
            //var webReq = (HttpWebRequest)WebRequest.Create(BlobUrl);
            //var httpClient.re

            //using (WebResponse response = await webReq.GetResponseAsync())
            //{
            //    using (Stream responseStream = response.GetResponseStream())
            //    {
            //        try
            //        {
            //            responseStream.CopyTo(stream);
            //        }
            //        catch (Exception ex)
            //        { }
            //    }
            //}

            //var image = new BitmapImage();
            //image.SetSource(stream);
            //Image = image;

            Image = bitmapImage;

            var scale = 350.0 / Image.PixelWidth;
            FaceBoxWidth = FaceRectangle.Width * scale;
            FaceBoxHeight = FaceRectangle.Height * scale;
            FaceBoxMargin = new Thickness(FaceRectangle.Left * scale, FaceRectangle.Top * scale, 0, 0);

            OnPropertyChanged("Image");
            OnPropertyChanged("FaceBoxWidth");
            OnPropertyChanged("FaceBoxHeight");
            OnPropertyChanged("FaceBoxMargin");
        }
开发者ID:CatalystCode,项目名称:bb9-oxford,代码行数:53,代码来源:SimilarFaceResult.cs

示例7: readQRCode

 private async void readQRCode()
 {
     StorageFile file = await KnownFolders.PicturesLibrary.GetFileAsync("photo.jpg");
     var sourceStream = (await file.OpenReadAsync()).GetInputStreamAt(0);
     byte[] imageBytes = await readFile(file);
     MemoryStream stream = new MemoryStream(imageBytes);
     IRandomAccessStream randomStream = stream.AsRandomAccessStream();
     BitmapImage bitmapImage = new BitmapImage();
     bitmapImage.SetSource(randomStream);
     var wb = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
     Result result = reader.Decode(wb);
     resultUrl.Text = result.Text;
 }
开发者ID:IPGPTP,项目名称:neon_windows_prototypes,代码行数:13,代码来源:MainPage.xaml.cs

示例8: GetThumbnail

        private async void GetThumbnail()
        {
            DeviceThumbnail thumb = await _information.GetGlyphThumbnailAsync();
            Stream s = thumb.AsStreamForRead();
            MemoryStream ms = new MemoryStream();
            await s.CopyToAsync(ms);
            ms.Seek(0, SeekOrigin.Begin);

            WriteableBitmap wb = new WriteableBitmap(1, 1);
            await wb.SetSourceAsync(ms.AsRandomAccessStream());
            _thumbnail = wb;
            OnPropertyChanged("Thumbnail");
        }
开发者ID:inthehand,项目名称:Charming,代码行数:13,代码来源:DeviceViewModel.cs

示例9: GetFromUrl

 public async Task<BitmapImage> GetFromUrl(string url)
 {
     using (var client = new HttpClient())
     {
         var imageData = await client.GetByteArrayAsync(url);
         using (var ms = new MemoryStream(imageData))
         {
             var image = new BitmapImage();
             await image.SetSourceAsync(ms.AsRandomAccessStream());
             return image;
         }
     }
 }
开发者ID:Viseo,项目名称:500px-gallery,代码行数:13,代码来源:ImageLoader.cs

示例10: MainPage

        public MainPage()
        {
            this.InitializeComponent();

            var imageLoader = new ImageLoader();

            // start image loading task, don't wait
            var task = ThreadPool.RunAsync(async (source) =>
            {
                await LoadImages(imageLoader);
            });

            // schedule image databaase refresh every 20 seconds
            TimeSpan imageLoadPeriod = TimeSpan.FromSeconds(20);
            ThreadPoolTimer imageLoadTimes = ThreadPoolTimer.CreatePeriodicTimer(
                async (source) =>
                {
                    await LoadImages(imageLoader);
                }, imageLoadPeriod);
        

            TimeSpan displayImagesPeriod = TimeSpan.FromSeconds(5);
            // display new images every five seconds
            ThreadPoolTimer imageDisplayTimer = ThreadPoolTimer.CreatePeriodicTimer(
                async (source) =>
                {
                    // get next image (byte aray) from database
                    var imageBytes = imageLoader.GetNextImage();

                    if (imageBytes != null)
                    {
                        // we have to update UI in UI thread only
                        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            async () =>
                            {
                                // create bitmap from byte array
                                BitmapImage bitmap = new BitmapImage();
                                MemoryStream ms = new MemoryStream(imageBytes);
                                await bitmap.SetSourceAsync(ms.AsRandomAccessStream());

                                // display image
                                splashImage.Source = bitmap;
                            }
                        );
                    }
                }, displayImagesPeriod);
                
        }
开发者ID:starnovsky,项目名称:IoTDisplayWithLocalStorage,代码行数:48,代码来源:MainPage.xaml.cs

示例11: MultithreadedSaveToStream

        public void MultithreadedSaveToStream()
        {
            // This is the stream version of the above test

            var task = Task.Run(async () =>
            {
                var device = new CanvasDevice();
                var rt = new CanvasRenderTarget(device, 16, 16, 96);

                var stream = new MemoryStream();
                await rt.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Bmp);

                rt.Dispose();
            });

            task.Wait();
        }
开发者ID:RainbowGardens,项目名称:Win2D,代码行数:17,代码来源:CanvasBitmapTests.cs

示例12: ScaleImageStreamAsync

        /// <summary>
        /// Scales the image in the given memory stream.
        /// </summary>
        /// <param name="originalStream">The original image stream to scale.</param>
        /// <param name="originalResolutionWidth">The original width.</param>
        /// <param name="originalResolutionHeight">The original height.</param>
        /// <param name="scaledStream">Stream where the scaled image is stored.</param>
        /// <param name="scaleWidth">The target width.</param>
        /// <param name="scaleHeight">The target height.</param>
        /// <returns></returns>
        public static async Task ScaleImageStreamAsync(MemoryStream originalStream,
                                                       int originalResolutionWidth,
                                                       int originalResolutionHeight,
                                                       MemoryStream scaledStream,
                                                       int scaleWidth,
                                                       int scaleHeight)
        {
            System.Diagnostics.Debug.WriteLine(DebugTag + "ScaleImageStreamAsync() ->");

            // Create a bitmap containing the full resolution image
            var bitmap = new WriteableBitmap(originalResolutionWidth, originalResolutionHeight);
            originalStream.Seek(0, SeekOrigin.Begin);
            await bitmap.SetSourceAsync(originalStream.AsRandomAccessStream());

            /* Construct a JPEG encoder with the newly created
             * InMemoryRandomAccessStream as target
             */
            IRandomAccessStream previewResolutionStream = new InMemoryRandomAccessStream();
            previewResolutionStream.Size = 0;
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(
                BitmapEncoder.JpegEncoderId, previewResolutionStream);

            // Copy the full resolution image data into a byte array
            Stream pixelStream = bitmap.PixelBuffer.AsStream();
            var pixelArray = new byte[pixelStream.Length];
            await pixelStream.ReadAsync(pixelArray, 0, pixelArray.Length);

            // Set the scaling properties
            encoder.BitmapTransform.ScaledWidth = (uint)scaleWidth;
            encoder.BitmapTransform.ScaledHeight = (uint)scaleHeight;
            encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
            encoder.IsThumbnailGenerated = true;

            // Set the image data and the image format setttings to the encoder
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                (uint)originalResolutionWidth, (uint)originalResolutionHeight,
                96.0, 96.0, pixelArray);

            await encoder.FlushAsync();
            previewResolutionStream.Seek(0);
            await previewResolutionStream.AsStream().CopyToAsync(scaledStream);

            System.Diagnostics.Debug.WriteLine(DebugTag + "<- ScaleImageStreamAsync()");
        }
开发者ID:TheHypnotoad,项目名称:filter-effects,代码行数:54,代码来源:AppUtils.cs

示例13: SetImage

        /// <summary>
        /// This method set ImageSource to image.
        /// </summary>
        /// <param name="imageBytes"></param>
        private async void SetImage(byte[] imageBytes)
        {
            BitmapImage im;

            // If imageBytes has data in it, then use it.
            if (imageBytes != null)
            {
                im = new BitmapImage();
                using (MemoryStream ms = new MemoryStream(imageBytes))
                {
                    await im.SetSourceAsync(ms.AsRandomAccessStream());
                }
            }
            // Otherwise use default picture.
            else
                im = new BitmapImage(new Uri("ms-appx:///Assets/icon-contact.png"));

            image.Source = im;
        }
开发者ID:kenakamu,项目名称:Activity-Tracker-UWP,代码行数:23,代码来源:EntityImageControl.xaml.cs

示例14: MultithreadedSaveToStream

        public void MultithreadedSaveToStream()
        {
            // This is the stream version of the above test

            using (new DisableDebugLayer()) // 6184116 causes the debug layer to fail when CanvasBitmap::SaveAsync is called
            {
                var task = Task.Run(async () =>
                {
                    var device = new CanvasDevice();
                    var rt = new CanvasRenderTarget(device, 16, 16, 96);

                    var stream = new MemoryStream();
                    await rt.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Bmp);

                    rt.Dispose();
                });

                task.Wait();
            }
        }
开发者ID:ben-sheeran,项目名称:Win2D,代码行数:20,代码来源:CanvasBitmapTests.cs

示例15: BitmapTransform

 private async void BitmapTransform(string filePath)
 {
     var client = new HttpClient();
     var stream = await client.GetStreamAsync(filePath);
     var memStream = new MemoryStream();
     await stream.CopyToAsync(memStream);
     memStream.Position = 0;
     var decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());
     var ras = new InMemoryRandomAccessStream();
     var enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
     var frame = await decoder.GetFrameAsync(0);
     enc.BitmapTransform.ScaledHeight = frame.PixelHeight;
     enc.BitmapTransform.ScaledWidth = frame.PixelWidth;
     var bounds = new BitmapBounds
     {
         Height = 80,
         Width = frame.PixelWidth,
         X = 0,
         Y = 0
     };
     enc.BitmapTransform.Bounds = bounds;
     try
     {
         await enc.FlushAsync();
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.ToString());
     }
     var bImg = BitmapFactory.New((int) frame.PixelWidth, (int) frame.PixelHeight);
     using (bImg.GetBitmapContext())
     {
         bImg = await BitmapFactory.New((int) frame.PixelWidth, (int) frame.PixelHeight).FromStream(ras);
         bImg.ForEach(
             (x, y, color) => color.ToString().Equals("#FFCECECE")
                 ? Color.FromArgb(80, 0, 0, 0)
                 : Colors.WhiteSmoke);
     }
     BorderBrushStreamPlayer.ImageSource = bImg;
 }
开发者ID:RBoelter,项目名称:hearthis-UWP-,代码行数:40,代码来源:TrackDetails.xaml.cs


注:本文中的System.IO.MemoryStream.AsRandomAccessStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。