예제 #1
0
        /// <summary>
        /// 指定した検索文字列でBing画像検索を行います。<para>
        /// 検索結果の画像URLから画像をダウンロードして内部コレクションに保持します。</para>
        /// </summary>
        /// <param name="searchWord">検索文字列</param>
        /// <param name="progress">進捗状態通知オブジェクト</param>
        /// <param name="page">ページ番号</param>
        /// <param name="imageCountPerPage">ページ当たりの画像数</param>
        public void DownloadSearchResult(string searchWord, IProgressNotifier progress, int page, int imageCountPerPage)
        {
            if (imageCountPerPage < 10)
            {
                throw new ArgumentException(nameof(imageCountPerPage) + " must be 10 or over.", nameof(imageCountPerPage));
            }

            logger.Info("検索中...");
            progress.Start();
            progress.Progress(0);

            var skip = (page - 1) * imageCountPerPage;

            disposable = ServiceClient.SearchImageAsObservable(searchWord, bingAccountKey, skip, imageCountPerPage, ThreadPoolScheduler.Instance)
                .SelectMany(async imageResult => new {
                    imageResult,
                    imageBytes = await DownloadThumbnailImageBytesAsync(imageResult)
                })
                .Where(a => a.imageBytes != null)
                .Select((a,index)=> new{imageResult = a.imageResult, imageBytes = a.imageBytes, index })
                .ObserveOn(UIDispatcherScheduler.Default)
                .Finally(() => progress.End())
                .Subscribe(
                    onNext: a =>
                    {
                        var thumbnail = BitmapImageHelper.CreateBitmap(a.imageBytes);
                        imagesSource.Add(new WebImage(a.imageResult, thumbnail, logger));
                        progress.Progress((double)(a.index + 1) / imageCountPerPage * 100);
                    },
                    onError: e =>
                    {
                        logger.Error("画像の検索に失敗しました。", e);
                    },
                    onCompleted: () =>
                    {
                        logger.Info("検索が完了しました。");
                        progress.Progress(100);
                    });
        }