示例#1
0
        async Task <int> AddImages()
        {
            int    amount;
            string search = SearchBox.Text;

            if (ImageNumBox.SelectedIndex != -1)
            {
                amount = imageNum[ImageNumBox.SelectedIndex];
            }
            else
            {
                amount = 5;
            }

            PixabaySharp.Models.ImageResult result = null;
            images = new ObservableCollection <PixaImage>();
            if (search == "")
            {
                while (result == null) //the library sometimes returns null for some reason
                {
                    result = await pixabayClient.QueryImagesAsync(new ImageQueryBuilder()
                    {
                        Page      = pixaPage,
                        PerPage   = amount,
                        MinWidth  = int.Parse(res[0]), //forgive me
                        MinHeight = int.Parse(res[1])
                    });
                }

                int i = 0;
                foreach (PixabaySharp.Models.ImageItem img in result.Images)
                {
                    images.Add(new PixaImage(i++, img.ImageURL, img.WebformatURL, img.FullHDImageURL, img.PageURL));
                    ImageGrid.ItemsSource = images;
                }
            }
            else
            {
                while (result == null) //the library sometimes returns null for some reason
                {
                    result = await pixabayClient.QueryImagesAsync(new ImageQueryBuilder()
                    {
                        Query     = search,
                        Page      = pixaPage,
                        PerPage   = amount,
                        MinWidth  = int.Parse(res[0]),
                        MinHeight = int.Parse(res[1])
                    });
                }

                int i = 0;
                foreach (PixabaySharp.Models.ImageItem img in result.Images)
                {
                    images.Add(new PixaImage(i++, img.ImageURL, img.WebformatURL, img.FullHDImageURL, img.PageURL));
                    ImageGrid.ItemsSource = images;
                }
            }
            return(0);
        }
示例#2
0
        private ImageResult GetMapperImage(PixabaySharp.Models.ImageResult images)
        {
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <PixabaySharp.Models.ImageResult, ImageResult>()
                .ForMember("TotalCount", src => src.MapFrom(p => p.Total));
                cfg.CreateMap <PixabaySharp.Models.ImageItem, Image>()
                .ForMember(src => src.Comments, p => p.Ignore())
                .ForMember("Title", src => src.MapFrom(p => p.Tags));
            }).CreateMapper();

            return(mapper.Map <PixabaySharp.Models.ImageResult, ImageResult>(images));
        }
示例#3
0
        private async Task GetNewImages()
        {
            roamingSettings.Values["wpTaskSearch"] = wpSearchBox.Text;
            string search = (string)roamingSettings.Values["wpTaskSearch"];

            string[] res;
            if (roamingSettings.Values["res"] != null)
            {
                res = (roamingSettings.Values["res"] as string).Split('x');
            }
            else
            {
                res = new string[] { "1920", "1080" }
            };

            List <string> urls = new List <string>();

            PixabaySharp.Models.ImageResult result = null;
            if (search == "")
            {
                while (result == null) //the library sometimes returns null for some reason
                {
                    result = await pixabayClient.QueryImagesAsync(new ImageQueryBuilder()
                    {
                        Page = 1, PerPage = 200, MinWidth = int.Parse(res[0]), MinHeight = int.Parse(res[1])
                    });
                }
            }
            else
            {
                while (result == null) //the library sometimes returns null for some reason
                {
                    result = await pixabayClient.QueryImagesAsync(new ImageQueryBuilder()
                    {
                        Query = search, Page = 1, PerPage = 200, MinWidth = int.Parse(res[0]), MinHeight = int.Parse(res[1])
                    });
                }
            }

            foreach (PixabaySharp.Models.ImageItem img in result.Images)
            {
                urls.Add(img.ImageURL);
            }

            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("wpTaskUrlList.txt", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteTextAsync(file, String.Join(";", urls));
        }
示例#4
0
        public static async Task ChangeWallpaper()
        {
            PixabaySharpClient       pixabayClient   = new PixabaySharpClient("3153915-c1b347f3736d73ef2cd6a0e79");
            ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

            string search = (string)roamingSettings.Values["wpTaskSearch"];

            string[] res;
            if (roamingSettings.Values["res"] != null)
            {
                res = (roamingSettings.Values["res"] as string).Split('x');
            }
            else
            {
                res = new string[] { "1920", "1080" }
            };

            string url;

            PixabaySharp.Models.ImageResult result = null;
            if (search == "")
            {
                while (result == null) //the library sometimes returns null for some reason
                {
                    result = await pixabayClient.QueryImagesAsync(new ImageQueryBuilder()
                    {
                        Page      = 1,
                        PerPage   = 200,
                        MinWidth  = int.Parse(res[0]),
                        MinHeight = int.Parse(res[1])
                    });
                }

                url = result.Images[new Random().Next(199)].ImageURL;
            }
            else
            {
                while (result == null) //the library sometimes returns null for some reason
                {
                    result = await pixabayClient.QueryImagesAsync(new ImageQueryBuilder()
                    {
                        Query     = search,
                        Page      = 1,
                        PerPage   = 200,
                        MinWidth  = int.Parse(res[0]),
                        MinHeight = int.Parse(res[1])
                    });
                }

                url = result.Images[new Random().Next(199)].ImageURL;
            }

            byte[] data;
            //string filename = DateTime.Now.ToString("d");
            HttpClient          httpClient = new HttpClient();
            HttpResponseMessage response   = await httpClient.GetAsync(new Uri(url, UriKind.Absolute));

            string mediaType = response.Content.Headers.ContentType.MediaType.Split('/')[1];

            data = await response.Content.ReadAsByteArrayAsync();

            string filename = "wallpaper." + mediaType;

            //ApplicationData.Current.LocalFolder
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBytesAsync(file, data);

            await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(file);

            roamingSettings.Values["wpTaskUrl"] = url;
        }
    }