예제 #1
0
        private void CreateMapData()
        {
            for (int row = 0; row < Rows; row++)
            {
                for (int column = 0; column < Columns; )
                {
                    int maxBlockSize = _sizeElements.Length;
                    int blockSize = GetBlockSize(maxBlockSize);

                    while (!CanFitBlockInAt(row, column, blockSize))
                    {
                        maxBlockSize -= 1;

                        if (maxBlockSize == 0)
                        {
                            maxBlockSize = -1;
                            break;
                        }

                        blockSize = GetBlockSize(maxBlockSize);
                    }

                    if (maxBlockSize == -1)
                    {
                        column += 1;
                        continue;
                    }

                    ImageMapEntry entry = new ImageMapEntry();
                    entry.Size = blockSize;
                    entry.StartRow = row;
                    entry.StartColumn = column;

                    for (int r = row; r < row + blockSize; r++)
                    {
                        for (int c = column; c < column + blockSize; c++)
                        {
                            _map[new KeyValuePair<int, int>(r, c)] = entry;
                        }
                    }

                    column += blockSize;
                }
            }
        }
예제 #2
0
        private Image GetImage(string[] allImages, ImageMapEntry block)
        {
            int index = _random.Next(allImages.Length - 1);

            string imagePath = allImages[index];
            BitmapImage bitmap = MemoryCache.Default.Get(imagePath) as BitmapImage;

            if (bitmap == null)
            {
                bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(imagePath, UriKind.Absolute);
                bitmap.DecodePixelHeight = 3*SquareSize;
                bitmap.DecodePixelWidth = 3*SquareSize;
                //bitmap.DecodePixelHeight = block.Size * SquareSize;
                //bitmap.DecodePixelWidth = block.Size * SquareSize;
                bitmap.CreateOptions = BitmapCreateOptions.DelayCreation;
                bitmap.CacheOption = BitmapCacheOption.OnDemand;
                bitmap.EndInit();

                CacheItemPolicy policy = new CacheItemPolicy();
                policy.SlidingExpiration = TimeSpan.FromMinutes(2);
                MemoryCache.Default.Add(imagePath, bitmap, policy);
            }

            Image image = new Image();
            image.Source = bitmap;
            image.Height = block.Size * SquareSize;
            image.Width = block.Size * SquareSize;
            image.Opacity = 0.0;
            image.RenderTransform = new ScaleTransform(0.3, 0.3);
            image.RenderTransformOrigin = new Point(0.5, 0.5);

            image.Loaded += delegate
                {
                    var animationDuration = new Duration(TimeSpan.FromMilliseconds(_random.Next(100, 1500)));

                    DoubleAnimation opacityAnimation = new DoubleAnimation();
                    opacityAnimation.To = 1.0;
                    opacityAnimation.Duration = animationDuration;

                    DoubleAnimation scaleXAnimation = new DoubleAnimation();
                    scaleXAnimation.To = 1.0;
                    scaleXAnimation.Duration = animationDuration;

                    DoubleAnimation scaleYAnimation = new DoubleAnimation();
                    scaleYAnimation.To = 1.0;
                    scaleYAnimation.Duration = animationDuration;

                    Timeline.SetDesiredFrameRate(opacityAnimation, 10);
                    Timeline.SetDesiredFrameRate(scaleXAnimation, 10);
                    Timeline.SetDesiredFrameRate(scaleYAnimation, 10);

                    Storyboard.SetTarget(opacityAnimation, image);
                    Storyboard.SetTarget(scaleXAnimation, image);
                    Storyboard.SetTarget(scaleYAnimation, image);
                    Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                    Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("RenderTransform.ScaleX"));
                    Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("RenderTransform.ScaleY"));

                    Storyboard s = new Storyboard();
                    s.BeginTime = TimeSpan.FromMilliseconds(_random.Next(100, 1500));
                    s.Children.Add(opacityAnimation);
                    s.Children.Add(scaleXAnimation);
                    s.Children.Add(scaleYAnimation);
                    s.Begin();
                };

            return image;
        }
        private IEnumerable<ImageMapEntry> CreateMapData(string[] imageFiles)
        {
            var map = new Dictionary<KeyValuePair<int, int>, ImageMapEntry>();

            Func<int, int, int, bool> canFitBlockInAt = (row, column, blockSize) =>
                                                             {
                                                                 for (int r = row; r < row + blockSize; r++)
                                                                 {
                                                                     for (int c = column; c < column + blockSize; c++)
                                                                     {
                                                                         if (map.ContainsKey(new KeyValuePair<int, int>(r, c)))
                                                                             return false;
                                                                     }
                                                                 }

                                                                 return true;
                                                             };

            for (int row = 0; row < Rows; row++)
            {
                for (int column = 0; column < Columns; )
                {
                    int maxBlockSize = _sizeElements.Length;
                    int blockSize = GetBlockSize(maxBlockSize);

                    while (!canFitBlockInAt(row, column, blockSize))
                    {
                        maxBlockSize -= 1;

                        if (maxBlockSize == 0)
                        {
                            maxBlockSize = -1;
                            break;
                        }

                        blockSize = GetBlockSize(maxBlockSize);
                    }

                    if (maxBlockSize == -1)
                    {
                        column += 1;
                        continue;
                    }

                    int index = _random.Next(imageFiles.Length - 1);
                    string imagePath = imageFiles[index];
                    BitmapImage bitmap = MemoryCache.Default.Get(imagePath) as BitmapImage;
                    if (bitmap == null)
                    {
                        try
                        {
                            bitmap = new BitmapImage();
                            bitmap.BeginInit();
                            bitmap.UriSource = new Uri(imagePath, UriKind.Absolute);
                            bitmap.DecodePixelWidth = blockSize * SquareSize;
                            bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                            bitmap.EndInit();
                            bitmap.Freeze();

                            CacheItemPolicy policy = new CacheItemPolicy();
                            policy.SlidingExpiration = TimeSpan.FromSeconds(5);
                            MemoryCache.Default.Add(imagePath, bitmap, policy);
                        }
                        catch (Exception)
                        {
                            // Swallow
                        }
                    }

                    ImageMapEntry entry = new ImageMapEntry();
                    entry.Size = blockSize;
                    entry.StartRow = row;
                    entry.StartColumn = column;
                    entry.Bitmap = bitmap;

                    for (int r = row; r < row + blockSize; r++)
                    {
                        for (int c = column; c < column + blockSize; c++)
                        {
                            map[new KeyValuePair<int, int>(r, c)] = entry;
                        }
                    }

                    column += blockSize;
                }
            }

            return map.Values.Distinct();
        }
        private void AddImage(ImageMapEntry entry)
        {
            Rectangle rectangle = new Rectangle();
            rectangle.Height = entry.Size * SquareSize;
            rectangle.Width = entry.Size * SquareSize;
            rectangle.Opacity = 0.0;
            rectangle.RenderTransform = new ScaleTransform(0.7, 0.7);
            rectangle.RenderTransformOrigin = new Point(0.5, 0.5);
            var animationDuration = new Duration(TimeSpan.FromMilliseconds(700));

            DoubleAnimation opacityAnimation = new DoubleAnimation();
            opacityAnimation.To = 1.0;
            opacityAnimation.Duration = animationDuration;

            DoubleAnimation scaleXAnimation = new DoubleAnimation();
            scaleXAnimation.To = 1.0;
            scaleXAnimation.Duration = animationDuration;
            scaleXAnimation.EasingFunction = new CubicEase
            {
                EasingMode = EasingMode.EaseIn
            };

            DoubleAnimation scaleYAnimation = new DoubleAnimation();
            scaleYAnimation.To = 1.0;
            scaleYAnimation.Duration = animationDuration;
            scaleYAnimation.EasingFunction = new CubicEase
            {
                EasingMode = EasingMode.EaseIn
            };

            Timeline.SetDesiredFrameRate(opacityAnimation, 10);
            Timeline.SetDesiredFrameRate(scaleXAnimation, 10);
            Timeline.SetDesiredFrameRate(scaleYAnimation, 10);

            Storyboard.SetTarget(opacityAnimation, rectangle);
            Storyboard.SetTarget(scaleXAnimation, rectangle);
            Storyboard.SetTarget(scaleYAnimation, rectangle);
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("RenderTransform.ScaleX"));
            Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("RenderTransform.ScaleY"));

            Storyboard s = new Storyboard();
            s.BeginTime = TimeSpan.FromMilliseconds(_random.Next(0, 500));
            s.Children.Add(opacityAnimation);
            s.Children.Add(scaleXAnimation);
            s.Children.Add(scaleYAnimation);
            s.Begin();

            if (entry.Bitmap != null)
            {
                rectangle.Fill = new ImageBrush(entry.Bitmap)
                {
                    Stretch = Stretch.UniformToFill
                };
            }

            rectangle.Stroke = Brushes.Black;
            rectangle.StrokeThickness = 1.5;
            Canvas.SetLeft(rectangle, entry.StartColumn * SquareSize);
            Canvas.SetTop(rectangle, entry.StartRow * SquareSize);
            _canvas.Children.Add(rectangle);
        }
예제 #5
0
        private void AddImage(ImageMapEntry entry)
        {
            Rectangle rectangle = new Rectangle();

            rectangle.Height                = entry.Size * SquareSize;
            rectangle.Width                 = entry.Size * SquareSize;
            rectangle.Opacity               = 0.0;
            rectangle.RenderTransform       = new ScaleTransform(0.7, 0.7);
            rectangle.RenderTransformOrigin = new Point(0.5, 0.5);
            var animationDuration = new Duration(TimeSpan.FromMilliseconds(700));

            DoubleAnimation opacityAnimation = new DoubleAnimation();

            opacityAnimation.To       = 1.0;
            opacityAnimation.Duration = animationDuration;

            DoubleAnimation scaleXAnimation = new DoubleAnimation();

            scaleXAnimation.To             = 1.0;
            scaleXAnimation.Duration       = animationDuration;
            scaleXAnimation.EasingFunction = new CubicEase
            {
                EasingMode = EasingMode.EaseIn
            };

            DoubleAnimation scaleYAnimation = new DoubleAnimation();

            scaleYAnimation.To             = 1.0;
            scaleYAnimation.Duration       = animationDuration;
            scaleYAnimation.EasingFunction = new CubicEase
            {
                EasingMode = EasingMode.EaseIn
            };

            Timeline.SetDesiredFrameRate(opacityAnimation, 10);
            Timeline.SetDesiredFrameRate(scaleXAnimation, 10);
            Timeline.SetDesiredFrameRate(scaleYAnimation, 10);

            Storyboard.SetTarget(opacityAnimation, rectangle);
            Storyboard.SetTarget(scaleXAnimation, rectangle);
            Storyboard.SetTarget(scaleYAnimation, rectangle);
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("RenderTransform.ScaleX"));
            Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("RenderTransform.ScaleY"));

            Storyboard s = new Storyboard();

            s.BeginTime = TimeSpan.FromMilliseconds(_random.Next(0, 500));
            s.Children.Add(opacityAnimation);
            s.Children.Add(scaleXAnimation);
            s.Children.Add(scaleYAnimation);
            s.Begin();

            if (entry.Bitmap != null)
            {
                rectangle.Fill = new ImageBrush(entry.Bitmap)
                {
                    Stretch = Stretch.UniformToFill
                };
            }

            rectangle.Stroke          = Brushes.Black;
            rectangle.StrokeThickness = 1.5;
            Canvas.SetLeft(rectangle, entry.StartColumn * SquareSize);
            Canvas.SetTop(rectangle, entry.StartRow * SquareSize);
            _canvas.Children.Add(rectangle);
        }
예제 #6
0
        private IEnumerable <ImageMapEntry> CreateMapData(string[] imageFiles)
        {
            var map = new Dictionary <KeyValuePair <int, int>, ImageMapEntry>();

            Func <int, int, int, bool> canFitBlockInAt = (row, column, blockSize) =>
            {
                for (int r = row; r < row + blockSize; r++)
                {
                    for (int c = column; c < column + blockSize; c++)
                    {
                        if (map.ContainsKey(new KeyValuePair <int, int>(r, c)))
                        {
                            return(false);
                        }
                    }
                }

                return(true);
            };

            for (int row = 0; row < Rows; row++)
            {
                for (int column = 0; column < Columns;)
                {
                    int maxBlockSize = _sizeElements.Length;
                    int blockSize    = GetBlockSize(maxBlockSize);

                    while (!canFitBlockInAt(row, column, blockSize))
                    {
                        maxBlockSize -= 1;

                        if (maxBlockSize == 0)
                        {
                            maxBlockSize = -1;
                            break;
                        }

                        blockSize = GetBlockSize(maxBlockSize);
                    }

                    if (maxBlockSize == -1)
                    {
                        column += 1;
                        continue;
                    }

                    int         index     = _random.Next(imageFiles.Length - 1);
                    string      imagePath = imageFiles[index];
                    BitmapImage bitmap    = MemoryCache.Default.Get(imagePath) as BitmapImage;
                    if (bitmap == null)
                    {
                        try
                        {
                            bitmap = new BitmapImage();
                            bitmap.BeginInit();
                            bitmap.UriSource        = new Uri(imagePath, UriKind.Absolute);
                            bitmap.DecodePixelWidth = blockSize * SquareSize;
                            bitmap.CreateOptions    = BitmapCreateOptions.IgnoreColorProfile;
                            bitmap.EndInit();
                            bitmap.Freeze();

                            CacheItemPolicy policy = new CacheItemPolicy();
                            policy.SlidingExpiration = TimeSpan.FromSeconds(5);
                            MemoryCache.Default.Add(imagePath, bitmap, policy);
                        }
                        catch (Exception)
                        {
                            // Swallow
                        }
                    }

                    ImageMapEntry entry = new ImageMapEntry();
                    entry.Size        = blockSize;
                    entry.StartRow    = row;
                    entry.StartColumn = column;
                    entry.Bitmap      = bitmap;

                    for (int r = row; r < row + blockSize; r++)
                    {
                        for (int c = column; c < column + blockSize; c++)
                        {
                            map[new KeyValuePair <int, int>(r, c)] = entry;
                        }
                    }

                    column += blockSize;
                }
            }

            return(map.Values.Distinct());
        }