示例#1
0
        /// <summary>
        /// Apply set as commands
        /// </summary>
        /// <param name="wallpaper"></param>
        /// <param name="action"></param>
        public void ApplyAction(Models.Wallpaper.Entities.Wallpaper wallpaper, string action)
        {
            if (action == "AsBackground")
            {
                new Uri(wallpaper.Path).SetAsDesktopWallpaper(SetAsWallpaperHelpers.Style.Centered);
            }
            else if (action == "SaveAs")
            {
                var folderBrowserDialog = new FolderBrowserDialog
                {
                    Description = ViewModelApplication.LanguageResourceDictionary["SelectSaveDestination"].ToString(),
                    RootFolder  = Environment.SpecialFolder.Desktop
                };

                var result = folderBrowserDialog.ShowDialog();

                if (result == DialogResult.OK && !string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
                {
                    var fileName = Path.GetFileName(wallpaper.Path);

                    File.Copy(wallpaper.Path, $"{folderBrowserDialog.SelectedPath}\\{fileName}");
                }
            }
            else if (action == "Remove")
            {
                if (!wallpaper.Path.StartsWith("https://"))
                {
                    File.Delete(wallpaper.Path);
                }

                if (!wallpaper.Thumbnail.BitmapImageToPath().StartsWith("https://"))
                {
                    File.Delete(wallpaper.Thumbnail.BitmapImageToPath());
                }

                using var db = new AppDbContext();
                db.Wallpapers.Remove(wallpaper);
                db.SaveChanges();

                wallpaper.Id         = 0;
                wallpaper.IsFavorite = false;
                wallpaper.Path       = wallpaper.WallpaperUrl;
                wallpaper.Thumbnail  = wallpaper.WallpaperThumbUrl.PathToBitmapImage();
            }
        }
示例#2
0
        /// <summary>
        /// Extract color palette from image
        /// </summary>
        /// <param name="wallpaper"></param>
        public void ExtractColors(Models.Wallpaper.Entities.Wallpaper wallpaper)
        {
            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;

                // wallpaper image
                var image = new BitmapImage(new Uri(wallpaper.Path));

                // copy to byte array
                int stride    = image.PixelWidth * 4;
                byte[] buffer = new byte[stride * image.PixelHeight];
                image.CopyPixels(buffer, stride, 0);

                // create bitmap
                System.Drawing.Bitmap bitmap =
                    new System.Drawing.Bitmap(
                        image.PixelWidth,
                        image.PixelHeight,
                        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                // lock bitmap data
                System.Drawing.Imaging.BitmapData bitmapData =
                    bitmap.LockBits(
                        new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                        bitmap.PixelFormat);

                // copy byte array to bitmap data
                System.Runtime.InteropServices.Marshal.Copy(
                    buffer, 0, bitmapData.Scan0, buffer.Length);

                // unlock
                bitmap.UnlockBits(bitmapData);

                // extract colors
                var colorThief = new ColorThief();
                var palette    = colorThief.GetPalette(bitmap, 5, 7);

                if (palette.Count > 0)
                {
                    foreach (var item in palette)
                    {
                        var color = new Models.Wallpaper.Entities.Color
                        {
                            WallpaperId = wallpaper.Id,
                            ColorCode   = item.Color.ToHexString()
                        };

                        wallpaper.ColorPalette.Add(color);
                    }

                    if (wallpaper.ColorPalette.Count > 0)
                    {
                        using var db = new AppDbContext();
                        db.Colors.AddRange(wallpaper.ColorPalette);
                        db.SaveChanges();
                    }
                }
            }).Start();
        }
示例#3
0
        /// <summary>
        /// Fetch and load latest wallpapers from apis
        /// </summary>
        public void LoadLatestWallpapers()
        {
            if (IsProcessingLatestWallpapers)
            {
                return;
            }

            new Task(async() =>
            {
                IsProcessingLatestWallpapers = true;
                LatestWallpapersLIVisibility = Visibility.Visible;

                await Task.Delay(1000);

                System.Windows.Application.Current.Dispatcher.Invoke(() =>
                {
                    // set empty wallpaper list
                    LatestWallpapers = new ObservableCollection <Models.Wallpaper.Entities.Wallpaper>();

                    // load desktop wallpapers
                    if (WallpapersType == WallpaperType.Desktop)
                    {
                        // random wallpapers api url
                        var apiUrl = $"{WallhavenApiUrl}?page={LatestCurrentPage}" +
                                     $"&resolutions={SelectedLatestResolution.ResolutionX}x{SelectedLatestResolution.ResolutionY}";

                        // get data
                        var wallpapersData = apiUrl.Get();

                        // if has not valid server response data
                        if (wallpapersData == null)
                        {
                            return;
                        }

                        // parsed wallpapers data
                        var jsonObjwallpapers = JsonConvert.DeserializeObject <WallpapersJson>(wallpapersData);

                        if (jsonObjwallpapers != null)
                        {
                            using var db = new AppDbContext();

                            // loop in wallpapers data
                            foreach (var item in jsonObjwallpapers.data)
                            {
                                var wallpaper = new Models.Wallpaper.Entities.Wallpaper
                                {
                                    UId               = item.id,
                                    Path              = item.path,
                                    Thumbnail         = item.thumbs.large.PathToBitmapImage(),
                                    DimensionX        = item.dimension_x,
                                    DimensionY        = item.dimension_y,
                                    WallpaperUrl      = item.path,
                                    WallpaperThumbUrl = item.thumbs.large,
                                    ColorPalette      = item.colors?.Select(color => new Models.Wallpaper.Entities.Color
                                    {
                                        ColorCode = color
                                    }).ToObservableCollection(),
                                    History = db.History.Where(x => x.WallpaperUId == item.id).ToObservableCollection()
                                };

                                wallpaper = db.Wallpapers
                                            .Select(x => new Models.Wallpaper.Entities.Wallpaper
                                {
                                    Id                = x.Id,
                                    UId               = x.UId,
                                    Path              = x.Path,
                                    Thumbnail         = x.Thumbnail,
                                    DimensionX        = x.DimensionX,
                                    DimensionY        = x.DimensionY,
                                    IsFavorite        = x.IsFavorite,
                                    WallpaperUrl      = x.WallpaperUrl,
                                    WallpaperThumbUrl = x.WallpaperThumbUrl,
                                    ColorPalette      = db.Colors.Where(c => c.WallpaperId == x.Id)
                                                        .ToObservableCollection(),
                                    History = db.History.Where(x => x.WallpaperUId == item.id).ToObservableCollection()
                                })
                                            .FirstOrDefault(x => x.UId == wallpaper.UId) ?? wallpaper;

                                LatestWallpapers.Add(wallpaper);
                            }

                            // select first wallpaper and scroll to top
                            SelectedLatestWallpaper = LatestWallpapers.FirstOrDefault();

                            // wallpaper pagination
                            LatestPagination = new Pagination(jsonObjwallpapers.meta.total, LatestCurrentPage, LatestPageLimit, 10);
                        }
                    }
                    // load mobile wallpapers
                    else if (WallpapersType == WallpaperType.Mobile)
                    {
                        // random wallpapers api url
                        var apiUrl = $"{WallpapersCraftApiUrl}?offset={LatestCurrentPage * 30}" +
                                     $"&screen[width]={SelectedLatestResolution.ResolutionX}&screen[height]={SelectedLatestResolution.ResolutionY}" +
                                     $"&sort=date&types[]=free&types[]=public";

                        // get data
                        var wallpapersData = apiUrl.Get();

                        // if has not valid server response data
                        if (wallpapersData == null)
                        {
                            return;
                        }

                        // parsed wallpapers data
                        var jsonObjwallpapers = JsonConvert.DeserializeObject <MobileWallpapersJson>(wallpapersData);

                        if (jsonObjwallpapers != null)
                        {
                            using var db = new AppDbContext();

                            // loop in wallpapers data
                            foreach (var item in jsonObjwallpapers.items)
                            {
                                var wallpaper = new Models.Wallpaper.Entities.Wallpaper
                                {
                                    UId               = item.id.ToString(),
                                    Path              = item.variations.adapted.url,
                                    Thumbnail         = item.variations.preview_small.url.PathToBitmapImage(),
                                    DimensionX        = item.variations.adapted.resolution.width,
                                    DimensionY        = item.variations.adapted.resolution.height,
                                    WallpaperUrl      = item.variations.adapted.url,
                                    WallpaperThumbUrl = item.variations.preview_small.url,
                                    WallpaperType     = WallpaperType.Mobile,
                                    History           = db.History.Where(x => x.WallpaperUId == item.id.ToString()).ToObservableCollection()
                                };

                                wallpaper = db.Wallpapers
                                            .Select(x => new Models.Wallpaper.Entities.Wallpaper
                                {
                                    Id                = x.Id,
                                    UId               = x.UId,
                                    Path              = x.Path,
                                    Thumbnail         = x.Thumbnail,
                                    DimensionX        = x.DimensionX,
                                    DimensionY        = x.DimensionY,
                                    IsFavorite        = x.IsFavorite,
                                    WallpaperUrl      = x.WallpaperUrl,
                                    WallpaperThumbUrl = x.WallpaperThumbUrl,
                                    ColorPalette      = db.Colors.Where(c => c.WallpaperId == x.Id)
                                                        .ToObservableCollection(),
                                    History = db.History.Where(x => x.WallpaperUId == item.id.ToString()).ToObservableCollection()
                                })
                                            .FirstOrDefault(x => x.UId == wallpaper.UId) ?? wallpaper;

                                LatestWallpapers.Add(wallpaper);
                            }

                            // select first wallpaper and scroll to top
                            SelectedLatestWallpaper = LatestWallpapers.FirstOrDefault();

                            // wallpaper pagination
                            LatestPagination = new Pagination(jsonObjwallpapers.count, LatestCurrentPage, LatestPageLimit, 10);
                        }
                    }
                });

                LatestWallpapersLIVisibility = Visibility.Hidden;
                IsProcessingLatestWallpapers = false;
            }).Start();
        }