简体   繁体   English

捕获Windows窗体图形到图像C#的问题

[英]Issue capturing windows form drawing to image C#

I'm trying to draw a few objects to a windows form, then create a image of that form and save it to a folder. 我试图将一些对象绘制到Windows窗体,然后创建该窗体的图像并将其保存到文件夹。

This is a two part question... Why isn't the image that is being drawn to the windows form showing up on the created image? 这是一个分为两个部分的问题...为什么绘制到Windows窗体上的图像没有显示在创建的图像上? Also second question is, how can I create an image of the windows form drawing, from say point 0,0 to point 500,500 without a background.... 另外一个第二个问题是,如何在没有背景的情况下 ,从点0,0到点500,500,创建Windows窗体绘图的图像。

Here is the code I have at the moment.... 这是我目前的代码。

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 { }

Can you see anything wrong here? 您在这里看到任何错误吗? What seems to be prohibiting the drawings to be saved to a .bmp file? 似乎是什么禁止将图形保存到.bmp文件?

Thank in advance! 预先感谢!

I ended up using the example shown here Saving System.Drawing.Graphics to a png or bmp 我最终使用了此处显示的示例,将System.Drawing.Graphics保存为png或bmp

        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 { }

And that has worked flawlessly for me now :D 那对我来说已经完美无缺了:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM