Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用PrintDialog使用BufferedGraphics打印_C#_Winforms - Fatal编程技术网

C# 使用PrintDialog使用BufferedGraphics打印

C# 使用PrintDialog使用BufferedGraphics打印,c#,winforms,C#,Winforms,我有一个名为backbufferGraphics的BufferedGraphics,我想使用PrintDialog打印缓冲区中的内容,这是我的代码,但不起作用: private print() { printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage); printDialog1.Document = printD

我有一个名为backbufferGraphics的BufferedGraphics,我想使用PrintDialog打印缓冲区中的内容,这是我的代码,但不起作用:

private print()
{
        printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
        printDialog1.Document = printDocument1;
        printDialog1.AllowSelection = true;
        printDialog1.AllowSomePages = true;
        printDialog1.ShowDialog();
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    DrawToPrint(e.Graphics);
}
private void DrawToPrint(Graphics _Gd)
{
    backbufferGraphics.Render(_Gd);
}

谢谢

您必须先在图像中设置缓冲区图形,然后再打印图像:

Bitmap bmp = new Bitmap();

int X = e.X - bmp.Width/2;
int Y = e.Y - bmp.Height/2;
// créer et initialiser le BufferedGraphics
BufferedGraphics bg =
BufferedGraphicsManager.Current.Allocate(this.CreateGraphics(),
ClientRectangle);
Graphics g = bg.Graphics;

g.Clear(SystemColors.Control);
g.DrawImage(bmp, new Point(X, Y));

bg.Render();
g.Dispose(); 
bg.Dispose();
//save bmp as file