Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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# 将面板另存为图像_C#_Drawing - Fatal编程技术网

C# 将面板另存为图像

C# 将面板另存为图像,c#,drawing,C#,Drawing,我在涂油漆。这有点简单。它由一个面板组成,我将在其中绘制,然后最后我将另存为JPG、BMP或PNG文件 我的应用程序工作得很好,但我面临的问题是,当我保存输出时,它不是画在面板上的东西,它的黑色图像什么都不是,只是黑色 我所有的工作都保存为 Thepic = new Bitmap(panel1.ClientRectangle.Width, this.ClientRectangle.Height); 在鼠标上(向下,向上)我有 它正常保存了工作,但结果是黑色图像,而不是面板包含的图像。我认为问题

我在涂油漆。这有点简单。它由一个面板组成,我将在其中绘制,然后最后我将另存为JPG、BMP或PNG文件

我的应用程序工作得很好,但我面临的问题是,当我保存输出时,它不是画在面板上的东西,它的黑色图像什么都不是,只是黑色

我所有的工作都保存为

Thepic = new Bitmap(panel1.ClientRectangle.Width, this.ClientRectangle.Height);
在鼠标上(向下,向上)我有


它正常保存了工作,但结果是黑色图像,而不是面板包含的图像。

我认为问题可能是您使用的是“克隆”方法

试试“DrawToBitmap”——这在过去对我很有用

下面是一个从名为“plotPrinter”的控件保存位图的示例:


你可以试试这个,这个对我有用

我用记忆流

MemoryStream ms = new MemoryStream();
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //you could ave in BPM, PNG  etc format.
byte[] Pic_arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(Pic_arr, 0, Pic_arr.Length);
ms.Close();

您的过滤器中有“pgn”而不是“png”。。。。我会自己更正这篇文章,但是如果这是从你的真实来源粘贴的,你可能想知道它……这没关系,但我还是很困惑为什么输出仍然是黑色的图像这对没有子控件的控件很有效,但是当控件有子控件时,
DrawToBitmap
以“z”的相反顺序绘制子控件(意思是在真正的前控件前面绘制控件后面)。找不到drawtobitmap方法
        int width = plotPrinter.Size.Width;
        int height = plotPrinter.Size.Height;

        Bitmap bm = new Bitmap(width, height);
        plotPrinter.DrawToBitmap(bm, new Rectangle(0, 0, width, height));

        bm.Save(@"D:\TestDrawToBitmap.bmp", ImageFormat.Bmp);
        Be aware of saving directly to the C directly as this is not 
        permitted with newer versions of window, try using SaveFileDialog.
    SaveFileDialog sf = new SaveFileDialog();
    sf.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
    sf.ShowDialog();
    var path = sf.FileName; 
MemoryStream ms = new MemoryStream();
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //you could ave in BPM, PNG  etc format.
byte[] Pic_arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(Pic_arr, 0, Pic_arr.Length);
ms.Close();