Ejemplo n.º 1
0
        public override BitmapSource GetBitmap()
        {
            if (_imageFiles == null)
            {
                _imageFiles     = GetImagesFromDirectory();
                _imageFilesPath = _imageFiles.GetEnumerator();
                _imageFilesPath.MoveNext();
                _timer.Restart();
            }
            var current = _imageFilesPath?.Current;

            if (string.IsNullOrEmpty(current))
            {
                return(null);
            }

            var bitmap   = new BitmapImage();
            var fileInfo = new FileInfo(current);

            if (fileInfo.Exists)
            {
                bitmap.BeginInit();
                bitmap.CacheOption   = BitmapCacheOption.OnLoad;
                bitmap.CreateOptions = BitmapCreateOptions.None;
                bitmap.UriSource     = new Uri(current, UriKind.RelativeOrAbsolute);
                bitmap.EndInit();
                bitmap.Freeze();
            }
            else
            {
                OnSettingChanged(null, null);
                return(GetBitmap());
            }


            BitmapSource ret_bitmap = bitmap;

            if (Setting.ImageStretch == ImageStretch.None)
            {
                bitmap = Utils.EnsureMaxWidthHeight(bitmap, Setting.MaxWidth, Setting.MaxHeight);
                if (bitmap.Width != bitmap.PixelWidth || bitmap.Height != bitmap.PixelHeight)
                {
                    ret_bitmap = Utils.ConvertToDpi96(bitmap);
                }
            }

            if (Setting.SoftEdgeX > 0 || Setting.SoftEdgeY > 0)
            {
                ret_bitmap = Utils.SoftenEdges(ret_bitmap, Setting.SoftEdgeX, Setting.SoftEdgeY);
            }

            return(ret_bitmap);
        }
Ejemplo n.º 2
0
        protected override void OnSettingChanged(object sender, EventArgs e)
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer.Elapsed -= OnTimerElapsed;
                _timer.Dispose();
            }
            _timer          = new PausableTimer(Setting.UpdateImageInterval.TotalMilliseconds);
            _timer.Elapsed += OnTimerElapsed;

            if ((sender as Setting)?.ImageBackgroundType != ImageBackgroundType.Slideshow)
            {
                _timer.Stop();
            }
            else
            {
                _imageFiles     = GetImagesFromDirectory();
                _imageFilesPath = _imageFiles.GetEnumerator();
                ChangeImage();
                _timer.Restart();
            }
        }
Ejemplo n.º 3
0
        private void ChangeImage()
        {
            if (Setting.ImageBackgroundType != ImageBackgroundType.WebApi)
            {
                return;
            }
            if (_timer.IsPaused)
            {
                return;
            }
            Task.Run(async() =>
            {
                using (var client = new WebClient())
                {
                    try
                    {
                        var endpointResult = await client.DownloadStringTaskAsync(new Uri(Setting.WebApiEndpoint));
                        var reader         = new JsonTextReader(new StringReader(endpointResult));

                        while (reader.Read())
                        {
                            if (reader.Value != null && reader.TokenType == JsonToken.PropertyName &&
                                reader.Value.ToString().Equals(Setting.WebApiJsonKey))
                            {
                                var imageUrl = reader.ReadAsString();
                                Image        = await ImageDownloader.LoadImage(imageUrl, Setting.ImageStretch, Setting.MaxWidth, Setting.MaxHeight);
                                _timer.Restart();
                                FireImageAvailable();
                                return;
                            }
                        }
                    }
                    catch {}
                }
            });
        }