Example #1
0
        public async Task ExportAsImage()
        {
            await InitializeGraphicsDevice();
            await ExecuteAndWaitForPlotsAsync(new string[] {
                "plot(1:10)",
            });

            foreach (var ext in new string[] { "bmp", "jpg", "jpeg", "png", "tif", "tiff" })
            {
                var outputFilePath = _testFiles.GetDestinationPath("ExportedPlot." + ext);
                FileDialog.SaveFilePath = outputFilePath;

                var deviceVC       = _workflow.Plots.GetPlotVisualComponent(_workflow.Plots.ActiveDevice);
                var deviceCommands = new RPlotDeviceCommands(_workflow, deviceVC);

                deviceCommands.ExportAsImage.Should().BeEnabled();
                await deviceCommands.ExportAsImage.InvokeAsync();

                File.Exists(outputFilePath).Should().BeTrue();
                CoreShell.LastShownErrorMessage.Should().BeNullOrEmpty();

                var image = BitmapImageFactory.Load(outputFilePath);
                image.PixelWidth.Should().Be(600);
                image.PixelHeight.Should().Be(500);
                ((int)Math.Round(image.DpiX)).Should().Be(96);
                ((int)Math.Round(image.DpiY)).Should().Be(96);
            }
        }
Example #2
0
        public async Task ExportAsImage()
        {
            using (await _workflow.GetOrCreateVisualComponent(_componentContainerFactory)) {
                // We set an initial size for plots, because export as image command
                // will use the current size of plot control as export parameter.
                await _workflow.Plots.ResizeAsync(600, 500, 96);

                await ExecuteAndWaitForPlotsAsync(new string[] {
                    "plot(1:10)",
                });

                foreach (var ext in new string[] { "bmp", "jpg", "jpeg", "png", "tif", "tiff" })
                {
                    var outputFilePath = _testFiles.GetDestinationPath("ExportedPlot." + ext);
                    CoreShell.SaveFilePath = outputFilePath;

                    _workflow.Plots.Commands.ExportAsImage.Should().BeEnabled();
                    await _workflow.Plots.Commands.ExportAsImage.InvokeAsync();

                    File.Exists(outputFilePath).Should().BeTrue();
                    CoreShell.LastShownErrorMessage.Should().BeNullOrEmpty();

                    var image = BitmapImageFactory.Load(outputFilePath);
                    image.PixelWidth.Should().Be(600);
                    image.PixelHeight.Should().Be(500);
                    ((int)Math.Round(image.DpiX)).Should().Be(96);
                    ((int)Math.Round(image.DpiY)).Should().Be(96);
                }
            }
        }
        public async Task <CommandResult> InvokeAsync()
        {
            string filePath = Path.GetTempFileName();

            try {
                await InteractiveWorkflow.Plots.ExportToBitmapAsync(
                    VisualComponent.ActivePlot,
                    "bmp",
                    filePath,
                    VisualComponent.Device.PixelWidth,
                    VisualComponent.Device.PixelHeight,
                    VisualComponent.Device.Resolution);

                InteractiveWorkflow.Shell.DispatchOnUIThread(() => {
                    try {
                        var image = BitmapImageFactory.Load(filePath);
                        Clipboard.SetImage(image);
                    } catch (Exception e) when(!e.IsCriticalException())
                    {
                        InteractiveWorkflow.Shell.ShowErrorMessage(string.Format(Resources.Plots_CopyToClipboardError, e.Message));
                    } finally {
                        try {
                            File.Delete(filePath);
                        } catch (IOException) {
                        }
                    }
                });
            } catch (RPlotManagerException ex) {
                InteractiveWorkflow.Shell.ShowErrorMessage(ex.Message);
            } catch (OperationCanceledException) {
            }

            return(CommandResult.Executed);
        }
Example #4
0
        private ImageSource GetImageFromResources(string name)
        {
            Bitmap      bmp    = null;
            ImageSource source = null;

            switch (name)
            {
            case "RProjectNode":
                bmp = Resources.RProjectNode;
                break;

            case "RFileNode":
                bmp = Resources.RFileNode;
                break;

            case "RDataNode":
                bmp = Resources.RDataNode;
                break;
            }

            if (bmp != null)
            {
                using (MemoryStream memory = new MemoryStream()) {
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
                    memory.Position = 0;
                    source          = BitmapImageFactory.Load(memory);
                }
            }

            return(source);
        }
Example #5
0
        // Create and show a preview image
        // to get fast load
        private static void LoadPreview(Image image, string filePath)
        {
            var previewheight = (int)((image.Parent as FrameworkElement).ActualHeight - 3.5);

            image.Source = BitmapImageFactory.CreateThumbnailFromFile(filePath, previewheight, WriteableBitmapEx.DesiredSize.Height);
            _ImageViewTransformer.SetOriginalDimentions((image.Source as BitmapImage).PixelWidth, (image.Source as BitmapImage).PixelHeight);
            if (!String.IsNullOrEmpty(Properties.Settings.Default.DefaultPreview))
            {
                _ImageViewTransformer.ExecuteTransformWith(Properties.Settings.Default.DefaultPreview);
            }
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string filePath = value as string;

            BitmapImage image = null;

            if (!String.IsNullOrEmpty(filePath))
            {
                image = BitmapImageFactory.CreateThumbnailFromFile(filePath, 64, DesiredSize.Width);
            }

            return(image);
        }
Example #7
0
        private async Task <BitmapImage> GetExpectedImageAsync(string imageType, int width, int height, int res, string name, string code)
        {
            var filePath = _testFiles.GetDestinationPath(_testMethod.Name + name + "." + imageType);
            var script   = string.Format(@"
{0}({1}, width={2}, height={3}, res={4})
{5}
dev.off()
", imageType, filePath.ToRPath().ToRStringLiteral(), width, height, res, code);

            var eval   = _workflow.ActiveWindow.InteractiveWindow.Evaluator;
            var result = await eval.ExecuteCodeAsync(script);

            return(BitmapImageFactory.Load(filePath));
        }
        private async Task <ImageSource> LoadAssetAsync(string itemName, Task <ImageSource> defaultImage)
        {
            var fileName = string.Format(AssetPathFormat, itemName);

            if (!File.Exists(fileName))
            {
                if (_options.DownloadMissingItemImages)
                {
                    await _wikiLoader.ProduceAsync(itemName, fileName).ConfigureAwait(false);
                }
                else
                {
                    return(await defaultImage.ConfigureAwait(false));
                }
            }
            return(BitmapImageFactory.Create(fileName));
        }
 /// <summary>
 /// Returns the image for the ItemClass. Each ItemClass image is only loaded once.
 /// </summary>
 public ImageSource LoadDefaultImage(ItemClass itemClass)
 {
     return(_defaultImageCache.GetOrAdd(itemClass, c =>
     {
         if (Application.Current == null)
         {
             return _errorImage;
         }
         try
         {
             var path = string.Format(ResourcePathFormat, c);
             return BitmapImageFactory.Create(path);
         }
         catch (Exception e)
         {
             Log.Warn(e, "Could not load default file for ItemClass " + c);
             return _errorImage;
         }
     }));
 }
Example #10
0
        private async Task <ImageSource> LoadOfficialAsync(string imageUrl, Task <ImageSource> defaultImage)
        {
            // Remove the query part, remove the host part, remove image/Art/2DItems/, trim slashes
            var relevantPart = Regex.Replace(imageUrl, @"(\?.*)|(.*(\.net|\.com)/)|(image/Art/2DItems/)", "").Trim('/');
            var match        = Regex.Match(relevantPart, @"gen/image/.*?/([a-zA-Z0-9]*)/Item\.png");
            var isRelic      = imageUrl.Contains("&relic=1");

            if (match.Success)
            {
                // These names are too long.
                // They contain groups of 20 chars (as folders) and end with a unique identifier.
                relevantPart = $"gen/{match.Groups[1]}.png";
            }
            else if (isRelic)
            {
                // Non-generated images for relics and normal uniques have the same url, the relic flag is in the
                // query part. As the query is removed, the file name needs to be adjusted for relics.
                relevantPart = relevantPart.Replace(".png", "Relic.png");
            }
            var fileName = string.Format(DownloadedPathFormat, relevantPart);

            if (!File.Exists(fileName))
            {
                if (!_options.DownloadMissingItemImages)
                {
                    return(await defaultImage.ConfigureAwait(false));
                }

                var imgData = await _httpClient.GetByteArrayAsync(imageUrl).ConfigureAwait(false);

                CreateDirectories(fileName);
                WikiApiUtils.SaveImage(imgData, fileName, false);
                Log.Info($"Downloaded item image {fileName} to the file system.");
            }
            return(BitmapImageFactory.Create(fileName));
        }
Example #11
0
 public static BitmapImage ToBitmapImage(this PlotMessage plot)
 {
     return(BitmapImageFactory.Load(plot.FilePath));
 }
Example #12
0
 public static BitmapImage ToBitmapImage(this PlotMessage plot)
 {
     return(BitmapImageFactory.Load(new MemoryStream(plot.Data)));
 }
Example #13
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string filePath = value as string;

            return(BitmapImageFactory.CreateFromFile(filePath));;
        }