コード例 #1
0
        public void Load_returns_correct_image_version_for_various_optimal_scale_factors()
        {
            var x1Stream      = new MemoryStream();
            var x2Stream      = new MemoryStream();
            var multiResImage = new MultiResolutionImage(
                new[]
            {
                new ImageStream(1.0, () => x1Stream),
                new ImageStream(2.0, () => x2Stream)
            });
            var iv0_5 = multiResImage.Load <Stream>(0.5, cache: false);

            Assert.That(iv0_5.ScaleFactor.Value, Is.EqualTo(1.0));
            Assert.That(ReferenceEquals(iv0_5.Image, x1Stream));

            var iv1 = multiResImage.Load <Stream>(1, cache: false);

            Assert.That(iv1.ScaleFactor.Value, Is.EqualTo(1.0));
            Assert.That(ReferenceEquals(iv1.Image, x1Stream));

            var iv1_5 = multiResImage.Load <Stream>(1.5, cache: false);

            Assert.That(iv1_5.ScaleFactor.Value, Is.EqualTo(2.0));
            Assert.That(ReferenceEquals(iv1_5.Image, x2Stream));

            var iv2 = multiResImage.Load <Stream>(2, cache: false);

            Assert.That(iv2.ScaleFactor.Value, Is.EqualTo(2.0));
            Assert.That(ReferenceEquals(iv2.Image, x2Stream));

            var iv3 = multiResImage.Load <Stream>(3, cache: false);

            Assert.That(iv3.ScaleFactor.Value, Is.EqualTo(2.0));
            Assert.That(ReferenceEquals(iv3.Image, x2Stream));
        }
コード例 #2
0
        public static BitmapImage GetBestIcon(this MultiResolutionImage icon, double dpi)
        {
            // Load icons if not already loaded
            if (icon.LoadedIcons.Count == 0)
            {
                LoadIcons(icon);
            }

            BitmapImage chosenImage = null;

            foreach (var res in icon.LoadedIcons)
            {
                var image = res as BitmapImage;
                if (chosenImage == null || // Nothing selected yet, or
                    // Current is lower DPI than screen and this one is higher than current, or
                    (chosenImage.DpiX <= dpi && image.DpiX > chosenImage.DpiX) ||
                    // Current is higher DPI than screen and this one is lower but still higher DPI than screen
                    (chosenImage.DpiX > dpi && image.DpiX < chosenImage.DpiX && image.DpiX >= dpi))
                {
                    chosenImage = image;
                }
            }

            return(chosenImage);
        }
コード例 #3
0
        private MultiResolutionImage LoadIcon(string resourceName, CompileContext context)
        {
            int[] resolutions = { 32, 64 };

            var icon = new MultiResolutionImage();

            foreach (int resolution in resolutions)
            {
                string resolutionName = $"{resourceName}_{resolution}.png";

                if (!context.Resources.HasResource(resolutionName))
                {
                    string warnMessage = $"Icon {resolutionName} not found.";
                    Log.LogWarning(warnMessage);
                    context.Errors.Add(new CompileError(LoadErrorCategory.Warning, warnMessage));
                    continue;
                }

                var resource = context.Resources.GetResource(resolutionName);
                using (var stream = resource.Open())
                {
                    byte[] data = ReadToArray(stream);

                    icon.Add(new SingleResolutionImage
                    {
                        Data     = data,
                        MimeType = resource.MimeType
                    });
                }
            }

            return(icon.Count > 0 ? icon : null);
        }
コード例 #4
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            MultiResolutionImage image = value as MultiResolutionImage;

            if (image == null)
            {
                return(null);
            }

            return(image.GetBestIcon(DPI));
        }
コード例 #5
0
        public static void LoadIcons(this MultiResolutionImage icon)
        {
            icon.LoadedIcons.Clear();

            foreach (var res in icon)
            {
                MemoryStream tempStream = new MemoryStream(res.Data);
                var          tempIcon   = new System.Windows.Media.Imaging.BitmapImage();
                tempIcon.BeginInit();
                tempIcon.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                tempIcon.StreamSource  = tempStream;
                tempIcon.EndInit();

                icon.LoadedIcons.Add(tempIcon);
            }
        }
        void LoadIcons(MultiResolutionImage icon)
        {
            icon.LoadedIcons.Clear();

            foreach (var res in icon)
            {
                var tempStream = new MemoryStream(res.Data);
                var tempIcon   = new BitmapImage();
                tempIcon.BeginInit();
                tempIcon.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                tempIcon.StreamSource  = tempStream;
                tempIcon.EndInit();

                icon.LoadedIcons.Add(tempIcon);
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: pacificIT/circuitdiagram
        private static MultiResolutionImage SetDefaultIcon(string componentName, string configuration, string defaultIconPath)
        {
            // Construct base name
            string baseName = String.Format("{0}", FormatNameForIcon(componentName));

            if (!String.IsNullOrEmpty(configuration))
            {
                baseName += String.Format("_{0}", FormatNameForIcon(configuration));
            }

            // Attempt to set common DPI values: 32, 64
            int[] resolutions = new int[] { 32, 64 };
            var   returnIcon  = new MultiResolutionImage();

            foreach (int resolution in resolutions)
            {
                string iconFileName = String.Format("{0}_{1}.png", baseName, resolution);
                string iconPath     = Path.Combine(defaultIconPath, iconFileName);

                if (!File.Exists(iconPath))
                {
                    continue;
                }

                var iconData = File.ReadAllBytes(iconPath);
                returnIcon.Add(new SingleResolutionImage()
                {
                    Data = iconData, MimeType = "image/png"
                });
            }

            if (returnIcon.Count > 0)
            {
                return(returnIcon);
            }
            else
            {
                return(null);
            }
        }
コード例 #8
0
        /// <summary>
        /// Populates the toolbox from the xml file.
        /// </summary>
        private void LoadToolbox()
        {
            try
            {
                var toolboxData = ToolboxManager.LoadToolbox();

                // Add shortcuts
                m_toolboxShortcuts.Clear();
                foreach(var cat in toolboxData)
                {
                    foreach(IdentifierWithShortcut component in cat)
                    {
                        if (component.ShortcutKey != Key.None)
                            m_toolboxShortcuts.Add(component.ShortcutKey, component);
                    }
                }

                // Add "select" tool
                var selectIcon = new MultiResolutionImage();
                selectIcon.LoadedIcons.Add(new BitmapImage(new Uri("pack://application:,,,/Images/Select32.png")));
                selectIcon.LoadedIcons.Add(new BitmapImage(new Uri("pack://application:,,,/Images/Select64.png")));
                var selectItem = new CustomToolboxItem()
                {
                    DisplayName = "Select",
                    Icon = selectIcon
                };
                var selectCategory = new List<IToolboxItem>();
                selectCategory.Add(selectItem);
                toolboxData.Insert(0, selectCategory);

                mainToolbox.ItemsSource = toolboxData;

                // Set "select" as the current tool in the toolbox
                ToolboxSelectSelectTool();
            }
            catch
            {
                TaskDialogOptions tdOptions = new TaskDialogOptions();
                tdOptions.Title = "Circuit Diagram";
                tdOptions.MainInstruction = "The toolbox is corrupt.";
                tdOptions.Content = "New items can be added to the toolbox under Tools->Toolbox."; // Toolbox missing
                tdOptions.CommonButtons = TaskDialogCommonButtons.Close;
                tdOptions.Owner = this;
                this.Dispatcher.BeginInvoke(new Action(() => TaskDialog.Show(tdOptions)));

                // Create new toolbox file
                ToolboxManager.OverwriteToolbox();
            }
        }