public async static Task <WriteableBitmap> ToBitmapImageAsync(this byte[] image)
        {
            WriteableBitmap resultBitmap = new WriteableBitmap(1, 1);

            using (IRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                await ms.WriteAsync(image.AsBuffer());

                ms.Seek(0); // Just to be sure.
                resultBitmap.SetSource(ms);
            }
            return(resultBitmap);
        }
Exemplo n.º 2
0
        public async Task <BitmapImage> ByteArrayToImageAsync(byte[] pixeByte)
        {
            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                BitmapImage image = new BitmapImage();
                await stream.WriteAsync(pixeByte.AsBuffer());

                stream.Seek(0);
                await image.SetSourceAsync(stream);

                return(image);
            }
        }
Exemplo n.º 3
0
 public static async Task<Mat> FromMediaCapture(MediaCapture _mediaCapture)
 {
    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
    {
       await _mediaCapture.CapturePhotoToStreamAsync(Windows.Media.MediaProperties.ImageEncodingProperties.CreateJpeg(), stream);
       stream.Seek(0);
       byte[] data = new byte[stream.Size];
       await stream.AsStreamForRead().ReadAsync(data, 0, data.Length);
       Mat result = new Mat();
       CvInvoke.Imdecode(data, LoadImageType.Color, result);
       return result;
    }
 }
Exemplo n.º 4
0
        protected async Task <WriteableBitmap> GetImage(string url)
        {
            try
            {
                IBuffer buffer = await BaseService.SendGetRequestAsBytes(url);

                if (buffer != null)
                {
                    BitmapImage     bi = new BitmapImage();
                    WriteableBitmap wb = null; Stream stream2Write;
                    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                    {
                        stream2Write = stream.AsStreamForWrite();

                        await stream2Write.WriteAsync(buffer.ToArray(), 0, (int)buffer.Length);

                        await stream2Write.FlushAsync();

                        stream.Seek(0);

                        await bi.SetSourceAsync(stream);

                        wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);
                        stream.Seek(0);
                        await wb.SetSourceAsync(stream);

                        return(wb);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 5
0
        public static async Task <Image <TColor, TDepth> > FromMediaCapture(MediaCapture _mediaCapture)
        {
            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await _mediaCapture.CapturePhotoToStreamAsync(Windows.Media.MediaProperties.ImageEncodingProperties.CreateJpeg(), stream);

                stream.Seek(0);
                byte[] data = new byte[stream.Size];
                await stream.AsStreamForRead().ReadAsync(data, 0, data.Length);

                return(Image <TColor, TDepth> .FromRawImageData(data));
            }
        }
Exemplo n.º 6
0
        private static async Task <InMemoryRandomAccessStream> CreateEmptyImage()
        {
            Uri         appUri = new Uri("ms-appx:///Assets/EmptyTile.png");
            StorageFile file   = await StorageFile.GetFileFromApplicationUriAsync(appUri).AsTask().ConfigureAwait(false);

            IBuffer buffer = await FileIO.ReadBufferAsync(file).AsTask().ConfigureAwait(false);

            InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
            await stream.WriteAsync(buffer).AsTask().ConfigureAwait(false);

            stream.Seek(0);
            return(stream);
        }
Exemplo n.º 7
0
        public static BitmapImage ConvertToBitmapImage(this string base64)
        {
            var ims        = new InMemoryRandomAccessStream();
            var img        = new BitmapImage();
            var bytes      = Convert.FromBase64String(base64);
            var dataWriter = new DataWriter(ims);

            dataWriter.WriteBytes(bytes);
            dataWriter.StoreAsync().AsTask().Wait();
            ims.Seek(0);
            img.SetSource(ims);
            return(img);
        }
Exemplo n.º 8
0
        private async void CopyImage_Click(object sender, RoutedEventArgs e)
        {
            //クリップボードへコピーする処理
            InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
            await randomAccessStream.WriteAsync(imageBytes.AsBuffer());

            randomAccessStream.Seek(0);
            RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromStream(randomAccessStream);
            var dp = new DataPackage();

            dp.SetBitmap(streamRef);
            Clipboard.SetContent(dp);
        }
Exemplo n.º 9
0
        public async Task LoadPreviewAsync()
        {
            if (This.Preview != null)
            {
                using (var stream = new InMemoryRandomAccessStream())
                {
                    await stream.WriteAsync(This.Preview.AsBuffer());

                    stream.Seek(0);
                    await Preview.SetSourceAsync(stream);
                }
            }
        }
Exemplo n.º 10
0
 private async Task Load(byte[] buffer)
 {
     using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
     {
         using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0UL)))
         {
             writer.WriteBytes(buffer);
             await writer.StoreAsync();
         }
         stream.Seek(0UL);
         await InkCanvas.InkPresenter.StrokeContainer.LoadAsync(stream);
     }
 }
Exemplo n.º 11
0
        private async Task loadContentAsync()
        {
            if (This.Content != null)
            {
                using (var stream = new InMemoryRandomAccessStream())
                {
                    await stream.WriteAsync(This.Content.AsBuffer());

                    stream.Seek(0);
                    await Content.SetSourceAsync(stream);
                }
            }
        }
        public async Task <byte[]> GetStrokeData()
        {
            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await Inker.InkPresenter.StrokeContainer.SaveAsync(stream);

                stream.Seek(0);
                byte[] data = new byte[stream.Size];
                await stream.ReadAsync(data.AsBuffer(), (uint)stream.Size, InputStreamOptions.None);

                return(data);
            }
        }
Exemplo n.º 13
0
        public async Task <BitmapImage> ConvertBinaryToPicture(byte[] blob)
        {
            BitmapImage image = new BitmapImage();

            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(blob.AsBuffer());

                stream.Seek(0);
                await image.SetSourceAsync(stream);
            }
            return(image);
        }
        private async Task <IInputStream> GetObject <T>(T item)
        {
            IRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream();
            var stream = inMemoryStream.AsStream();

            inMemoryStream.Seek(0);
            var writer = new StreamWriter(stream);

            _jsonSerializer.Serialize(writer, item);
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            return(await Task.FromResult <IInputStream>(new InputStreamWithContentType(inMemoryStream, "application/javascript")));
        }
        public async Task Should_detect_truncated_stream()
        {
            using (var input = new InMemoryRandomAccessStream())
            {
                await CopyData(input, "IO.HashedBlockStream.bin");

                input.Size -= 8;

                input.Seek(0);
                await Assert.ThrowsAsync <InvalidDataException>(
                    () => HashedBlockFileFormat.Read(input));
            }
        }
Exemplo n.º 16
0
        private async void LoadImage()
        {
            var image = new BitmapImage();

            using (var stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(CoverBytes.AsBuffer());

                stream.Seek(0);
                await image.SetSourceAsync(stream);
            }
            Cover = image;
        }
Exemplo n.º 17
0
        public async static Task <ImageSource> GetSourceAsync(this byte[] imagem)
        {
            var retorno = new BitmapImage();

            using (var stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(imagem.AsBuffer());

                stream.Seek(0);
                retorno.SetSource(stream);
            }
            return(retorno);
        }
Exemplo n.º 18
0
        public static async Task ToMat(this MediaCapture mediaCapture, Mat result)
        {
            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await mediaCapture.CapturePhotoToStreamAsync(Windows.Media.MediaProperties.ImageEncodingProperties.CreateJpeg(), stream);

                stream.Seek(0);
                byte[] data = new byte[stream.Size];
                await stream.AsStreamForRead().ReadAsync(data, 0, data.Length);

                CvInvoke.Imdecode(data, ImreadModes.Color, result);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Converts the sketch to a bitmap image.
        /// </summary>
        /// <returns>The `UIImage` representation of the sketch.</returns>
        public BitmapImage ToImage()
        {
            // Convert the image and return
            var skPngdata  = Draw();
            var imageBytes = skPngdata.ToArray();
            var image      = new BitmapImage();
            InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();

            ms.AsStreamForWrite().Write(imageBytes, 0, imageBytes.Length);
            ms.Seek(0);
            image.SetSource(ms);
            return(image);
        }
Exemplo n.º 20
0
        public async static Task <BitmapImage> ImageFromBytes(byte[] bytes)
        {
            BitmapImage image = new BitmapImage();

            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(bytes.AsBuffer());

                stream.Seek(0);
                await image.SetSourceAsync(stream);
            }
            return(image);
        }
        public async Task GetRef()
        {
            softwareBitmap = await holst.SaveToBitmap(container);

            var randomAccessStream = new InMemoryRandomAccessStream();
            var encoder            = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, randomAccessStream);

            encoder.SetSoftwareBitmap(softwareBitmap);
            await encoder.FlushAsync();

            randomAccessStream.Seek(0);
            reference = RandomAccessStreamReference.CreateFromStream(randomAccessStream);
        }
Exemplo n.º 22
0
        private async void newAvatar(byte[] newAvatar)
        {
            using (var stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(new MyBuffer(newAvatar).Buffer);

                BitmapImage x = new BitmapImage();
                stream.Seek(0);
                await x.SetSourceAsync(stream);

                this.Avatar = x;
            }
        }
Exemplo n.º 23
0
        public async static Task <BitmapImage> GetImageAsync(IBuffer buffer)
        {
            var img = new BitmapImage();

            using (var stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(buffer);

                stream.Seek(0);
                await img.SetSourceAsync(stream);
            }
            return(img);
        }
Exemplo n.º 24
0
        public static ImageSource GetSource(this byte[] imagem)
        {
            var retorno = new BitmapImage();

            using (var stream = new InMemoryRandomAccessStream())
            {
                var otherStream = stream.AsStreamForWrite();
                otherStream.Write(imagem, 0, imagem.Length);
                stream.Seek(0);
                retorno.SetSource(stream);
            }
            return(retorno);
        }
Exemplo n.º 25
0
        public async Task RegisterFace(Uri uri, string name)
        {
            if (faceDetector == null)
            {
                faceDetector = await FaceDetector.CreateAsync();
            }

            var stream = await http.GetStreamAsync(uri);

            var memStream = new MemoryStream();
            await stream.CopyToAsync(memStream);

            memStream.Position = 0;



            var decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());

            var bitmap = await decoder.GetSoftwareBitmapAsync();

            /*
             * var faceBounds = await FindFace(bitmap);
             * if(!faceBounds.HasValue) {
             *  System.Diagnostics.Debug.WriteLine("More than or less than one face found in training image");
             *  return;
             * }
             */

            // https://forums.xamarin.com/discussion/63447/cannot-crop-or-resize-images-on-windows-universal-uwp
            var dstStream = new InMemoryRandomAccessStream();
            //var encoder = await BitmapEncoder.CreateForTranscodingAsync(dstStream, decoder);
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, dstStream);

            //encoder.BitmapTransform.Bounds = faceBounds.Value;
            encoder.SetSoftwareBitmap(bitmap);
            await encoder.FlushAsync();

            dstStream.Seek(0);
            var imgBuffer = new byte[dstStream.Size];
            await dstStream.ReadAsync(imgBuffer.AsBuffer(), (uint)dstStream.Size, InputStreamOptions.None);

            /*
             * // uncomment to dump files to disk for debug
             * var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync($"test-{name}.jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting);
             * using(var outStream = await file.OpenStreamForWriteAsync()) {
             *  await outStream.WriteAsync(imgBuffer, 0, imgBuffer.Length);
             * }
             */

            server.SaveFace(imgBuffer, name, true, null);
        }
Exemplo n.º 26
0
        public async Task <WriteableBitmap> GetWriteableBitmapAsync(string url)
        {
            try
            {
                IBuffer buffer = await getBufferHttp(url);

                if (buffer != null)
                {
                    BitmapImage     bi = new BitmapImage();
                    WriteableBitmap wb = null;
                    Stream          stream1;
                    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                    {
                        stream1 = stream.AsStreamForWrite();
                        await stream1.WriteAsync(buffer.ToArray(), 0, (int)buffer.Length);

                        await stream1.FlushAsync();

                        stream.Seek(0);
                        await bi.SetSourceAsync(stream);

                        wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);
                        stream.Seek(0);
                        await wb.SetSourceAsync(stream);

                        return(wb);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 27
0
        public static async Task <WriteableBitmap> GetWriteableBitmapAsync(byte[] buffer)
        {
            try
            {
                //byte[] buffer = await doPost1(url);
                if (buffer != null)
                {
                    BitmapImage     bi = new BitmapImage();
                    WriteableBitmap wb = null; Stream stream2Write;
                    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                    {
                        stream2Write = stream.AsStreamForWrite();

                        await stream2Write.WriteAsync(buffer, 0, buffer.Length);

                        await stream2Write.FlushAsync();

                        stream.Seek(0);

                        await bi.SetSourceAsync(stream);

                        wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);
                        stream.Seek(0);
                        await wb.SetSourceAsync(stream);

                        return(wb);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 28
0
        async void AddItem(StorageFile f)
        {
            var b = new Button()
            {
                Width = 120, Height = 85, Margin = new Thickness(5)
            };

            b.Tag    = f;
            b.Click += (s, o) =>
            {
                FileDefaultLaunch(f);
            };
            b.RightTapped += (s, e) =>
            {
                if (contextFlyout == null)
                {
                    InitContextMemu(f);
                }
                contextFlyout.ShowAt(s as UIElement, e.GetPosition(s as UIElement));
            };
            var thumbnail = await f.GetScaledImageAsThumbnailAsync(ThumbnailMode.DocumentsView);

            BitmapImage bitmapImage = new BitmapImage();
            InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
            await RandomAccessStream.CopyAsync(thumbnail, randomAccessStream);

            randomAccessStream.Seek(0);
            bitmapImage.SetSource(randomAccessStream);
            var im = new Image()
            {
                Source = bitmapImage
            };
            Grid g = new Grid();

            g.RowDefinitions.Add(new RowDefinition());
            g.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(25)
            });
            g.Children.Add(im);
            TextBlock t = new TextBlock()
            {
                Text = f.Name
            };

            Grid.SetRow(t, 1);
            g.Children.Add(t);
            b.Content = g;
            ResourcePanel.Items.Add(b);
        }
Exemplo n.º 29
0
        public static async Task CopyToClipboardBitmap(byte[] data, string filename = null)
        {
            InMemoryRandomAccessStream rstream = new InMemoryRandomAccessStream();
            await rstream.WriteAsync(data.AsBuffer());

            rstream.Seek(0);

            var package = new DataPackage();

            package.SetBitmap(RandomAccessStreamReference.CreateFromStream(rstream));
            package.RequestedOperation = DataPackageOperation.Copy;
            Clipboard.SetContent(package);
            Clipboard.Flush();
        }
Exemplo n.º 30
0
        private async void GenerateResultImage()
        {
            var img = GetDrawings(false);

            if (img != null)
            {
                IRandomAccessStream stream = new InMemoryRandomAccessStream();
                await img.SaveAsync(stream, CanvasBitmapFileFormat.Jpeg);

                BitmapImage result = new BitmapImage();
                stream.Seek(0);
                await result.SetSourceAsync(stream);
            }
        }
Exemplo n.º 31
0
 /// <summary>
 /// Converts the specified byte array to a <see cref="IRandomAccessStream" />.
 /// </summary>
 /// <param name="buffer"></param>
 /// <returns></returns>
 private static async Task<IRandomAccessStream> ConvertToRandomAccessStream(byte[] buffer)
 {
     //https://stackoverflow.com/questions/16397509/how-to-convert-byte-array-to-inmemoryrandomaccessstream-or-irandomaccessstream-i
     var randomAccessStream = new InMemoryRandomAccessStream();
     await randomAccessStream.WriteAsync(buffer.AsBuffer());
     randomAccessStream.Seek(0);
     return randomAccessStream;
 }