Exemplo n.º 1
0
        /// <summary>
        /// Handles the DoWork event of the queued background worker.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Manina.Windows.Forms.QueuedWorkerDoWorkEventArgs"/> instance
        /// containing the event data.</param>
        void Bw_DoWork(object sender, QueuedWorkerDoWorkEventArgs e)
        {
            CacheRequest request = e.Argument as CacheRequest;

            // Should we continue processing this item?
            // The callback checks the following and returns false if
            //   the item is already cached -OR-
            //   the item is in the edit cache -OR-
            //   the item is outside the visible area (only if the CacheMode is OnDemand).
            if (!OnCanContinueProcessing(request))
            {
                e.Cancel = true;
                return;
            }

            Image  thumb        = null;
            string diskCacheKey = request.Adaptor.GetUniqueIdentifier(request.VirtualItemKey, request.Size, request.UseEmbeddedThumbnails, request.AutoRotate, request.UseWIC);

            // Check the disk cache
            using (Stream stream = DiskCache.Read(diskCacheKey))
            {
                if (stream.Length > 0)
                {
                    thumb = new Bitmap(stream);
                }
            }

            //Trace.WriteLine("BwGetThumbnail:"+request.VirtualItemKey);
            // Extract the thumbnail from the source image.
            if (thumb == null)
            {
                thumb = request.Adaptor.GetThumbnail(request.VirtualItemKey, request.Size, request.UseEmbeddedThumbnails, request.AutoRotate, request.UseWIC);
                // Save to disk cache
                if (thumb != null)
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        thumb.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        DiskCache.Write(diskCacheKey, stream);
                    }
                }
            }

            // Return the thumbnail
            CacheItem result = null;

            if (thumb == null && !RetryOnError)
            {
                result = new CacheItem(request.Guid, request.Size, null, CacheState.Error, request.UseEmbeddedThumbnails, request.AutoRotate, request.UseWIC);
            }
            else
            {
                result = new CacheItem(request.Guid, request.Size, thumb, CacheState.Cached, request.UseEmbeddedThumbnails, request.AutoRotate, request.UseWIC);
            }

            e.Result = result;
        }