Export chart to xaml icons disappear

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
yosi
Newbie
Newbie
Posts: 5
Joined: Mon May 19, 2014 12:00 am

Export chart to xaml icons disappear

Post by yosi » Tue Oct 28, 2014 10:33 am

Hello,
We are using WPF TChart.
When we try to add to the chart some text and icons
and export the chart to xaml, the icons are not shown
When exporting the chart to bitmap the icons do show up.
We need the icons to show when exporting to xaml..
I add a sample project that demonstrate the problem,
just click "show xaml and image" button.

Thanks for your help
Best regard
Attachments
TChartXamlBug.rar
(59.59 KiB) Downloaded 663 times

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Export chart to xaml icons disappear

Post by Christopher » Tue Oct 28, 2014 3:54 pm

yosi wrote: When exporting the chart to bitmap the icons do show up.
We need the icons to show when exporting to xaml..
d
You can add them in manually to the XAML file, e.g.

Code: Select all

<Image Canvas.Left="20" Canvas.Top="10" Source="D:\Downloads\TChartXamlBug\TChartXamlBug\WpfApplication54\WpfApplication54\pic20.png"/>
<TextBlock TextOptions.TextFormattingMode="Ideal" Canvas.Left="50" Canvas.Top="10" Foreground="#FF000000" FontFamily="Verdana" FontSize="11.2" >
X
</TextBlock>
The problem with exporting the TeeChart "Graphics3D.Draw(x, y, icon)" method is knowing where to leave the image so the XAML file has access to it ... anyhow, I have added the issue to our bug database with id=985.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

yosi
Newbie
Newbie
Posts: 5
Joined: Mon May 19, 2014 12:00 am

Re: Export chart to xaml icons disappear

Post by yosi » Sun Nov 02, 2014 9:42 am

Thanks for your answer.

We are using your component as part of critical care software.
Doctors and nurses using reports on daily base.
Your request that we will handle these icons is beyond our scope of development schedule.
We are using your component as a black box and we expect that a trivial behaver such as this will work.
We need a time line for fixing this bug so we will able to update our customers.

Best Regards

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Export chart to xaml icons disappear

Post by Christopher » Mon Nov 03, 2014 10:56 am

yosi wrote:Thanks for your answer.
You're very welcome.

An alternative way to solve the issue is the following:

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xaml;
using System.Xml;
using Steema.TeeChart.WPF.Drawing;
using XamlReader = System.Windows.Markup.XamlReader;
using XamlWriter = System.Windows.Markup.XamlWriter;

namespace WpfApplication54
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    private const string xamlFile = "TTT.Xaml";
    private const string imageFile = "TTT.bmp";
    private string _xamlFilePath;
    private string _imageFilePath;

    public MainWindow()
    {
      InitializeComponent();
      SetFilePath();
    }

    private void TChartControl_OnAfterDraw(object sender, Graphics3D g)
    {
      var icon = (BitmapImage)Resources["V"];
      int x = 0;
      int y = 0;
      for (int i = 0; i < 10; i++)
      {
        x += 20;
        y += 10;
        TChartControl.Graphics3D.Draw(x, y, icon);
        TChartControl.Graphics3D.TextOut(x + 30, y, "X ");
      }
    }

    private void SetFilePath()
    {
      var dir = Environment.CurrentDirectory;

      _xamlFilePath = System.IO.Path.Combine(dir, xamlFile);
      _imageFilePath = System.IO.Path.Combine(dir, imageFile);
    }


    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {

      MyXAMLFormat xmal = new MyXAMLFormat(TChartControl.Chart);
      xmal.Save(_xamlFilePath);
      TChartControl.Export.Image.Bitmap.Save(_imageFilePath);

      Window1 win = new Window1();
      object canvas = null;
      using (var fs = new FileStream(_xamlFilePath, FileMode.Open, FileAccess.Read))
      {
        canvas = XamlReader.Load(fs);
      }

      win.PalceHolder1.Content = canvas;
      BitmapImage bimg = BitmapFromUri(new Uri(_imageFilePath));
      win.PalceHolder2.Source = bimg;
      win.ShowDialog();


    }

    public static BitmapImage BitmapFromUri(Uri source)
    {
      var bitmap = new BitmapImage();
      bitmap.BeginInit();
      bitmap.UriSource = source;
      bitmap.CacheOption = BitmapCacheOption.OnLoad;
      bitmap.EndInit();
      return bitmap;
    }
  }

  public class MyGraphics3DXAML : Steema.TeeChart.WPF.Drawing.Graphics3DXAML
  {
    public MyGraphics3DXAML(Stream stream, Steema.TeeChart.WPF.Chart chart, bool doShowImage)
      : base(stream, chart, doShowImage)
    {
    }

    private int imageCount = 0;

    public override void Draw(double x, double y, BitmapSource image)
    {
      ++imageCount;
      string path = ImageExportPath + "\\MyImage" + imageCount.ToString() + ".png";
      using (var fileStream = new FileStream(path, FileMode.Create))
      {
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(fileStream);
      }
      AddToStream("<Image Width=\"" + image.PixelWidth + "\"  Height=\"" + image.PixelHeight + "\" Canvas.Left=\"" + FloatToStr(x) + "\" Canvas.Top=\"" + FloatToStr(y) + "\" Source=\"" + path + "\"/>");
    }

    public void AddToStreamPublic(string s)
    {
      AddToStream(s);
    }

    public string ImageExportPath
    {
      get;
      set;
    }

  }

  public class MyXAMLFormat : Steema.TeeChart.WPF.Export.XAMLFormat
  {
    Steema.TeeChart.WPF.Chart chart;

    public MyXAMLFormat(Steema.TeeChart.WPF.Chart c)
      : base(c)
    {
      chart = c;
    }

    public override void Save(string FileName)
    {
      ImageExportPath = System.IO.Path.GetDirectoryName(FileName);
      base.Save(FileName);
    }

    public override void Save(Stream stream)
    {
      Steema.TeeChart.WPF.Drawing.Graphics3D oldGraphics = chart.Graphics3D;

      try
      {
        chart.Graphics3D = new MyGraphics3DXAML(stream, chart, false);
        ((MyGraphics3DXAML)chart.Graphics3D).ImageExportPath = ImageExportPath;
        chart.Draw(null, new Rect(0, 0, Width, Height));

        AdornerLayer parent = AdornerLayer.GetAdornerLayer(chart.Parent.GetControl());
        if (parent != null)
        {
          Adorner[] adorners = parent.GetAdorners(chart.Parent.GetControl());
          if (adorners != null)
          {
            foreach (Steema.TeeChart.WPF.TeeAdorner item in adorners)
            {
              item.Draw(chart.Graphics3D);
            }
          }
        }

        ((MyGraphics3DXAML)chart.Graphics3D).AddToStreamPublic("</Canvas>");
      }
      finally
      {
        chart.Graphics3D = oldGraphics;
      }
    }

    public string ImageExportPath
    {
      get;
      set;
    }
  }
}
If you are happy with this solution, please let me know and I'll add it to the TeeChart source code.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

yosi
Newbie
Newbie
Posts: 5
Joined: Mon May 19, 2014 12:00 am

Re: Export chart to xaml icons disappear

Post by yosi » Mon Nov 17, 2014 2:08 pm

Hi
Thanks for the solution
It works if the picture is on the disk.
If we resize the pictures and the pictures is in memory it not working
I add test project that demonstrate the Situation
I would appreciate your help
Best Regards
Attachments
TChartXamParse.rar
(69.82 KiB) Downloaded 659 times

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Export chart to xaml icons disappear

Post by Christopher » Mon Nov 17, 2014 3:09 pm

Hello,

You're code is wrong. Have a look at the TTT.Xaml file in a text editor:

Code: Select all

</TextBlock>
<Image Width="24"  Height="24" Canvas.Left="140" Canvas.Top="70" Source="System.Windows.Media.Imaging.BitmapFrameEncode"/>
<TextBlock TextOptions.TextFormattingMode="Ideal" Canvas.Left="170" Canvas.Top="70" Foreground="#FF000000" FontFamily="Verdana" FontSize="11.2" >
X 
</TextBlock>
This " Source="System.Windows.Media.Imaging.BitmapFrameEncode"" is wrong, and it is wrong because of your code in MyGraphics3DXAML:

Code: Select all

        public override void Draw(double x, double y, BitmapSource image)
        {                      
            AddToStream("<Image Width=\"" + image.PixelWidth + "\"  Height=\"" + image.PixelHeight 
                + "\" Canvas.Left=\"" + FloatToStr(x) + "\" Canvas.Top=\""+ FloatToStr(y) 
                + "\" Source=\"" +image + "\"/>");          
        }
If you have a look at my original code, you will be able to see how to do this correctly.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

yosi
Newbie
Newbie
Posts: 5
Joined: Mon May 19, 2014 12:00 am

Re: Export chart to xaml icons disappear

Post by yosi » Tue Nov 18, 2014 8:11 am

Hi
As I say if you work with path for the pictures it work and the TTT.xaml file look like this
<Image Width="24" Height="24" Canvas.Left="80" Canvas.Top="40" Source="pack://application:,,,/TChartXamParse;component/pic20.png"/>
<TextBlock TextOptions.TextFormattingMode="Ideal" Canvas.Left="110" Canvas.Top="40" Foreground="#FF000000" FontFamily="Verdana" FontSize="11.2" >
X
</TextBlock>
Look for the ResizeBitmap method it resize the picture and the draw method get
System.Windows.Media.Imaging.BitmapFrameEncode so we don’t have path
If you mark this method it work and get the picture path
We need solution for this Situation
Best regards

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Export chart to xaml icons disappear

Post by Christopher » Tue Nov 18, 2014 10:36 am

yosi wrote: Look for the ResizeBitmap method it resize the picture and the draw method get
System.Windows.Media.Imaging.BitmapFrameEncode so we don’t have path
If you mark this method it work and get the picture path
We need solution for this Situation
I'm sorry, I don't understand your problem. I see two possibilities:
1) you save the TTT.xaml to file, in which case there is no problem
2) you save the TTT.xaml to stream, in which case there is no solution
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

yosi
Newbie
Newbie
Posts: 5
Joined: Mon May 19, 2014 12:00 am

Re: Export chart to xaml icons disappear

Post by yosi » Tue Nov 18, 2014 11:47 am

Hi
The TTT file saves to Xaml file.
The problem is to add the pictures to the xaml file in the draw method
public override void Draw(double x, double y, BitmapSource image)
{
AddToStream("<Image Width=\"" + image.PixelWidth + "\" Height=\"" + image.PixelHeight
+ "\" Canvas.Left=\"" + FloatToStr(x) + "\" Canvas.Top=\""+ FloatToStr(y)
+ "\" Source=\"" +image + "\"/>");
}
Because the picture is BitmapFrameEncode type after it resize in the ResizeBitmap method
The chart and the Bitmap.Save() display the images;
The xaml not, it set the source of the images to BitmapFrameEncode this is the problem
The test project demo this
Best Regards,

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Export chart to xaml icons disappear

Post by Christopher » Tue Nov 18, 2014 1:58 pm

yosi wrote:Because the picture is BitmapFrameEncode type after it resize in the ResizeBitmap method
The chart and the Bitmap.Save() display the images;
The xaml not, it set the source of the images to BitmapFrameEncode this is the problem
The test project demo this
,
If I replace the MyXAMLFormat class and the MyGraphics3DXAML class in your demo with the classes in my example, there is no problem. I do not understand why you cannot use my version of these classes.

The ResizeBitmap is not a problem. This method calls "TChartControl.Graphics3D.Draw(x, y, rIcon);" and this method is overridden in "MyGraphics3DXAML.Draw(double x, double y, BitmapSource image)". If you do not save this image to file in MyGraphics3DXAML.Draw, as I do in my version of this method but as you do not do in your version of this method, then the XAML file cannot contain the correct image.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply