本文整理汇总了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 "";
}
}