将新矩形 graphics.DrawRectangle(Pens.Red, newRect)添加到list中
时间: 2023-11-28 09:48:02 浏览: 41
可以使用 `List<T>.Add` 方法将新矩形对象添加到 `List<Rectangle>` 中,如下所示:
```
List<Rectangle> rectangleList = new List<Rectangle>();
...
// 创建一个新的矩形对象
Rectangle newRect = new Rectangle(x, y, width, height);
// 将新矩形对象添加到列表中
rectangleList.Add(newRect);
// 在画布上绘制所有矩形
foreach (Rectangle rect in rectangleList)
{
graphics.DrawRectangle(Pens.Red, rect);
}
```
其中 `T` 是列表中元素的类型,这里是 `Rectangle`。在代码中,我们首先声明了 `rectangleList`,然后在需要时使用 `List<T>.Add` 方法将新矩形对象添加到列表中。最后,我们使用 `foreach` 循环遍历列表中所有的矩形对象,并调用 `graphics.DrawRectangle` 方法在画布上绘制出来。
相关问题
e.Graphics.DrawRectangle(visualBrush, null, rect); 不能设置VisualBrush 参数
非常抱歉,我的回答有误。在打印事件的处理程序中,我们需要使用 `e.Graphics.DrawImage` 方法来绘制可视化副本而不是 `DrawRectangle` 方法。以下是修正后的代码:
```csharp
private void PrintScrollViewer(ScrollViewer scrollViewer)
{
// 创建一个可视化副本
VisualBrush visualBrush = new VisualBrush(scrollViewer);
// 创建一个打印文档对象
PrintDocument printDocument = new PrintDocument();
// 设置打印页面的大小和边距
printDocument.PrintPage += (sender, e) =>
{
double pageWidth = e.PrintableArea.Width;
double pageHeight = e.PrintableArea.Height;
// 计算可视化副本在打印页面上的位置和大小
double scaleX = pageWidth / scrollViewer.ActualWidth;
double scaleY = pageHeight / scrollViewer.ActualHeight;
double scale = Math.Min(scaleX, scaleY);
double translateX = (pageWidth - scrollViewer.ActualWidth * scale) / 2;
double translateY = (pageHeight - scrollViewer.ActualHeight * scale) / 2;
// 设置绘制的变换矩阵
e.Graphics.Transform = new Matrix(scale, 0, 0, scale, translateX, translateY);
// 绘制可视化副本到打印页面上
e.Graphics.DrawImage(visualBrush.Visual, new Rect(0, 0, scrollViewer.ActualWidth, scrollViewer.ActualHeight));
e.HasMorePages = false; // 只打印一页
};
// 打开打印对话框
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintDocument(printDocument.DocumentPaginator, "Printing ScrollViewer");
}
}
```
在这个修正后的代码中,我们首先计算可视化副本在打印页面上的位置和大小,以便将其正确地缩放和定位在打印页面上。然后,使用 `e.Graphics.Transform` 设置绘制的变换矩阵,以便将可视化副本绘制到正确的位置和大小。最后,使用 `e.Graphics.DrawImage` 方法将可视化副本绘制到打印页面上。
请注意,你可能需要根据自己的需求进行适当的修改和扩展。这只是一个示例代码,供你参考。
怎么将winform的picturebox中使用g.DrawRectangle画出的矩形四个点坐标保存在text文件中
您可以使用 StreamWriter 类将矩形的四个点坐标保存到文本文件中。具体步骤如下:
1. 在画矩形的方法中,获取矩形的四个点坐标。
例如,使用 Rectangle 类的 Location 和 Size 属性获取矩形的左上角坐标和宽高:
```
Rectangle rect = new Rectangle(10, 10, 50, 50);
int x1 = rect.Location.X;
int y1 = rect.Location.Y;
int x2 = rect.Location.X + rect.Size.Width;
int y2 = rect.Location.Y + rect.Size.Height;
```
2. 创建一个 StreamWriter 对象,打开或创建要保存的文本文件。
例如,使用以下代码创建一个名为“coordinates.txt”的文本文件:
```
StreamWriter writer = new StreamWriter("coordinates.txt");
```
3. 将矩形的四个点坐标写入文本文件中。
例如,使用以下代码将矩形的四个点坐标写入文本文件:
```
writer.WriteLine($"({x1}, {y1})");
writer.WriteLine($"({x2}, {y1})");
writer.WriteLine($"({x2}, {y2})");
writer.WriteLine($"({x1}, {y2})");
```
4. 关闭 StreamWriter 对象。
例如,使用以下代码关闭 StreamWriter 对象:
```
writer.Close();
```
完整示例代码:
```
private void DrawRectangle()
{
Rectangle rect = new Rectangle(10, 10, 50, 50);
int x1 = rect.Location.X;
int y1 = rect.Location.Y;
int x2 = rect.Location.X + rect.Size.Width;
int y2 = rect.Location.Y + rect.Size.Height;
using (Graphics g = pictureBox1.CreateGraphics())
{
g.DrawRectangle(Pens.Black, rect);
}
using (StreamWriter writer = new StreamWriter("coordinates.txt"))
{
writer.WriteLine($"({x1}, {y1})");
writer.WriteLine($"({x2}, {y1})");
writer.WriteLine($"({x2}, {y2})");
writer.WriteLine($"({x1}, {y2})");
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)