简体   繁体   English

C#:如何在单个图像(例如* .png)中保存图表和文本框?

[英]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 . (推荐:)您可以使用TextAnnotationRectangleAnnotation :将其放置在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.

相关问题 C#-过滤表单对象(例如文本框) - C# - Filtering form objects (e.g. textbox) for times 将Kinect for Windows v2的深度和颜色流另存为图像文件(例如png或jpg) - Save depth and color streams of Kinect for Windows v2 as image files(e.g. png or jpg) 我可以在C#或C ++中使用反向地理编码服务器端吗? - Can I use the reverse geocoding server side, e.g. with C# or C++? 我如何公开我的C#脚本(例如VBScript)与之交互? (当前使用SendKeys,但这会引起一些问题) - How can I expose my C# for scripts (e.g. VBScript) to interact with it? (Currently using SendKeys but this is causing some issues) 如何在c#中解析JSON字符串获取错误无法反序列化当前的JSON对象(例如{“name”:“value”}) - How can i parse JSON string in c# Get error Cannot deserialize the current JSON object (e.g. {“name”:“value”}) Serilog C# 如何防止记录大数据,例如图像数据或大数据 JSON object - Serilog C# how to prevent logging big data e.g. image data or large JSON object 如何在不下载的情况下以编程方式判断网站上的二进制文件(例如图像)是否已更改? - How can I programmatically tell if a binary file on a website (e.g. image) has changed without downloading it? C#如何将模糊输入的对象(例如“ var”)作为特定类型的对象(例如“ Form”)访问 - C# How to access an ambiguously typed object (e.g. 'var') as a specifically typed object (e.g. 'Form') 如何在C#项目中以编程方式(例如在预生成事件中)以编程方式设置Visual Studio 2015的OutDir属性? - How can I set Visual Studio 2015's OutDir property programatically, e.g. in a pre-build event, in a C# project? C# 如何将文本框中的值保存为字符串? - C# How can I save a value in a TextBox as a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM