dotnet core6 image export issue

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
sihmon
Newbie
Newbie
Posts: 12
Joined: Thu Jul 14, 2022 12:00 am

dotnet core6 image export issue

Post by sihmon » Thu Jan 19, 2023 5:34 am

Visual Studio 2022, dotnet core6
Steema.TeeChart.NET(4.2022.12.1)

After drawing the chart, I want to save it as an image.
I've drawn it with the function below, but the first one draws well, but from the second it draws everything in black.
If I open TeeChart Editor Tool and copy or save from the Export menu, a black image is saved.
Please check it.

TChart.Image.Bitmap.CopyToClipboard();
TChart.Export.Image.Bitmap.Save(fileNames);

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

Re: dotnet core6 image export issue

Post by Christopher » Thu Jan 19, 2023 9:06 am

Hello,

the following code:

Code: Select all

    public Form1()
    {
      InitializeComponent();
      InitializeChart();
    }

    private void InitializeChart()
    {
      var bar = new Bar(tChart1.Chart);

      bar.FillSampleValues();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      tChart1.Export.Image.Bitmap.CopyToClipboard();
      tChart1.Export.Image.Bitmap.Save(@"C:\tmp\chart.bmp");
    }
Successfully copies a bitmap to the clipboard and also produces a valid bitmap image in .NET 6.0 with NuGet v.4.2022.12.1. Could you please modify the above code so we can reproduce your problem here?
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

sokim
Newbie
Newbie
Posts: 1
Joined: Thu Aug 04, 2016 12:00 am

Re: dotnet core6 image export issue

Post by sokim » Thu Jan 26, 2023 1:42 pm

I found a sample when the problem occurs.
First, there is no problem when exporting an image with the button docked to the top and then the tchart docked to the fill.

Code: Select all

        private TChart tChart1; 

        public Form1()
        {
            InitializeComponent();

            Button btnOK = new Button();
            btnOK.Text = "image";
            btnOK.Dock = DockStyle.Top;
            this.Controls.Add((Button)btnOK);
            btnOK.Click += (s, e) =>
            {
                tChart1.Export.Image.Bitmap.CopyToClipboard();
            };

            tChart1 = new TChart();
            tChart1.Dock = DockStyle.Fill;
            this.Controls.Add(tChart1);
        }
Image
Image



However, if the tchart is first docked as fill and then the button is docked as top, a problem occurs when exporting the image. A black bar as high as the height of the button is displayed at the top.

Code: Select all

        private TChart tChart1; 

        public Form1()
        {
            InitializeComponent();


            tChart1 = new TChart();
            tChart1.Dock = DockStyle.Fill;
            this.Controls.Add(tChart1);

            Button btnOK = new Button();
            btnOK.Text = "image";
            btnOK.Dock = DockStyle.Top;
            this.Controls.Add((Button)btnOK);
            btnOK.Click += (s, e) =>
            {
                tChart1.Export.Image.Bitmap.CopyToClipboard();
            };
        }
Image
Image

please check this.
I hope for good news regarding this issue.

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

Re: dotnet core6 image export issue

Post by Christopher » Thu Jan 26, 2023 3:46 pm

Hello,
I hope for good news regarding this issue.
If I use this code:

Code: Select all

    private TChart tChart1;

    public Form1()
    {
      InitializeComponent();

      tChart1 = new TChart();
      tChart1.Dock = DockStyle.Fill;
      this.Controls.Add(tChart1);

      Button btnOK = new Button();
      btnOK.Text = "image";
      btnOK.Dock = DockStyle.Top;
      this.Controls.Add((Button)btnOK);
      btnOK.Click += (s, e) =>
      {
        tChart1.Export.Image.Bitmap.CopyToClipboard();
      };

      this.Paint += (s, e) =>
      {
        MessageBox.Show($"ClipRectangle Height = {e.ClipRectangle.Height}, tChart1 Height = {tChart1.Height}, button Height = {btnOK.Height}");
      };
    }
I obtain this:
Screenshot from 2023-01-26 15-49-30.png
Screenshot from 2023-01-26 15-49-30.png (21.5 KiB) Viewed 7024 times


Whereas with this code:

Code: Select all

    private TChart tChart1;

    public Form1()
    {
      InitializeComponent();

      Button btnOK = new Button();
      btnOK.Text = "image";
      btnOK.Dock = DockStyle.Top;
      this.Controls.Add((Button)btnOK);
      btnOK.Click += (s, e) =>
      {
        tChart1.Export.Image.Bitmap.CopyToClipboard();
      };

      tChart1 = new TChart();
      tChart1.Dock = DockStyle.Fill;
      this.Controls.Add(tChart1);
      btnOK.Click += (s, e) =>
      {
        tChart1.Export.Image.Bitmap.CopyToClipboard();
      };

      this.Paint += (s, e) =>
      {
        MessageBox.Show($"ClipRectangle Height = {e.ClipRectangle.Height}, tChart1 Height = {tChart1.Height}, button Height = {btnOK.Height}");
      };
    }
I obtain this:
Screenshot from 2023-01-26 15-53-28.png
Screenshot from 2023-01-26 15-53-28.png (20.16 KiB) Viewed 7024 times
This helps us to understand what is happening. When we add the Button to the Form's Controls collection _after_ we add the Chart, the Chart's Height area is reduced by the Height of the Button. When we add the Button to the Form's Controls collection _before_ we add the Chart, the Chart maintains the same Height as the Height of the Form's ClipRectangle. When the Chart's Height is reduced, we obtain the black rectangle, and when the Chart's Height isn't reduced we do not.

Our best suggestion is that you design your Windows Forms so that the addition of Controls to the Form's Controls collection does not reduce Height of the Chart. In the case of the code snippet that produces a black rectangle, the first snippet, removing the black rectangle involves moving a single line of code:

Code: Select all

diff --git a/ctl1.txt b/ctl2.txt
index 431ee06..13e3488 100644
--- a/ctl1.txt
+++ b/ctl2.txt
@@ -6,12 +6,12 @@
 
       tChart1 = new TChart();
       tChart1.Dock = DockStyle.Fill;
-      this.Controls.Add(tChart1);
 
       Button btnOK = new Button();
       btnOK.Text = "image";
       btnOK.Dock = DockStyle.Top;
       this.Controls.Add((Button)btnOK);
+      this.Controls.Add(tChart1);
       btnOK.Click += (s, e) =>
       {
         tChart1.Export.Image.Bitmap.CopyToClipboard();
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

sihmon
Newbie
Newbie
Posts: 12
Joined: Thu Jul 14, 2022 12:00 am

Re: dotnet core6 image export issue

Post by sihmon » Thu Jan 26, 2023 10:54 pm

That's not what I mean.
There is a problem with the image copied to the clipboard by clicking the btnOk button.
Similarly, the command below also has a black bar at the top.

tChart1.Export.Image.Bitmap.Save(@"C:\tmp\chart.bmp");

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

Re: dotnet core6 image export issue

Post by Christopher » Fri Jan 27, 2023 7:35 am

Hello,
sihmon wrote:
Thu Jan 26, 2023 10:54 pm
There is a problem with the image copied to the clipboard by clicking the btnOk button.
yes, that's what I referred to as the 'black rectangle' in my last message. You will find, as I said, that changing the position of that single line of code resolves the issue of a 'black rectangle' both when copying an image to the clipboard and saving an image to file.
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

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

Re: dotnet core6 image export issue

Post by Christopher » Fri Jan 27, 2023 8:46 am

Hello,

Just to be clear. This code:

Code: Select all

    private TChart tChart1;

    public Form1()
    {
      InitializeComponent();

      tChart1 = new TChart();
      tChart1.Dock = DockStyle.Fill;
      this.Controls.Add(tChart1);

      Button btnOK = new Button();
      btnOK.Text = "image";
      btnOK.Dock = DockStyle.Top;
      this.Controls.Add((Button)btnOK);
      btnOK.Click += (s, e) =>
      {
        tChart1.Export.Image.Bitmap.CopyToClipboard();
      };
    }
gives me this image when I click the button:
Screenshot from 2023-01-27 09-42-46.png
Screenshot from 2023-01-27 09-42-46.png (17.5 KiB) Viewed 6986 times
Whereas this code:

Code: Select all

    private TChart tChart1;

    public Form1()
    {
      InitializeComponent();

      tChart1 = new TChart();
      tChart1.Dock = DockStyle.Fill;

      Button btnOK = new Button();
      btnOK.Text = "image";
      btnOK.Dock = DockStyle.Top;
      this.Controls.Add((Button)btnOK);
      this.Controls.Add(tChart1);
      btnOK.Click += (s, e) =>
      {
        tChart1.Export.Image.Bitmap.CopyToClipboard();
      };
    }
gives me this image:
Screenshot from 2023-01-27 09-43-52.png
Screenshot from 2023-01-27 09-43-52.png (17.63 KiB) Viewed 6986 times
The difference between the code that gives me the bad image and the code that gives me the good image is a single line:
Screenshot from 2023-01-27 09-45-38.png
Screenshot from 2023-01-27 09-45-38.png (23.93 KiB) Viewed 6986 times
The reason for why this is happening I explain in my first reply. I trust that this makes the issue clear to you.
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