。net图形操作(MSDN)
此示例说明如何在窗体上绘制实心椭圆。
示例
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.FillEllipse(myBrush, new Rectangle(0,0,200,300)); myBrush.Dispose(); formGraphics.Dispose();
编译代码
本示例需要:
- 一个 Windows 应用程序项目。
代码需要处于 form 类的范围内。该窗体实例由 this 来表示。
可靠编程
应该始终对使用系统资源的任何对象(如 Brush 和 Graphics 对象)调用 Dispose。
此示例说明如何在窗体上绘制实心矩形。
示例
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.FillRectangle(myBrush, new Rectangle(0,0,200,300)); myBrush.Dispose(); formGraphics.Dispose();
编译代码
本示例需要:
- 一个 Windows 应用程序项目。
代码需要处于 form 类的范围内。该窗体实例由 this 来表示。
可靠编程
应该始终对使用系统资源的任何对象(如 Brush 和 Graphics 对象)调用 Dispose。
此示例说明如何在窗体上绘制空心椭圆和矩形。
示例
private void DrawEllipse() { System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300)); myPen.Dispose(); formGraphics.Dispose(); } private void DrawRectangle() { System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawRectangle(myPen, new Rectangle(0,0,200,300)); myPen.Dispose(); formGraphics.Dispose(); }
编译代码
该示例需要:
- 一个 Windows 应用程序项目。
代码需要处于 form 类的范围内。该窗体实例由 this 来表示。
可靠编程
应该始终对使用系统资源的任何对象(如 Pen 和 Graphics 对象)调用 Dispose。
此示例说明如何在窗体上竖向绘制文本。
示例
private void DrawVerticalText() { System.Drawing.Graphics formGraphics = this.CreateGraphics(); string drawString = "Sample Text"; System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); float x = 150.0f; float y = 50.0f; System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical); formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat); drawFont.Dispose(); drawBrush.Dispose(); formGraphics.Dispose(); }
编译代码
本示例需要:
- 一个 Windows 应用程序项目。
代码需要处于 form 类的范围内。该窗体实例由 this 来表示。
可靠编程
应该始终对使用系统资源的任何对象(如 Font 和 Graphics 对象)调用 Dispose。
以下情况可能会导致异常:
- 未安装 Arial 字体。
此示例说明如何对当前窗体进行打印预览的副本。
示例
[System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop); private Bitmap memoryImage; private void CaptureScreen() { Graphics mygraphics = this.CreateGraphics(); Size s = this.Size; memoryImage = new Bitmap(s.Width, s.Height, mygraphics); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 = mygraphics.GetHdc(); IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376); mygraphics.ReleaseHdc(dc1); memoryGraphics.ReleaseHdc(dc2); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImage(memoryImage, 0, 0); } private void printButton_Click(System.Object sender, System.EventArgs e) { CaptureScreen(); printPreviewDialog1.Show(); }
编译代码
本示例需要:
- 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
- 名为 printPreviewDialog1 的 PrintPreviewDialog 组件,其 Document 属性设置为 printDocument1。
- 名为 printButton 且包含 Click 事件处理程序的 Button 对象。
该示例代码替换现有的事件处理程序。单击 printButton 时会显示窗体的打印预览。
可靠编程
以下情况可能会导致异常:
- 您没有访问该打印机的权限。
- 您没有使用非托管代码的权限。
- 没有安装打印机。
- 该“打印预览”对话框以前被处置过。在关闭“打印预览”对话框后会出现该情况。
安全性
为了运行此示例,您必须具有执行非托管代码和访问打印机的权限。
此示例说明如何打印 DataGrid 控件。
示例
private void printGrid_Click(System.Object sender, System.EventArgs e) { printDocument1.Print(); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size)); this.InvokePaint(dataGrid1, myPaintArgs); }
编译代码
本示例需要:
- 名为 printGrid 且含有 Click 事件处理程序的按钮控件。
- 名为 dataGrid1 的 DataGrid 控件。
- 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
该示例代码替换现有的事件处理程序。
可靠编程
以下情况可能会导致异常:
- 您没有访问该打印机的权限。
- 没有安装打印机。
此示例说明如何打印文本文件。
示例
System.IO.StreamReader fileToPrint; System.Drawing.Font printFont; private void printButton_Click(object sender, EventArgs e) { string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); fileToPrint = new System.IO.StreamReader(printPath + @"\myFile.txt"); printFont = new System.Drawing.Font("Arial", 10); printDocument1.Print(); fileToPrint.Close(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { float yPos = 0f; int count = 0; float leftMargin = e.MarginBounds.Left; float topMargin = e.MarginBounds.Top; string line = null; float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics); while (count < linesPerPage) { line = fileToPrint.ReadLine(); if (line == null) { break; } yPos = topMargin + count * printFont.GetHeight(e.Graphics); e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } if (line != null) { e.HasMorePages = true; } }
编译代码
本示例需要:
- 名为 printButton 且包含 Click 事件处理程序的按钮控件。
- 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
此示例还假定在您的桌面上有一个名为 myFile.txt 的文本文件。该示例代码替换现有的事件处理程序。
可靠编程
文件操作应被包括在适当的 try...catch...finally 块内。使用完它后,请始终调用 StreamReader.Close。
以下情况可能会导致异常:
- 您没有访问该打印机的权限。
- 您没有访问文件系统的权限。
- 没有安装打印机。
- 未安装 Arial 字体。
安全性
为了运行此示例,您必须具有访问文件系统和打印机的权限。
此示例说明如何打印当前窗体的副本。
示例
[System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop); private Bitmap memoryImage; private void CaptureScreen() { Graphics mygraphics = this.CreateGraphics(); Size s = this.Size; memoryImage = new Bitmap(s.Width, s.Height, mygraphics); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 = mygraphics.GetHdc(); IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376); mygraphics.ReleaseHdc(dc1); memoryGraphics.ReleaseHdc(dc2); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImage(memoryImage, 0, 0); } private void printButton_Click(System.Object sender, System.EventArgs e) { CaptureScreen(); printDocument1.Print(); }
编译代码
本示例需要:
- 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
- 名为 printButton 且包含 Click 事件处理程序的 Button 对象。
该示例代码替换现有的事件处理程序。单击 printButton
时,就会打印该窗体。
可靠编程
以下情况可能会导致异常:
- 您没有访问该打印机的权限。
- 您没有使用非托管代码的权限。
- 没有安装打印机。
安全性
为了运行此示例,您必须具有执行非托管代码和访问打印机的权限。
此示例说明如何更改已有 Pen 对象的颜色。
示例
myPen.Color = System.Drawing.Color.PeachPuff;
编译代码
本示例需要:
- 名为 myPen 的 Pen 对象。
可靠编程
您应该始终对使用系统资源的任何对象(如 Pen 对象)调用 Dispose。