Ejemplo n.º 1
0
        private async Task SaveImageAsync(WriteableBitmap bitmap)
        {
            var folder = KnownFolders.PicturesLibrary;
            var file = await folder.CreateFileAsync("placa.jpeg", CreationCollisionOption.GenerateUniqueName);

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                await bitmap.ToStreamAsJpeg(stream);
            }
        }
Ejemplo n.º 2
0
        private static async Task<Color> GetPixels(WriteableBitmap bitmap, Color[] pixels, Int32 width, Int32 height)
        {
            IRandomAccessStream bitmapStream = new InMemoryRandomAccessStream();
            await bitmap.ToStreamAsJpeg(bitmapStream);
            var bitmapDecoder = await BitmapDecoder.CreateAsync(bitmapStream);
            var pixelProvider = await bitmapDecoder.GetPixelDataAsync();
            Byte[] byteArray = pixelProvider.DetachPixelData();
            Int32 r = 0, g = 0, b = 0;
            int sum = pixels.Length;
            for (var i = 0; i < height; i++)
            {
                for (var j = 0; j < width; j++)
                {

                    r += byteArray[(i * width + j) * 4 + 2];
                    g += byteArray[(i * width + j) * 4 + 1];
                    b += byteArray[(i * width + j) * 4 + 0];
                }
            }
            return Color.FromArgb((byte)(255), (byte)(r / sum), (byte)(g / sum), (byte)(b / sum));
        }
Ejemplo n.º 3
0
        public static async Task GetPixels(WriteableBitmap bitmap, Color[] pixels, Int32 width, Int32 height)
        {
            IRandomAccessStream bitmapStream = new InMemoryRandomAccessStream();
            await bitmap.ToStreamAsJpeg(bitmapStream);

            var bitmapDecoder = await BitmapDecoder.CreateAsync(bitmapStream);
            var pixelProvider = await bitmapDecoder.GetPixelDataAsync();
            Byte[] byteArray = pixelProvider.DetachPixelData();

            for (var i = 0; i < height; i++)
            {
                for (var j = 0; j < width; j++)
                {
                    Int32 r = byteArray[(i * height + j) * 4 + 2];
                    Int32 g = byteArray[(i * height + j) * 4 + 1];
                    Int32 b = byteArray[(i * height + j) * 4 + 0];

                    pixels[(i * height + j)] = Color.FromArgb(0xFF, (byte)r, (byte)g, (byte)b);
                }
            }
        }
Ejemplo n.º 4
0
        private async void LoadWallpaperArt()
        {
            if (_loaded ||
                !App.Locator.AppSettingsHelper.Read("WallpaperArt", true, SettingsStrategy.Roaming)) return;

            var wait = App.Locator.AppSettingsHelper.Read<int>("WallpaperDayWait");
            var created = App.Locator.AppSettingsHelper.ReadJsonAs<DateTime>("WallpaperCreated");

            // Set the image brush
            var imageBrush = new ImageBrush { Opacity = .25, Stretch = Stretch.UniformToFill, AlignmentY = AlignmentY.Top};
            LayoutGrid.Background = imageBrush;

            // Once a week remake the wallpaper
            if ((DateTime.Now - created).TotalDays > wait)
            {
                var albums =
                    App.Locator.CollectionService.Albums.ToList()
                        .Where(p => p.HasArtwork)
                        .ToList();

                var albumCount = albums.Count;

                if (albumCount < 10) return;


                var h = Window.Current.Bounds.Height;
                var rows = (int) Math.Ceiling(h/(ActualWidth/5));
                const int collumns = 5;

                var albumSize = (int) Window.Current.Bounds.Width/collumns;

                var numImages = rows*5;
                var imagesNeeded = numImages - albumCount;

                var shuffle = await Task.FromResult(albums
                    .Shuffle()
                    .Take(numImages > albumCount ? albumCount : numImages)
                    .ToList());

                if (imagesNeeded > 0)
                {
                    var repeatList = new List<Album>();

                    while (imagesNeeded > 0)
                    {
                        var takeAmmount = imagesNeeded > albumCount ? albumCount : imagesNeeded;

                        await Task.Run(() => repeatList.AddRange(shuffle.Shuffle().Take(takeAmmount)));

                        imagesNeeded -= shuffle.Count;
                    }

                    shuffle.AddRange(repeatList);
                }

                // Initialize an empty WriteableBitmap.
                var destination = new WriteableBitmap((int) Window.Current.Bounds.Width,
                    (int) Window.Current.Bounds.Height);
                var col = 0; // Current Column Position
                var row = 0; // Current Row Position
                destination.Clear(Colors.Black); // Set the background color of the image to black

                // will be copied
                foreach (var artworkPath in shuffle.Select(album => string.Format(AppConstant.ArtworkPath, album.Id)))
                {
                    var file = await WinRtStorageHelper.GetFileAsync(artworkPath);

                    // Read the image file into a RandomAccessStream
                    using (var fileStream = await file.OpenReadAsync())
                    {
                        try
                        {
                            // Now that you have the raw bytes, create a Image Decoder
                            var decoder = await BitmapDecoder.CreateAsync(fileStream);

                            // Get the first frame from the decoder because we are picking an image
                            var frame = await decoder.GetFrameAsync(0);

                            // Convert the frame into pixels
                            var pixelProvider = await frame.GetPixelDataAsync();

                            // Convert pixels into byte array
                            var srcPixels = pixelProvider.DetachPixelData();
                            var wid = (int) frame.PixelWidth;
                            var hgt = (int) frame.PixelHeight;
                            // Create an in memory WriteableBitmap of the same size
                            var bitmap = new WriteableBitmap(wid, hgt); // Temporary bitmap into which the source

                            using (var pixelStream = bitmap.PixelBuffer.AsStream())
                            {
                                pixelStream.Seek(0, SeekOrigin.Begin);
                                // Push the pixels from the original file into the in-memory bitmap
                                await pixelStream.WriteAsync(srcPixels, 0, srcPixels.Length);
                                bitmap.Invalidate();

                                // Resize the in-memory bitmap and Blit (paste) it at the correct tile
                                // position (row, col)
                                destination.Blit(new Rect(col*albumSize, row*albumSize, albumSize, albumSize),
                                    bitmap.Resize(albumSize, albumSize, WriteableBitmapExtensions.Interpolation.Bilinear),
                                    new Rect(0, 0, albumSize, albumSize));
                                col++;
                                if (col < collumns) continue;

                                row++;
                                col = 0;
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                }

                var wallpaper =
                    await WinRtStorageHelper.CreateFileAsync("wallpaper.jpg", ApplicationData.Current.LocalFolder);
                using (var rndWrite = await wallpaper.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await destination.ToStreamAsJpeg(rndWrite);
                }

                App.Locator.AppSettingsHelper.WriteAsJson("WallpaperCreated", DateTime.Now);
                // If there are 30 or less albums then recreate in one day, else wait a week
                App.Locator.AppSettingsHelper.Write("WallpaperDayWait", albums.Count < 30 ? 1 : 7);

                imageBrush.ImageSource = null;
                imageBrush.ImageSource = new BitmapImage(new Uri("ms-appdata:/local/wallpaper.jpg"));
            }
            else if (created != DateTime.MinValue)
            {
                // Not the first time, so there must already be one created
                imageBrush.ImageSource = new BitmapImage(new Uri("ms-appdata:/local/wallpaper.jpg"));
            }

            _loaded = true;
        }