C# 将windows窗体图形捕获到图像C时出现问题

C# 将windows窗体图形捕获到图像C时出现问题,c#,image,save,drawing,C#,Image,Save,Drawing,我试图在windows窗体中绘制一些对象,然后创建该窗体的图像并将其保存到文件夹中 这是一个由两部分组成的问题。。。为什么正在绘制到windows窗体的图像没有显示在创建的图像上?第二个问题是,如何创建windows窗体图形的图像,例如从点0,0到点500500,没有背景 这是我目前掌握的代码 Form draw = new Drawing(); // Opens the drawing form draw.Show(); try {

我试图在windows窗体中绘制一些对象,然后创建该窗体的图像并将其保存到文件夹中

这是一个由两部分组成的问题。。。为什么正在绘制到windows窗体的图像没有显示在创建的图像上?第二个问题是,如何创建windows窗体图形的图像,例如从点0,0到点500500,没有背景

这是我目前掌握的代码

Form draw = new Drawing();      // Opens the drawing form
draw.Show();

        try
        {
            //Try to draw something...
            System.Drawing.Pen myPen;       
            myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
            System.Drawing.Graphics formGraphics = draw.CreateGraphics();
            formGraphics.DrawLine(myPen, 0, 0, 500, 500);
            myPen.Dispose();
            formGraphics.Dispose();

            var path = this.outputFolder.Text;      // Create variable with output path

            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);  // Create path if it doesn't exist
            }

            using (var bitmap = new Bitmap(draw.Width, draw.Height))  // Creating the .bmp file from windows form
            {
                draw.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
                bitmap.Save(path + "\\" + i + ".bmp");
             }
        }
        catch { }
你能看出这里有什么不对劲吗?什么似乎禁止将图形保存到.bmp文件


提前感谢

我最后使用了这里显示的示例


这对我来说已经完美无瑕了:D

你的位图是表单的大小,但与表单无关;也不确定第一部分与任何事情有什么关系;CreateGraphics几乎总是用错误的方式将某物绘制到窗体上。你是什么意思,第一部分与任何东西都无关。。。你能详细说明一下吗?你建议我如何把东西画到窗体上,记住它以后需要保存为图像;表单不是一张画布,它将保留在其他地方绘制的内容。如果你不想要背景,那是不是意味着表单背景色?然后设置use MakeTransparent以将其设置为这样-任何具有该颜色的控件在图像中也将是白色/无颜色的。你可能还需要另存为PNG来保存透明度OK,比如说,我真的不需要将图像绘制到表单上。。。有没有一种方法可以使用类似于CreateGraphics的控件直接绘制到.bmp或.png?我在这里使用的示例似乎很有效!
        Form draw = new Drawing();      // Opens the drawing form
        draw.Show();
        try
        {
            var path = this.outputFolder.Text; // Path where images will be saved to

            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);     // Create a directory if it does not already exist
            }

            Bitmap bitmap = new Bitmap(Convert.ToInt32(1024), Convert.ToInt32(1024), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bitmap);

            System.Drawing.Pen myPen;
            myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
            g.DrawLine(myPen, 0, 0, 1024, 1024);

            bitmap.Save(path + "\\" + i + ".png", ImageFormat.Png);

        }
        catch { }