Beispiel #1
1
        /// <summary>
        /// Save the primitive image and thumbnail image and return the Photo model with local url
        /// </summary>
        /// <param name="imageBuffer"></param>
        /// <returns></returns>
        public static async Task<Photo> SaveImage(IBuffer imageBuffer)
        {
            try
            {
                Photo photo = new Photo
                {
                    CreatedTime = DateTime.UtcNow
                };

                AutoResizeConfiguration tbRC = null;
                AutoResizeConfiguration pvRC = null;
                Rect thumbnailRect;
                Size pvSize;
                
                using (var source = new BufferImageSource(imageBuffer))
                {
                    var info = await source.GetInfoAsync();
                    //var thumbnailSize = ImageHelper.CalculateSize(info.ImageSize);
                    
                    thumbnailRect = CalculateRect(info.ImageSize);
                    pvSize = CalculateSize(info.ImageSize);
                    tbRC = new AutoResizeConfiguration(ImageHelper.thumbnailMaxBytes, thumbnailMaxSize, new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    pvRC = new AutoResizeConfiguration(ImageHelper.pvMaxBytes, pvSize, new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                }

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //Get LocalPath
                    var localPath = AppResources.LocalImagePath;

                    if (!store.DirectoryExists(localPath))
                    {
                        store.CreateDirectory(localPath);
                    }

                    //Save the primitive bitmap file
                    using (var file = store.CreateFile(photo.LocalUri = localPath + @"\" + photo.Tag + @"_Raw"))
                    {
                        using (var localImage = imageBuffer.AsStream())
                        {
                            localImage.CopyTo(file);
                            file.Flush();
                            localImage.Close();
                        }
                    }

                    //Save Preview Image
                    if (pvRC != null)
                    {
                        //Resize the Image to priview image

                        var pvBuffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(imageBuffer, pvRC);

                        //Save Preview Image
                        using (var file = store.CreateFile(localPath + @"\" + photo.Tag + @"_Preview.jpg"))
                        {
                            photo.PreviewDetailUri = file.Name;
                            ;
                            using (var localImage = pvBuffer.AsStream())
                            {
                                localImage.CopyTo(file);
                                file.Flush();
                                localImage.Close();
                            }
                        }
                    }

                    //Save thumbnail Image
                    if(tbRC != null)
                    {
                        //Crop to the square
                        var rb = await Reframe(imageBuffer, thumbnailRect);

                        //Resize the Image to thumbnail
                        var tb = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(rb, tbRC);

                        //Save thumbnail
                        using (var file = store.CreateFile(photo.LocalThumbnailUri = localPath + @"\" + photo.Tag + @"_Thumbnail.jpg"))
                        {
                            photo.ThumbnailDetailUri = file.Name;

                            using (var localImage = tb.AsStream())
                            {
                                localImage.CopyTo(file);
                                file.Flush();
                                localImage.Close();
                            }

                        }
                    }
                }

            return photo;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private async Task <string> SaveToCameraRoll(Stream cameraImage, string filenameBase)
        {
            string                  filename            = filenameBase + ".jpg";
            StorageFolder           storageFolder       = KnownFolders.CameraRoll;
            AutoResizeConfiguration resizeConfiguration = null;
            var buffer = StreamToBuffer(cameraImage);

            // Store low resolution image
            using (var source = new BufferImageSource(buffer))
            {
                var info = await source.GetInfoAsync();

                if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                {
                    var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);
                    resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                                                                      new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var library = new MediaLibrary())
                {
                    library.SavePictureToCameraRoll(filename, buffer.AsStream());
                }
            }

            // Store high resolution image
            if (resizeConfiguration != null)
            {
                filename             = filenameBase + HIGH_RESOLUTION_PHOTO_SUFFIX + @".jpg";
                cameraImage.Position = 0;

                using (var stream = await storageFolder.OpenStreamForWriteAsync(filename, CreationCollisionOption.GenerateUniqueName))
                {
                    await cameraImage.CopyToAsync(stream);
                }
            }
            _saved = true;

            return(storageFolder.Path + "\\" + filename);
        }
        /// <summary>
        /// Asynchronously saves a low resolution version of the photo to MediaLibrary and if the photo is too large to be saved
        /// to MediaLibrary as is also saves the original high resolution photo to application's local storage so that the
        /// high resolution version is not lost.
        /// </summary>
        public async Task SaveAsync()
        {
            if (_image != null && _image.Length > 0 && LibraryPath == null)
            {
                var buffer = StreamToBuffer(_image);

                AutoResizeConfiguration resizeConfiguration = null;

                using (var source = new BufferImageSource(buffer))
                {
                    var info = await source.GetInfoAsync();

                    if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                    {
                        var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);

                        resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                            new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    }
                }

                var filenameBase = "photoinspector";

                if (_original != null)
                {
                    if (_originalPath != null)
                    {
                        var originalPathParts = _originalPath.Split(new char[] { '_', '.' }, StringSplitOptions.RemoveEmptyEntries);

                        filenameBase += '_' + originalPathParts[1];
                    }
                    else
                    {
                        filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            if (!store.DirectoryExists(Mapping.ORIGINALS_PATH))
                            {
                                store.CreateDirectory(Mapping.ORIGINALS_PATH);
                            }

                            var originalPath = Mapping.ORIGINALS_PATH + @"\" + filenameBase + @".jpg";

                            using (var file = store.CreateFile(originalPath))
                            {
                                _original.Position = 0;
                                _original.CopyTo(file);

                                file.Flush();

                                OriginalPath = originalPath;
                            }
                        }
                    }
                }

                filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                if (resizeConfiguration != null)
                {
                    // Store high resolution original to application local storage

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.DirectoryExists(Mapping.LOCALS_PATH))
                        {
                            store.CreateDirectory(Mapping.LOCALS_PATH);
                        }

                        var localPath = Mapping.LOCALS_PATH + @"\" + filenameBase + @".jpg";

                        using (var file = store.CreateFile(localPath))
                        {
                            _image.Position = 0;
                            _image.CopyTo(file);

                            file.Flush();

                            LocalPath = localPath;
                        }
                    }

                    // Compact the buffer for saving to the library

                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var libraryImage = buffer.AsStream())
                {
                    libraryImage.Position = 0;

                    using (var library = new MediaLibrary())
                    {
                        using (var picture = library.SavePictureToCameraRoll(filenameBase, libraryImage))
                        {
                            LibraryPath = picture.GetPath();
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Asynchronously saves a low resolution version of the photo to MediaLibrary and if the photo is too large to be saved
        /// to MediaLibrary as is also saves the original high resolution photo to application's local storage so that the
        /// high resolution version is not lost.
        /// </summary>
        public async Task SaveAsync()
        {
            if (_image != null && _image.Length > 0 && LibraryPath == null)
            {
                var buffer = StreamToBuffer(_image);

                AutoResizeConfiguration resizeConfiguration = null;

                using (var source = new BufferImageSource(buffer))
                {
                    var info = await source.GetInfoAsync();

                    if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                    {
                        var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);

                        resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                                                                          new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    }
                }

                var filenameBase = "photoinspector";

                if (_original != null)
                {
                    if (_originalPath != null)
                    {
                        var originalPathParts = _originalPath.Split(new char[] { '_', '.' }, StringSplitOptions.RemoveEmptyEntries);

                        filenameBase += '_' + originalPathParts[1];
                    }
                    else
                    {
                        filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            if (!store.DirectoryExists(Mapping.ORIGINALS_PATH))
                            {
                                store.CreateDirectory(Mapping.ORIGINALS_PATH);
                            }

                            var originalPath = Mapping.ORIGINALS_PATH + @"\" + filenameBase + @".jpg";

                            using (var file = store.CreateFile(originalPath))
                            {
                                _original.Position = 0;
                                _original.CopyTo(file);

                                file.Flush();

                                OriginalPath = originalPath;
                            }
                        }
                    }
                }

                filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                if (resizeConfiguration != null)
                {
                    // Store high resolution original to application local storage

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.DirectoryExists(Mapping.LOCALS_PATH))
                        {
                            store.CreateDirectory(Mapping.LOCALS_PATH);
                        }

                        var localPath = Mapping.LOCALS_PATH + @"\" + filenameBase + @".jpg";

                        using (var file = store.CreateFile(localPath))
                        {
                            _image.Position = 0;
                            _image.CopyTo(file);

                            file.Flush();

                            LocalPath = localPath;
                        }
                    }

                    // Compact the buffer for saving to the library

                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var libraryImage = buffer.AsStream())
                {
                    libraryImage.Position = 0;

                    using (var library = new MediaLibrary())
                    {
                        using (var picture = library.SavePictureToCameraRoll(filenameBase, libraryImage))
                        {
                            LibraryPath = picture.GetPath();
                        }
                    }
                }
            }
        }
        private async Task<string> SaveToCameraRoll(Stream cameraImage, string filenameBase)
        {
            string filename = filenameBase + ".jpg";
            StorageFolder storageFolder = KnownFolders.CameraRoll;
            AutoResizeConfiguration resizeConfiguration = null;
            var buffer = StreamToBuffer(cameraImage);

            // Store low resolution image
            using (var source = new BufferImageSource(buffer))
            {
                var info = await source.GetInfoAsync();

                if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                {
                    var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);
                    resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                        new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var library = new MediaLibrary())
                {
                    library.SavePictureToCameraRoll(filename, buffer.AsStream());
                }
            }

            // Store high resolution image
            if (resizeConfiguration != null)
            {
                filename = filenameBase + HIGH_RESOLUTION_PHOTO_SUFFIX + @".jpg";
                cameraImage.Position = 0;

                using (var stream = await storageFolder.OpenStreamForWriteAsync(filename, CreationCollisionOption.GenerateUniqueName))
                {
                    await cameraImage.CopyToAsync(stream);
                }
            }
            _saved = true;

            return storageFolder.Path + "\\" + filename;
        }
Beispiel #6
0
        private async Task <IRandomAccessStream> ResizeStreamAsync(IRandomAccessStream stream, Size size, PhotoOrientation orientation)
        {
            var rotation = 0;

            switch (orientation)
            {
            case PhotoOrientation.Rotate180:
            {
                rotation = -180;
            };
                break;

            case PhotoOrientation.Rotate270:
            {
                rotation = -270;
            };
                break;

            case PhotoOrientation.Rotate90:
            {
                rotation = -90;
            };
                break;
            }

            using (var resizedStream = new InMemoryRandomAccessStream())
            {
                var buffer = new byte[stream.Size].AsBuffer();

                stream.Seek(0);

                await stream.ReadAsync(buffer, buffer.Length, InputStreamOptions.None);

                var resizeConfiguration = new AutoResizeConfiguration(
                    (uint)(size.Width * size.Height * 4 * 2), size,
                    new Size(0, 0), AutoResizeMode.Automatic, 0.7, ColorSpace.Yuv420);

                buffer = await JpegTools.AutoResizeAsync(buffer, resizeConfiguration);

                await resizedStream.WriteAsync(buffer);

                await resizedStream.FlushAsync();

                if (rotation != 0)
                {
                    resizedStream.Seek(0);

                    var filters = new List <IFilter>()
                    {
                        new RotationFilter(rotation)
                    };

                    using (var source = new RandomAccessStreamImageSource(resizedStream))
                        using (var effect = new FilterEffect(source)
                        {
                            Filters = filters
                        })
                            using (var renderer = new JpegRenderer(effect))
                            {
                                buffer = await renderer.RenderAsync();

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

                                    await rotatedResizedStream.FlushAsync();

                                    return(rotatedResizedStream.CloneStream());
                                }
                            }
                }
                else
                {
                    return(resizedStream.CloneStream());
                }
            }
        }
Beispiel #7
0
        private async Task<IRandomAccessStream> ResizeStreamAsync(IRandomAccessStream stream, Size size, PhotoOrientation orientation)
        {
            var rotation = 0;

            switch (orientation)
            {
                case PhotoOrientation.Rotate180:
                    {
                        rotation = -180;
                    };
                    break;

                case PhotoOrientation.Rotate270:
                    {
                        rotation = -270;
                    };
                    break;

                case PhotoOrientation.Rotate90:
                    {
                        rotation = -90;
                    };
                    break;
            }

            using (var resizedStream = new InMemoryRandomAccessStream())
            {
                var buffer = new byte[stream.Size].AsBuffer();

                stream.Seek(0);

                await stream.ReadAsync(buffer, buffer.Length, InputStreamOptions.None);

                var resizeConfiguration = new AutoResizeConfiguration(
                    (uint)(size.Width * size.Height * 4 * 2), size,
                    new Size(0, 0), AutoResizeMode.Automatic, 0.7, ColorSpace.Yuv420);

                buffer = await JpegTools.AutoResizeAsync(buffer, resizeConfiguration);

                await resizedStream.WriteAsync(buffer);
                await resizedStream.FlushAsync();

                if (rotation != 0)
                {
                    resizedStream.Seek(0);

                    var filters = new List<IFilter>() { new RotationFilter(rotation) };

                    using (var source = new RandomAccessStreamImageSource(resizedStream))
                    using (var effect = new FilterEffect(source) { Filters = filters })
                    using (var renderer = new JpegRenderer(effect))
                    {
                        buffer = await renderer.RenderAsync();

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

                            return rotatedResizedStream.CloneStream();
                        }
                    }
                }
                else
                {
                    return resizedStream.CloneStream();
                }
            }
        }