예제 #1
0
        private static BitmapSource?ChooseOrEncodeWindowIcon(BitmapSource?smallIcon, BitmapSource?largeIcon)
        {
            BitmapSource?bitmapSource = null;

            if (largeIcon != null)
            {
                if (smallIcon != null)
                {
                    BitmapFrame bitmapFrame;
                    var         tiffBitmapEncoder = new TiffBitmapEncoder();
                    tiffBitmapEncoder.Frames.Add(BitmapFrame.Create(smallIcon));
                    tiffBitmapEncoder.Frames.Add(BitmapFrame.Create(largeIcon));
                    using (var memoryStream = new MemoryStream())
                    {
                        tiffBitmapEncoder.Save(memoryStream);
                        bitmapFrame = BitmapFrame.Create(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                    }
                    FreezeImage(bitmapFrame);
                    bitmapSource = bitmapFrame;
                }
                else
                {
                    bitmapSource = largeIcon;
                }
            }
            else if (smallIcon != null)
            {
                bitmapSource = smallIcon;
            }
            return(bitmapSource);
        }
예제 #2
0
        public async Task InitializeAsync(int?decodePixelHeight = null)
        {
            // remember subitems are checked to have at least one file when they are created
            var firstFile = this.Subitem.Files.First();

            if (!FileTypes.IsImage(firstFile))
            {
                return;
            }

            var file = await StorageFile.GetFileFromPathAsync(firstFile);

            var image = new BitmapImage {
                DecodePixelType = DecodePixelType.Logical
            };

            if (decodePixelHeight is { } h)
            {
                image.DecodePixelHeight = h;
            }

            await image.SetSourceAsync(await file.OpenReadAsync());

            this.ThumbnailImage = image;
            this.OnPropertyChanged(nameof(this.ThumbnailImage));
        }
예제 #3
0
        private static ObjectAnimationUsingKeyFrames?GetAnimation(Image imageControl, BitmapSource source)
        {
            var animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl));

            if (animation != null)
            {
                return(animation);
            }
            if (!(GetDecoder(source, imageControl, out var gifMetadata) is GifBitmapDecoder decoder) || decoder.Frames.Count <= 1)
            {
                return(null);
            }

            var fullSize = GetFullSize(decoder, gifMetadata);
            var index    = 0;

            animation = new ObjectAnimationUsingKeyFrames();
            var          totalDuration = TimeSpan.Zero;
            BitmapSource?baseFrame     = null;

            foreach (var rawFrame in decoder.Frames)
            {
                var metadata = GetFrameMetadata(decoder, gifMetadata, index);

                var frame    = MakeFrame(fullSize, rawFrame, metadata, baseFrame);
                var keyFrame = new DiscreteObjectKeyFrame(frame, totalDuration);
                animation.KeyFrames.Add(keyFrame);

                totalDuration += metadata.Delay;

                switch (metadata.DisposalMethod)
                {
                case FrameDisposalMethod.None:
                case FrameDisposalMethod.DoNotDispose:
                    baseFrame = frame;
                    break;

                case FrameDisposalMethod.RestoreBackground:
                    baseFrame = IsFullFrame(metadata, fullSize) ? null : ClearArea(frame, metadata);
                    break;

                case FrameDisposalMethod.RestorePrevious:
                    // Reuse same base frame
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                index++;
            }
            animation.Duration = totalDuration;

            animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, gifMetadata);

            AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
            AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl));
            return(animation);
        }
        private static void AddToCache(string iconUrl, BitmapSource?iconBitmapImage)
        {
            var policy = new CacheItemPolicy
            {
                SlidingExpiration = TimeSpan.FromMinutes(10),
                RemovedCallback   = CacheEntryRemoved
            };

            BitmapImageCache.Set(iconUrl, iconBitmapImage, policy);
        }
예제 #5
0
        private static BitmapSource?BitmapSourceFromHIcon(IntPtr iconHandle)
        {
            BitmapSource?bitmapSource = null;

            if (iconHandle != IntPtr.Zero)
            {
                bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                User32.DestroyIcon(iconHandle);
                FreezeImage(bitmapSource);
            }
            return(bitmapSource);
        }
예제 #6
0
 private static bool IsLoadingDeferred(BitmapSource?source, Image imageControl)
 {
     if (!(source is BitmapImage bmp))
     {
         return(false);
     }
     if (bmp.UriSource != null && !bmp.UriSource.IsAbsoluteUri)
     {
         return(bmp.BaseUri == null && (imageControl as IUriContext)?.BaseUri == null);
     }
     return(false);
 }
예제 #7
0
        private async Task LoadSampleImageAsync(AssemblyResourceLink sourceLink)
        {
            var source = new BitmapImage();

            using (var randomAccessStream = sourceLink.OpenRead().AsRandomAccessStream())
            {
                await source.SetSourceAsync(randomAccessStream);
            }

            _bitmapSource = source;

            this.RaisePropertyChanged(nameof(this.BitmapSource));
        }
예제 #8
0
        private static BitmapSource?ExtractIconsFromExecutable(ref BitmapSource?smallIcon, ref BitmapSource?largeIcon)
        {
            var executablePath = Assembly.GetExecutingAssembly().Location;
            var phiconLarge    = new[] { IntPtr.Zero };
            var phiconSmall    = new[] { IntPtr.Zero };

            if (Shell32.ExtractIconEx(executablePath.Trim('"'), 0, phiconLarge, phiconSmall, 1) > 0)
            {
                smallIcon = BitmapSourceFromHIcon(phiconSmall[0]);
                largeIcon = BitmapSourceFromHIcon(phiconLarge[0]);
            }
            return(null);
        }
예제 #9
0
        private static bool IsLoadingDeferred(BitmapSource?source)
        {
            var bmp = source as BitmapImage;

            if (bmp == null)
            {
                return(false);
            }
            if (bmp.UriSource != null && !bmp.UriSource.IsAbsoluteUri)
            {
                return(bmp.BaseUri == null);
            }
            return(false);
        }
예제 #10
0
 private static void GetWindowIcon(Func <BitmapSource?> imageGetter, ref bool imageGotFlag)
 {
     lock (SyncLock)
     {
         if (imageGotFlag)
         {
             return;
         }
         try
         {
             _windowIcon = imageGetter();
         }
         finally
         {
             imageGotFlag = true;
         }
     }
 }
예제 #11
0
        private static BitmapSource MakeFrame(
            Int32Size fullSize,
            BitmapSource rawFrame, FrameMetadata metadata,
            BitmapSource?baseFrame)
        {
            if (baseFrame == null && IsFullFrame(metadata, fullSize))
            {
                // No previous image to combine with, and same size as the full image
                // Just return the frame as is
                return(rawFrame);
            }

            var visual = new DrawingVisual();

            using (var context = visual.RenderOpen())
            {
                if (baseFrame != null)
                {
                    var fullRect = new Rect(0, 0, fullSize.Width, fullSize.Height);
                    context.DrawImage(baseFrame, fullRect);
                }

                var rect = new Rect(metadata.Left, metadata.Top, metadata.Width, metadata.Height);
                context.DrawImage(rawFrame, rect);
            }
            var bitmap = new RenderTargetBitmap(
                fullSize.Width, fullSize.Height,
                96, 96,
                PixelFormats.Pbgra32);

            bitmap.Render(visual);

            var result = new WriteableBitmap(bitmap);

            if (result.CanFreeze && !result.IsFrozen)
            {
                result.Freeze();
            }
            return(result);
        }
예제 #12
0
 public void preload()
 {
     this.mapImageSource = ImageUriHelper.instance.imageSource(mapImageSourcePath);
 }
예제 #13
0
        private static ObjectAnimationUsingKeyFrames?GetAnimation(Image imageControl, BitmapSource source)
        {
            ObjectAnimationUsingKeyFrames?animation = AnimationCache.GetAnimation(source,
                                                                                  GetRepeatBehavior(imageControl));

            if (animation != null)
            {
                return(animation);
            }

            GifFile?gifMetadata;
            var     decoder = GetDecoder(source, out gifMetadata) as GifBitmapDecoder;

            if (decoder != null && decoder.Frames.Count > 1)
            {
                Int32Size fullSize = GetFullSize(decoder, gifMetadata);
                int       index    = 0;
                animation = new ObjectAnimationUsingKeyFrames();
                TimeSpan     totalDuration = TimeSpan.Zero;
                BitmapSource?baseFrame     = null;
                foreach (BitmapFrame rawFrame in decoder.Frames)
                {
                    FrameMetadata metadata = GetFrameMetadata(decoder, gifMetadata, index);

                    BitmapSource frame    = MakeFrame(fullSize, rawFrame, metadata, baseFrame);
                    var          keyFrame = new DiscreteObjectKeyFrame(frame, totalDuration);
                    animation.KeyFrames.Add(keyFrame);

                    totalDuration += metadata.Delay;

                    switch (metadata.DisposalMethod)
                    {
                    case FrameDisposalMethod.None:
                    case FrameDisposalMethod.DoNotDispose:
                        baseFrame = frame;
                        break;

                    case FrameDisposalMethod.RestoreBackground:
                        if (IsFullFrame(metadata, fullSize))
                        {
                            baseFrame = null;
                        }
                        else
                        {
                            baseFrame = ClearArea(frame, metadata);
                        }
                        break;

                    case FrameDisposalMethod.RestorePrevious:
                        // Reuse same base frame
                        break;
                    }

                    index++;
                }
                animation.Duration       = totalDuration;
                animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, gifMetadata);

                AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
                return(animation);
            }
            return(null);
        }
예제 #14
0
 public void preload()
 {
     this.mapImageSource = AppModel.instance.imageSource(mapImageSourcePath);
 }