当前位置: 首页>>代码示例>>C#>>正文


C# Flickr.PhotosGetSizes方法代码示例

本文整理汇总了C#中FlickrNet.Flickr.PhotosGetSizes方法的典型用法代码示例。如果您正苦于以下问题:C# Flickr.PhotosGetSizes方法的具体用法?C# Flickr.PhotosGetSizes怎么用?C# Flickr.PhotosGetSizes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FlickrNet.Flickr的用法示例。


在下文中一共展示了Flickr.PhotosGetSizes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetWallpaper

        public static string GetWallpaper(string theme, int minW, int minH, string dlFolder, out string flickrPage, List<string> bannedWalls, Action<string> banWallpaper)
        {
            flickrPage = "";

            theme = theme.Replace("Twilight", "Sunset"); //because flickr usually returns nothing when the tag combination contains "Twilight"

            try
            {
                Flickr flickr = new Flickr("928a5bfb36cc1ea3160c6d236c2c76d4");

                //search flickr
                PhotoSearchOptions opts = new PhotoSearchOptions();

                opts.Tags = theme.Replace(" - ", ",");
                opts.TagMode = TagMode.AllTags;
                opts.SortOrder = PhotoSearchSortOrder.InterestingnessDescending;
                opts.Licenses.Add(LicenseType.AttributionCC);
                opts.Licenses.Add(LicenseType.AttributionNoDerivativesCC);
                opts.Licenses.Add(LicenseType.AttributionShareAlikeCC);
                opts.Licenses.Add(LicenseType.NoKnownCopyrightRestrictions);
                opts.SafeSearch = SafetyLevel.Safe;
                opts.ContentType = ContentTypeSearch.PhotosOnly;
                opts.MediaType = MediaType.Photos;
                opts.Extras = PhotoSearchExtras.OwnerName;

                PhotoCollection flickrResults = flickr.PhotosSearch(opts);

                if (flickrResults.Count == 0) //no suitable wallpapers found
                    return "";

                //choose random images for one that satisfies min. resolution
                SizeCollection sizes;
                Random rand = new Random((int)DateTime.Now.Ticks);
                string url = "";
                int pick = -1, i;

                while (pick == -1)
                {
                    pick = rand.Next(flickrResults.Count);

                    if (bannedWalls.Contains(flickrResults[pick].WebUrl))
                    {
                        //wallpaper banned, pick another
                        flickrResults.RemoveAt(pick);
                        pick = -1;
                    }
                    else
                    {
                        sizes = flickr.PhotosGetSizes(flickrResults[pick].PhotoId);

                        for (i = sizes.Count - 1; i >= 0; i--)
                            if (sizes[i].Width < minW || sizes[i].Height < minH)
                                break;

                        i++;
                        if (i == sizes.Count) //image not large enough
                        {
                            banWallpaper(flickrResults[pick].WebUrl); //ban it so future searches can ignore it
                            flickrResults.RemoveAt(pick);

                            pick = -1;
                        }
                        else
                            url = sizes[i].Source;
                    }

                    if (flickrResults.Count == 0) //no suitable wallpapers found
                        return "";
                }

                //get valid filename from title
                string filename = flickrResults[pick].Title;
                foreach (char c in System.IO.Path.GetInvalidFileNameChars())
                    filename = filename.Replace(c, '_');

                filename += url.Substring(url.LastIndexOf('.')); //append file extension from url

                //dl image
                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadFile(url, dlFolder + filename);
                }

                flickrPage = flickrResults[pick].WebUrl;
                return dlFolder + filename;
            }
            catch (Exception e)
            {
                return "";
            }
        }
开发者ID:Winterstark,项目名称:Wallcreeper,代码行数:91,代码来源:classOnlineSources.cs


注:本文中的FlickrNet.Flickr.PhotosGetSizes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。