Graphics.Save 方法
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
保存此 Graphics 的当前状态,并使用 GraphicsState标识保存的状态。
public:
System::Drawing::Drawing2D::GraphicsState ^ Save();
public System.Drawing.Drawing2D.GraphicsState Save();
member this.Save : unit -> System.Drawing.Drawing2D.GraphicsState
Public Function Save () As GraphicsState
此方法返回表示此 Graphics的保存状态的 GraphicsState。
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
通过向量(100,0)转换 Windows 窗体的世界转换。
保存窗体的图形状态。
将窗体的世界转换重置为标识(2x2 标识矩阵加上零向量转换),并使用纯红色画笔填充矩形。
还原转换后的图形状态,并使用纯蓝色画笔填充矩形。
结果是左侧未转换的红色填充矩形和窗体右侧的已转换蓝色填充矩形。
public:
void SaveRestore3( PaintEventArgs^ e )
{
// Translate transformation matrix.
e->Graphics->TranslateTransform( 100, 0 );
// Save translated graphics state.
GraphicsState^ transState = e->Graphics->Save();
// Reset transformation matrix to identity and fill rectangle.
e->Graphics->ResetTransform();
e->Graphics->FillRectangle( gcnew SolidBrush( Color::Red ), 0, 0, 100, 100 );
// Restore graphics state to translated state and fill second
// rectangle.
e->Graphics->Restore( transState );
e->Graphics->FillRectangle( gcnew SolidBrush( Color::Blue ), 0, 0, 100, 100 );
}
private void SaveRestore3(PaintEventArgs e)
{
// Translate transformation matrix.
e.Graphics.TranslateTransform(100, 0);
// Save translated graphics state.
GraphicsState transState = e.Graphics.Save();
// Reset transformation matrix to identity and fill rectangle.
e.Graphics.ResetTransform();
e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100);
// Restore graphics state to translated state and fill second
// rectangle.
e.Graphics.Restore(transState);
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 100, 100);
}
Private Sub SaveRestore3(ByVal e As PaintEventArgs)
' Translate transformation matrix.
e.Graphics.TranslateTransform(100, 0)
' Save translated graphics state.
Dim transState As GraphicsState = e.Graphics.Save()
' Reset transformation matrix to identity and fill rectangle.
e.Graphics.ResetTransform()
e.Graphics.FillRectangle(New SolidBrush(Color.Red), 0, 0, 100, 100)
' Restore graphics state to translated state and fill second
' rectangle.
e.Graphics.Restore(transState)
e.Graphics.FillRectangle(New SolidBrush(Color.Blue), 0, 0, _
100, 100)
End Sub
调用 Graphics的 Save 方法时,会将保存 Graphics 状态的信息块放在堆栈上。 Save 方法返回一个标识该信息块的 GraphicsState。 将标识 GraphicsState 传递给 Restore 方法时,将从堆栈中删除信息块,并用于将 Graphics 还原到 Save 方法调用时的状态。 请注意,对 Save 方法的给定调用返回的 GraphicsState 只能传递给 Restore 方法一次。
可以嵌套对 Save 方法的调用;也就是说,在调用 Restore 方法之前,可以多次调用 Save 方法。 每次调用 Save 方法时,都会将信息块放在堆栈上,并且你会收到信息块的 GraphicsState。 将其中一个对象传递给 Restore 方法时,Graphics 将返回到返回该特定 GraphicsStateSave 方法调用时的状态。 通过该 Save 方法调用放置在堆栈上的信息块将从堆栈中删除,在该堆栈上放置的所有信息块之后,也会删除 Save 方法调用。
对 BeginContainer 方法的调用会将信息块放置在与调用 Save 方法相同的堆栈上。 与 Save 调用配对 Restore 调用时,EndContainer 方法调用与 BeginContainer 方法调用配对。
调用 Restore 方法时,从堆栈中删除对 Save 方法的相应调用后(由 Save 方法或 BeginContainer 方法)上放置的所有信息块。 同样,在调用 EndContainer 方法时,从堆栈中删除对 BeginContainer 方法的相应调用后(由 Save 方法或 BeginContainer 方法)上放置的所有信息块。
产品 | 版本 |
---|---|
.NET | 8 (package-provided), 9 (package-provided), 10 (package-provided) |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9, 10 |