[英]c#: how can i save a chart and a textbox in a single image (e.g. *.png)?
Solution: Used DrawtoBitmap to draw the chart and textbox separately on the same bitmap, and saved it using bitmap.save. 解决方案:使用DrawtoBitmap在同一位图上分别绘制图表和文本框,并使用bitmap.save保存它。
Problem Description: I have code which analyses data and draws graphs saving them as a .png file. 问题描述:我有分析数据并绘制图形并将它们另存为.png文件的代码。 I want to add text in the .png file along with the graph chart.
我想在.png文件中添加文本以及图形图表。 Is it possible to collage the chart and textbox to a single image, and save it as a single file?
是否可以将图表和文本框拼贴为单个图像,然后将其另存为单个文件? Please the the graph image.png i have till now, and how i want my text to be in the same image.
请获取我到目前为止的图形image.png,以及我希望我的文本在同一图像中。
You have quite a number of options: 您有很多选择:
You can take a screenshot 您可以截图
You can Save
the Chart
's Image and DrawToBitmap
the TextBox
and then draw the 2nd image over the former. 您可以
Save
Chart
的图像,并在TextBox
绘制DrawToBitmap
,然后在前一个图像上绘制第二个图像。
You can Save
the Chart
's Image and DrawString
the TextBox.Text
over the image 您可以
Save
Chart
的图像,并在图像DrawString
TextBox.Text
绘制为TextBox.Text
。
You can add the TextBox
to the Chart.Controls
(in code!) and then use DrawToBitmap
to make the chart draw both into one image. 您可以将
TextBox
添加到Chart.Controls
(在代码中!),然后使用DrawToBitmap
使图表将两者都绘制到一个图像中。
You can DrawString
the text in a Chart.xxxPaint
event and use DrawToBitmap
您可以对
Chart.xxxPaint
事件中的文本进行DrawString
并使用DrawToBitmap
The next two options make the text a genuine part of the Chart: 接下来的两个选项使文本成为图表的真实组成部分:
(Recommended:) you can use a TextAnnotation
or a RectangleAnnotation
: Place it at a suitable spot on the Chart
and use maybe the TextBox.TextChanged
event to copy the text into the TextAnnotation
. (推荐:)您可以使用
TextAnnotation
或RectangleAnnotation
:将其放置在Chart
的适当位置,并使用TextBox.TextChanged
事件将文本复制到TextAnnotation
。 This is the most 'chart-like' option and most likely what I would do. 这是最“类似于图表”的选项,也是我可能要做的。
You could even replace the TextBox
by a editable TextAnnotation
..! 您甚至可以将
TextBox
替换为可编辑的TextAnnotation
..!
Not seeing the chart and not knowing the purpose in any detail, it is hard to recommend which way to go.. 没有看到图表,也没有详细了解目标,因此很难建议要走的路。
All Annotations
are a bit tricky to place, but will move when the chart is resized and will even be saved to xml when you serialize it. 所有
Annotations
的放置都有些棘手,但在调整图表大小时会移动,甚至在序列化时还会保存到xml。 Well worth learning! 非常值得学习!
Update: 更新:
Here is an example that places the text right below the Legend
, assuming it is on the default top right position. 这是一个示例,假设该文本位于默认的右上位置,将其放置在
Legend
正下方。
Note that an ElementPosition
contains both location and size and that all numbers are in percentages of the container, in out case of the Chart
..: 注意 ,在
Chart
以外的情况下, ElementPosition
包含位置和大小,并且所有数字均以容器的百分比表示:
First we declare it globally; 首先,我们在全球进行声明; we also could access it via the
chart.Annotions
collection but I'm lazy; 我们也可以通过
chart.Annotions
集合访问它,但是我很懒。 also note that I use a RectangleAnnotation
, which is the same as TextAnnotation
but with the option of back color and border..: 还请注意,我使用了
RectangleAnnotation
,它与TextAnnotation
相同,但是具有背景色和边框的选项。
RectangleAnnotation RA = null;
To create it I use this code: 要创建它,我使用以下代码:
RA = new RectangleAnnotation();
RA.BackColor = Color.LightPink;
RA.Alignment = ContentAlignment.TopLeft;
chart.Annotations.Add(RA);
RaPos();
To place it I use a function: 要放置它,我使用一个函数:
void RaPos()
{
if (RA == null) return;
ElementPosition LP = chart.Legends[0].Position;
RA.X = LP.X;
RA.Y = LP.Bottom + 5; // 5% below the legend
RA.Width = LP.Width;
RA.Height = 100 - LP.Bottom - 10; // leave 10% of the remaining space
}
We need to adapt the position at some points like here: 我们需要像下面这样调整位置:
private void chart_SizeChanged(object sender, EventArgs e)
{
RaPos();
}
And to synch it with the textbox: 并将其与文本框同步:
private void textBox1_TextChanged(object sender, EventArgs e)
{
RA.Text = textBox1.Text;
}
When saving all Annotations do get saved, unless they were placed outside the visible area..: 保存所有注释时,除非将它们放置在可见区域之外,否则确实会保存它们。
chart.SaveImage(someFileName.png, ChartImageFormat.Png);
This annotation will always keep docked below the legend and will resize its height to fill the remaining space nicely.. 该注释将始终停靠在图例下方,并将调整其高度以很好地填充剩余空间。
Also note that by adding this: RA.AllowTextEditing = true;
还请注意,通过添加以下内容:
RA.AllowTextEditing = true;
the user could doubleclick the rectangle and put the Annotation
into editing mode with the need for a separate TextBox
.. 用户可以双击该矩形,然后将
Annotation
置于编辑模式,并需要单独的TextBox
。
Update 2: 更新2:
Now, as you have posted the chart, I can see that you are docking the Legend to the bottom. 现在,当您发布图表时,我可以看到您将图例停靠在底部。 Of course the code would need some changes, maybe like this:
当然,代码需要进行一些更改,例如:
When setting it up I create some space at the right side: 设置它时,我在右侧创建了一些空间:
ChartArea ca = chart.ChartAreas[0];
ca.Position = new ElementPosition(5, 5, 75, 85);
And in the positioning I create the new ElementPosition directly so to fit in: 在定位中,我直接创建新的ElementPosition以便适合:
// numbers are in percent!!
RA.X = 83;
RA.Y = 8;
RA.Width = 15;
RA.Height = 80;
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.