Example #1
0
        private void ProcessImages(int resizeImageWidth, int resizeImageHeight, int thumbWidth, int thumbHeight, int hashThumbWidth, int hashThumbHeight, List <SKBitmap> skImages, ref List <Image> images)
        {
            for (int i = 0; i < skImages.Count; i++)
            {
                SKBitmap img;
                if (skImages[i].Width > resizeImageWidth || skImages[i].Height > resizeImageHeight)
                {
                    img = _imageService.ResizeImage(skImages[i], resizeImageWidth, resizeImageHeight);
                }
                else
                {
                    img = _imageService.ResizeImage(skImages[i], skImages[i].Width, skImages[i].Height); // To prevent GDI+ Exception
                }

                using (var image = img)
                {
                    // to prevent hash colisions
                    string hashThumbnail = _imageService.GetThumbnail(image, hashThumbWidth, hashThumbHeight);

                    // Check for duplicate
                    Image original = repository.GetImageByThumb(hashThumbnail);

                    // Return original, dont save duplicate
                    if (original != null)
                    {
                        images.Add(original);
                    }
                    else
                    {
                        var   newId    = Guid.NewGuid();
                        Image newImage = new Image
                        {
                            Id    = newId,
                            Thumb = hashThumbnail,
                            Path  = newId.ToString() + ".jpg",
                        };

                        // Saving FULL to storage
                        using (MemoryStream fullMs = _imageService.ConvertToStream(image))
                        {
                            _storageService.SaveToStorage(newImage.Path, fullMs);
                        }

                        images.Add(newImage);
                        repository.AddImage(newImage);

                        // Converting thumb
                        var thumbImage = _imageService.ResizeImage(image, thumbWidth, thumbHeight);
                        using (MemoryStream thumbMs = _imageService.ConvertToStream(thumbImage))
                        {
                            _storageService.SaveToStorage(newImage.Id.ToString() + "s.jpg", thumbMs);
                        }
                    }
                }

                img.Dispose();
                skImages[i].Dispose();
            }
        }