private void LoaderThreadThumbnails() { do { _loaderThreadThumbnailEvent.WaitOne(); LoadImageRequest loadTask = null; do { lock (_loadThumbnailStack) { loadTask = _loadThumbnailStack.Count > 0 ? _loadThumbnailStack.Pop() : null; } if (loadTask != null && !loadTask.IsCanceled) { var bitmapSource = GetBitmapSource(loadTask); EndLoading(loadTask.Image, bitmapSource, loadTask, false); lock (_loadNormalStack) { _loadNormalStack.Push(loadTask); } _loaderThreadNormalSizeEvent.Set(); } }while(loadTask != null); }while(true); }
private void BeginLoading(Image image, LoadImageRequest loadTask) { lock (_imagesLastRunningTask) { if (_imagesLastRunningTask.ContainsKey(image)) { _imagesLastRunningTask[image].IsCanceled = true; _imagesLastRunningTask[image] = loadTask; } else { _imagesLastRunningTask.Add(image, loadTask); } } image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetIsLoading(image, true); if (image.RenderTransform == Transform.Identity) { if (Loader.GetDisplayWaitingAnimationDuringLoading(image)) { image.Source = _loadingImage; image.RenderTransformOrigin = new Point(0.5, 0.5); image.RenderTransform = _loadingAnimationTransform; } } })); }
public void LoadImage(string source, Image image) { var loadTask = new LoadImageRequest { Image = image, Source = source }; BeginLoading(image, loadTask); lock (_loadThumbnailStack) { _loadThumbnailStack.Push(loadTask); } _loaderThreadThumbnailEvent.Set(); }
private void EndLoading(Image image, ImageSource imageSource, LoadImageRequest loadTask, bool markAsFinished) { lock (_imagesLastRunningTask) { if (_imagesLastRunningTask.ContainsKey(image)) { if (_imagesLastRunningTask[image].Source != loadTask.Source) { return; } if (markAsFinished) { _imagesLastRunningTask.Remove(image); } } else { Debug.WriteLine("EndLoading() - unexpected condition: there is no running task for this image!"); } image.Dispatcher.BeginInvoke(new ThreadStart(delegate { if (image.RenderTransform == _loadingAnimationTransform) { image.RenderTransform = Transform.Identity; } if (Loader.GetErrorDetected(image) && Loader.GetDisplayErrorThumbnailOnError(image)) { imageSource = _errorThumbnail; } image.Source = imageSource; if (markAsFinished) { Loader.SetIsLoading(image, false); } })); } }
public void LoadImage(Image image) { LoadImageRequest loadTask = new LoadImageRequest() { Image = image, SourceUri = image.SourceUri }; BeginLoading(image); _loadQueue.Push(loadTask); _loaderThreadEvent.Set(); }
private ImageSource GetBitmapSource(LoadImageRequest loadTask) { Image image = loadTask.Image; string sourceUri = loadTask.SourceUri; if (string.IsNullOrWhiteSpace(sourceUri)) { SetError(image); return null; } double width = double.NaN; double height = double.NaN; image.Dispatcher.Invoke(new ThreadStart(delegate { width = image.Width; height = image.Height; })); string cacheKey = ComputeHash(sourceUri); byte[] byteImage = _cache.Get(cacheKey) as byte[]; if (byteImage == null) { byteImage = LoaderFactory.CreateLoader(sourceUri).Load(); if (byteImage != null) { try { _cache.Set( cacheKey, byteImage, new CacheItemPolicy() { SlidingExpiration = new TimeSpan(0, 0, ItemCacheExpirationMinutes, 0, 0) } ); } catch { } } } if (byteImage == null) { SetError(image); return null; } try { using (MemoryStream memStream = new MemoryStream(byteImage)) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; if (!double.IsNaN(width) && !double.IsNaN(height)) { bitmapImage.DecodePixelWidth = (int)width; bitmapImage.DecodePixelHeight = (int)height; } bitmapImage.StreamSource = memStream; bitmapImage.EndInit(); bitmapImage.Freeze(); return bitmapImage; } } catch { SetError(image); return null; } }
public void LoadImage(string source, Image image) { LoadImageRequest loadTask = new LoadImageRequest() { Image = image, Source = source }; // Begin Loading BeginLoading(image, loadTask); lock (_loadThumbnailStack) { _loadThumbnailStack.Push(loadTask); } _loaderThreadThumbnailEvent.Set(); }
private ImageSource GetBitmapSource(LoadImageRequest loadTask, DisplayOptions loadType) { Image image = loadTask.Image; string source = loadTask.Source; ImageSource imageSource = null; if (!string.IsNullOrWhiteSpace(source)) { Stream imageStream = null; SourceType sourceType = SourceType.LocalDisk; image.Dispatcher.Invoke(new ThreadStart(delegate { sourceType = Loader.GetSourceType(image); })); try { if (loadTask.Stream == null) { ILoader loader = LoaderFactory.CreateLoader(sourceType); imageStream = loader.Load(source); loadTask.Stream = imageStream; } else { imageStream = new MemoryStream(); loadTask.Stream.Position = 0; loadTask.Stream.CopyTo(imageStream); imageStream.Position = 0; } } catch (Exception) { } if (imageStream != null) { try { if (loadType == DisplayOptions.Preview) { BitmapFrame bitmapFrame = BitmapFrame.Create(imageStream); imageSource = bitmapFrame.Thumbnail; if (imageSource == null) // Preview it is not embedded into the file { // we'll make a thumbnail image then ... (too bad as the pre-created one is FAST!) TransformedBitmap thumbnail = new TransformedBitmap(); thumbnail.BeginInit(); thumbnail.Source = bitmapFrame as BitmapSource; // we'll make a reasonable sized thumnbail with a height of 240 int pixelH = bitmapFrame.PixelHeight; int pixelW = bitmapFrame.PixelWidth; int decodeH = 240; int decodeW = (bitmapFrame.PixelWidth * decodeH) / pixelH; double scaleX = decodeW / (double)pixelW; double scaleY = decodeH / (double)pixelH; TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new ScaleTransform(scaleX, scaleY)); thumbnail.Transform = transformGroup; thumbnail.EndInit(); // this will disconnect the stream from the image completely ... WriteableBitmap writable = new WriteableBitmap(thumbnail); writable.Freeze(); imageSource = writable; } } else if (loadType == DisplayOptions.FullResolution) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = imageStream; bitmapImage.EndInit(); imageSource = bitmapImage; } } catch (Exception) { } } if (imageSource == null) { image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, true); })); } else { imageSource.Freeze(); image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, false); })); } } else { image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, false); })); } return imageSource; }
private void EndLoading(Image image, ImageSource imageSource, LoadImageRequest loadTask, bool markAsFinished) { lock (_imagesLastRunningTask) { if (_imagesLastRunningTask.ContainsKey(image)) { if (_imagesLastRunningTask[image].Source != loadTask.Source) return; // if the last launched task for this image is not this one, abort it! if (markAsFinished) _imagesLastRunningTask.Remove(image); } else { /* ERROR! */ System.Diagnostics.Debug.WriteLine("EndLoading() - unexpected condition: there is no running task for this image!"); } image.Dispatcher.BeginInvoke(new ThreadStart(delegate { if (image.RenderTransform == _loadingAnimationTransform) { image.RenderTransform = MatrixTransform.Identity; } if (Loader.GetErrorDetected(image) && Loader.GetDisplayErrorThumbnailOnError(image)) { imageSource = _errorThumbnail; } image.Source = imageSource; if (markAsFinished) { // Set IsLoading Pty Loader.SetIsLoading(image, false); } })); } }
private void BeginLoading(Image image, LoadImageRequest loadTask) { lock (_imagesLastRunningTask) { if (_imagesLastRunningTask.ContainsKey(image)) { // Cancel previous loading... _imagesLastRunningTask[image].IsCanceled = true; _imagesLastRunningTask[image] = loadTask; } else { _imagesLastRunningTask.Add(image, loadTask); } } image.Dispatcher.BeginInvoke(new ThreadStart(delegate { // Set IsLoading Pty Loader.SetIsLoading(image, true); if (image.RenderTransform == MatrixTransform.Identity) // Don't apply loading animation if image already has transform... { // Manage Waiting Image Parameter if (Loader.GetDisplayWaitingAnimationDuringLoading(image)) { image.Source = _loadingImage; image.RenderTransformOrigin = new Point(0.5, 0.5); image.RenderTransform = _loadingAnimationTransform; } } })); }
private ImageSource GetBitmapSource(LoadImageRequest loadTask, DisplayOptions loadType) { DisplayOptions lt = loadType; Image image = loadTask.Image; string source = loadTask.Source; string nSource = source; string cacheFile = Path.Combine(AppStateSettings.CacheFolder, "Media", nSource.GetSHA1Hash().ToString() + "s" + loadTask.CachHeight + ".png"); // REVIEW TODO: Used Path instead of String concat. if (File.Exists(cacheFile)) { nSource = cacheFile; lt = DisplayOptions.FullResolution; //return new BitmapImage(new Uri("file://" + cacheFile)); } ImageSource imageSource = null; if (!string.IsNullOrWhiteSpace(nSource)) { Stream imageStream = null; SourceType sourceType = SourceType.LocalDisk; image.Dispatcher.Invoke(new ThreadStart(delegate { sourceType = Loader.GetSourceType(image); })); try { if (loadTask.Stream == null) { ILoader loader = LoaderFactory.CreateLoader(sourceType, nSource); imageStream = loader.Load(nSource); loadTask.Stream = imageStream; } else { imageStream = new MemoryStream(); loadTask.Stream.Position = 0; loadTask.Stream.CopyTo(imageStream); imageStream.Position = 0; } } catch (Exception e) { Console.WriteLine(e.Message); } if (imageStream != null) { try { if (lt == DisplayOptions.Preview) { BitmapFrame bitmapFrame = BitmapFrame.Create(imageStream); imageSource = bitmapFrame.Thumbnail; if (imageSource == null) // Preview it is not embedded into the file { // we'll make a thumbnail image then ... (too bad as the pre-created one is FAST!) TransformedBitmap thumbnail = new TransformedBitmap(); thumbnail.BeginInit(); thumbnail.Source = bitmapFrame as BitmapSource; // we'll make a reasonable sized thumnbail with a height of 240 int pixelH = bitmapFrame.PixelHeight; int pixelW = bitmapFrame.PixelWidth; int decodeH = (int)loadTask.CachHeight; int decodeW = (bitmapFrame.PixelWidth * decodeH) / pixelH; double scaleX = decodeW / (double)pixelW; double scaleY = decodeH / (double)pixelH; TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new ScaleTransform(scaleX, scaleY)); thumbnail.Transform = transformGroup; thumbnail.EndInit(); // this will disconnect the stream from the image completely ... WriteableBitmap writable = new WriteableBitmap(thumbnail); BitmapFrame frame = BitmapFrame.Create(writable); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(frame); using (var stream = File.Create(cacheFile)) { encoder.Save(stream); } writable.Freeze(); imageSource = writable; } } else if (lt == DisplayOptions.FullResolution) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = imageStream; bitmapImage.EndInit(); imageSource = bitmapImage; } } catch (Exception e) { Console.WriteLine(e.Message); } } if (imageSource == null) { image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, true); })); } else { imageSource.Freeze(); image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, false); })); } } else { image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, false); })); } return imageSource; }
public void LoadImage(object source, Image image, double cacheHeight) { if (source == null) return; var imageSource = source as ImageSource; if (imageSource != null) { image.Source = imageSource; } else { var loadTask = new LoadImageRequest { Image = image, Source = source.ToString(), CachHeight = cacheHeight }; // Begin Loading BeginLoading(image, loadTask); lock (_loadThumbnailStack) { _loadThumbnailStack.Push(loadTask); } _loaderThreadThumbnailEvent.Set(); } }
private ImageSource GetBitmapSource(LoadImageRequest loadTask, DisplayOptions loadType) { Image image = loadTask.Image; object source = loadTask.Source; ImageSource imageSource = null; if (source != null) { Stream imageStream = null; SourceType sourceType = SourceType.LocalDisk; image.Dispatcher.Invoke(new ThreadStart(delegate { sourceType = Loader.GetSourceType(image); })); try { if (loadType != DisplayOptions.VideoPreview) { if (loadTask.Stream == null) { ILoader loader = LoaderFactory.CreateLoader(sourceType); imageStream = loader.Load(source); loadTask.Stream = imageStream; } else { imageStream = new MemoryStream(); loadTask.Stream.Position = 0; loadTask.Stream.CopyTo(imageStream); imageStream.Position = 0; } } else if (sourceType == SourceType.ZipFile) { throw new InvalidOperationException("Can't load video preview from zip file."); } } catch (Exception) { } if (imageStream != null || loadType == DisplayOptions.VideoPreview) { try { if (loadType == DisplayOptions.Preview) { BitmapFrame bitmapFrame = BitmapFrame.Create(imageStream); int rotation = GetRotation(bitmapFrame.Metadata as BitmapMetadata); if (bitmapFrame.Thumbnail != null) { BitmapSource src = bitmapFrame.Thumbnail; // crop black bars if necessary double ratio = (double)bitmapFrame.PixelWidth / bitmapFrame.PixelHeight; double thumbRatio = (double)src.PixelWidth / src.PixelHeight; if (Math.Abs(ratio - thumbRatio) >= 0.01) { if (ratio > thumbRatio) // crop top/bottom { int newHeight = (int)(src.PixelWidth / ratio); int top = (src.PixelHeight - newHeight) / 2; src = new CroppedBitmap(src, new Int32Rect(0, top, src.PixelWidth, newHeight)); } else // crop left/right { int newWidth = (int)(src.PixelHeight * ratio); int left = (src.PixelWidth - newWidth) / 2; src = new CroppedBitmap(src, new Int32Rect(left, 0, newWidth, src.PixelHeight)); } } TransformedBitmap thumbnail = new TransformedBitmap(); thumbnail.BeginInit(); thumbnail.Source = src; TransformGroup transformGroup = new TransformGroup(); // rotate according to metadata transformGroup.Children.Add(new RotateTransform(rotation)); thumbnail.Transform = transformGroup; thumbnail.EndInit(); imageSource = thumbnail; } else // Preview it is not embedded into the file { // we'll make a thumbnail image then ... (too bad as the pre-created one is FAST!) TransformedBitmap thumbnail = new TransformedBitmap(); thumbnail.BeginInit(); thumbnail.Source = bitmapFrame as BitmapSource; // we'll make a reasonable sized thumnbail with a height of 240 int pixelH = bitmapFrame.PixelHeight; int pixelW = bitmapFrame.PixelWidth; int decodeH = 240; int decodeW = (pixelW * decodeH) / pixelH; double scaleX = decodeW / (double)pixelW; double scaleY = decodeH / (double)pixelH; TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new ScaleTransform(scaleX, scaleY)); transformGroup.Children.Add(new RotateTransform(rotation)); thumbnail.Transform = transformGroup; thumbnail.EndInit(); // this will disconnect the stream from the image completely ... WriteableBitmap writable = new WriteableBitmap(thumbnail); writable.Freeze(); imageSource = writable; } } else if (loadType == DisplayOptions.Combined || loadType == DisplayOptions.FullResolution) { BitmapFrame bitmapFrame = BitmapFrame.Create(imageStream); int rotation = GetRotation(bitmapFrame.Metadata as BitmapMetadata); TransformedBitmap bitmapImage = new TransformedBitmap(); bitmapImage.BeginInit(); bitmapImage.Source = bitmapFrame as BitmapSource; TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform(rotation)); bitmapImage.Transform = transformGroup; bitmapImage.EndInit(); WriteableBitmap writable = new WriteableBitmap(bitmapImage); writable.Freeze(); imageSource = writable; } else if (loadType == DisplayOptions.VideoPreview) { var player = new MediaPlayer { Volume = 0, ScrubbingEnabled = true }; Uri uri; if (loadTask.Source is string) uri = new Uri(loadTask.Source as string); else if (loadTask.Source is Uri) uri = loadTask.Source as Uri; else throw new InvalidOperationException(); player.Open(uri); player.Pause(); player.Position = new TimeSpan(0, 0, 20); // go to 20 seconds (if the video is shorter, a black image will be captured) Thread.Sleep(1000); int i = 0; while (i < 10 && (player.NaturalDuration.TimeSpan.TotalSeconds* player.BufferingProgress) <= 20) { Thread.Sleep(100); i++; } var pixelW = player.NaturalVideoWidth; var pixelH = player.NaturalVideoHeight; int decodeH = 240; int decodeW = (pixelW * decodeH) / pixelH; var rtb = new RenderTargetBitmap(decodeW, decodeH, 96, 96, PixelFormats.Pbgra32); DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) dc.DrawVideo(player, new Rect(0, 0, decodeW, decodeH)); rtb.Render(dv); imageSource = (ImageSource)BitmapFrame.Create(rtb).GetCurrentValueAsFrozen(); player.Close(); } } catch (Exception) { } } if (imageSource == null) { image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, true); })); } else { imageSource.Freeze(); image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, false); })); } } else { image.Dispatcher.BeginInvoke(new ThreadStart(delegate { Loader.SetErrorDetected(image, false); })); } return imageSource; }
public void LoadImage(object source, Image image) { LoadImageRequest loadTask = new LoadImageRequest() { Image = image, Source = source }; // Begin Loading BeginLoading(image, loadTask); lock (_loadThumbnailQueue) { _loadThumbnailQueue.Enqueue(loadTask); } _loaderThreadThumbnailEvent.Set(); }
private ImageSource GetBitmapSource(LoadImageRequest loadTask) { var image = loadTask.Image; var source = loadTask.Source; ImageSource imageSource = null; if (!string.IsNullOrWhiteSpace(source)) { Stream imageStream = null; var sourceType = SourceType.LocalDisk; if (source.StartsWith("http")) { sourceType = SourceType.ExternalResource; } //image.Dispatcher.Invoke(new ThreadStart(delegate { sourceType = Loader.GetSourceType(image); })); try { if (loadTask.Stream == null) { var loader = LoaderFactory.CreateLoader(sourceType); imageStream = loader.Load(source); loadTask.Stream = imageStream; } else { imageStream = new MemoryStream(); loadTask.Stream.Position = 0; loadTask.Stream.CopyTo(imageStream); imageStream.Position = 0; } } catch (Exception ex) { Debug.WriteLine(ex.Message); } if (imageStream != null) { try { var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = imageStream; bitmapImage.EndInit(); imageSource = bitmapImage; } catch (Exception ex) { Debug.WriteLine(ex.Message); } } if (imageSource == null) { image.Dispatcher.BeginInvoke(new ThreadStart(() => Loader.SetErrorDetected(image, true))); } else { imageSource.Freeze(); image.Dispatcher.BeginInvoke(new ThreadStart(() => Loader.SetErrorDetected(image, false))); } } else { image.Dispatcher.BeginInvoke(new ThreadStart(() => Loader.SetErrorDetected(image, false))); } return(imageSource); }