Page 1 of 1
dotnet core6 image export issue
Posted: Thu Jan 19, 2023 5:34 am
by 15694038
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);
Re: dotnet core6 image export issue
Posted: Thu Jan 19, 2023 9:06 am
by Christopher
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?
Re: dotnet core6 image export issue
Posted: Thu Jan 26, 2023 1:42 pm
by 15678850
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);
}
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();
};
}
please check this.
I hope for good news regarding this issue.
Re: dotnet core6 image export issue
Posted: Thu Jan 26, 2023 3:46 pm
by Christopher
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 (21.5 KiB) Viewed 7030 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 (20.16 KiB) Viewed 7030 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();
Re: dotnet core6 image export issue
Posted: Thu Jan 26, 2023 10:54 pm
by 15694038
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");
Re: dotnet core6 image export issue
Posted: Fri Jan 27, 2023 7:35 am
by Christopher
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.
Re: dotnet core6 image export issue
Posted: Fri Jan 27, 2023 8:46 am
by Christopher
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 (17.5 KiB) Viewed 6992 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 (17.63 KiB) Viewed 6992 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 (23.93 KiB) Viewed 6992 times
The reason for why this is happening I explain in my first reply. I trust that this makes the issue clear to you.