Пример #1
0
        private void InitCollector(DatasGathering.CollectorConfig conf) {

            _currFileName = null;

            if (_collector != null)
                _collector.Dispose();

            _collector = new DatasGathering.DatasCollector(conf);
            _collector.DatasChanged += _collector_DatasChanged;
            _collector.StateChanged += _collector_StateChanged;
            _collector.AwaitTick += _collector_AwaitTick;
            _chart = new ChartRendering.ChartRenderer(cnvChart, _collector.Datas);            
            _chart.Render();

            lblError.Text = "";
            imgError.Visibility = Visibility.Hidden;

            var t = _collector.Start();

            grdNoConfiguration.Visibility = Visibility.Hidden;
            mnSave.IsEnabled = true;
        }
Пример #2
0
        private void btSaveImage_Click(object sender, RoutedEventArgs e) {

            if (_collector == null)
                return;

            // Create new canvas and renderer
            var cnv = new Canvas() { Margin = cnvChart.Margin, Width = cnvChart.ActualWidth, Height = cnvChart.ActualHeight, Background = System.Windows.Media.Brushes.White };            
            var ch = new ChartRendering.ChartRenderer(cnv, _collector.Datas);
            cnv.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
            cnv.Arrange(new Rect(0, 0, cnv.DesiredSize.Width, cnv.DesiredSize.Height));
            var cwidth = (int)cnv.RenderSize.Width;
            var cheight = (int)(cnv.RenderSize.Height + cnv.Margin.Top * 2);
            var rtb = new RenderTargetBitmap(cwidth, cheight, 96d, 96d, PixelFormats.Default);

            // Generate a white background
            var dv = new DrawingVisual();
            var dvct = dv.RenderOpen();
            dvct.DrawRectangle(System.Windows.Media.Brushes.White, null, new Rect(0, 0, rtb.Width, rtb.Height));
            dvct.Close();
            rtb.Render(dv);

            // Render chart and create Bitmap
            ch.Render(false);
            // Add additional informations overlays            
            AddChartImageInfos(cnv);
            cnv.UpdateLayout();
            rtb.Render(cnv);

            // Open file dialog and save image if OK
            var fd = new SaveFileDialog();
            fd.Filter = "PNG Images|*.png";
            fd.DefaultExt = ".png";
            fd.InitialDirectory = Properties.Settings.Default.fdpath_img;

            if (true == fd.ShowDialog(this)) {
                Properties.Settings.Default.fdpath_img = Path.GetDirectoryName(fd.FileName);
                Properties.Settings.Default.Save();

                var bf = BitmapFrame.Create(rtb);
                var pngEncoder = new PngBitmapEncoder();
                pngEncoder.Frames.Add(bf);                
                using (var st = System.IO.File.OpenWrite(fd.FileName)) {
                    pngEncoder.Save(st);
                }

                var result = MessageBox.Show("Show generated file in Explorer ?", "Image successfully generated", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                    System.Diagnostics.Process.Start("explorer.exe", "/select, " + fd.FileName);
            }
            
        }