[英]Issue capturing windows form drawing to image C#
我试图将一些对象绘制到Windows窗体,然后创建该窗体的图像并将其保存到文件夹。
这是一个分为两个部分的问题...为什么绘制到Windows窗体上的图像没有显示在创建的图像上? 另外一个第二个问题是,如何在没有背景的情况下 ,从点0,0到点500,500,创建Windows窗体绘图的图像。
这是我目前的代码。
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文件?
预先感谢!
我最终使用了此处显示的示例,将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 { }
那对我来说已经完美无缺了:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.