C# 将面板另存为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1836338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:16:43  来源:igfitidea点击:

Saving Panel as an Image

c#drawing

提问by Tony

I'm doing this paint application. It's kind of simple. It consist of a panel where I will draw on and then finally I will save as JPG or BMP or PNG file.

我正在做这个油漆应用。这有点简单。它由一个面板组成,我将在其中绘图,然后最后将保存为 JPG 或 BMP 或 PNG 文件。

My application work perfectly but the problem I'm facing is that when I'm saving the output is not what drawn on the panel its black Image nothing just black.

我的应用程序运行良好,但我面临的问题是,当我保存输出时,输出不是在面板上绘制的,它的黑色图像没有黑色。

all my work is been saved as

我所有的工作都被保存为

Thepic = new Bitmap(panel1.ClientRectangle.Width, this.ClientRectangle.Height);

and on the mouse (down,up thing) I have

和鼠标(向下,向上的东西)我有

snapshot = (Bitmap)tempDraw.Clone();

and it saved the work normally but again the rsult is black Image not what the panel contain.

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

采纳答案by Tom Bushell

I think the problem may be that you're using the "Clone" method.

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

Try "DrawToBitmap" - that's worked for me in the past.

尝试“ DrawToBitmap” - 过去对我有用

Here's a sample that saves a bitmap from a control called "plotPrinter":

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

        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.
        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; 

回答by Leinad

You could try this, it work for me, instead I sed MemoryStream.

你可以试试这个,它对我有用,而不是我 sed MemoryStream。

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();