在paint app c中保存图像

时间:2011-05-20 09:02:34

标签: c# winforms

我正在制作一个简单的绘画应用程序。除了保存,我得到了一切工作。我正在进行面板内的所有油漆操作。我需要将其保存为图像。怎么做?

4 个答案:

答案 0 :(得分:8)

使用此代码

Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);//to create bmp of same size as panel
Rectangle rect=new Rectangle(0,0,panel1.Width,panel1.Height); //to set bounds to image
panel1.DrawToBitmap(bmp,rect);      // drawing panel1 imgae into bmp of bounds of rect
bmp.Save("C:\\a.png", System.Drawing.Imaging.ImageFormat.Png); //save location and type

答案 1 :(得分:2)

以下是绘画应用的一个很好的例子: 1)WPF 2)WinForms

答案 2 :(得分:1)

这样的事情:

public void SaveAs()
    {
        SaveFileDialog diag = new SaveFileDialog();
        DialogResult dr = diag.ShowDialog();

        if (dr.Equals(DialogResult.OK))
        {

            string _filename = diag.FileName;

            // filename not specified. Use FileName = ...
            if (_filename == null || _filename.Length == 0)
                throw new Exception("Unspecified file name");

            // cannot override RO file
            if (File.Exists(_filename)
                && (File.GetAttributes(_filename)
                & FileAttributes.ReadOnly) != 0)
                throw new Exception("File exists and is read-only!");

            // check supported image formats
            ImageFormat format = FormatFromExtension(_filename);
            if (format == null)
                throw new Exception("Unsupported image format");

            // JPG images get special treatement
            if (format.Equals(ImageFormat.Jpeg))
            {
                EncoderParameters oParams = new EncoderParameters(1);
                oParams.Param[0] = new EncoderParameter(
                    System.Drawing.Imaging.Encoder.Quality, 100L);
                ImageCodecInfo oCodecInfo = GetEncoderInfo("image/jpeg");
                yourImage.Save(_filename, oCodecInfo, oParams);
            }
            else
                yourImage.Save(_filename, format);

        }
    }

答案 3 :(得分:1)

如果你正在使用wpf,你可以在RenderTargetBitmap处理。它可以将任何视觉渲染成位图,然后使用@danyogiaxs awnser

进行保存

-edit -

我还发现this SO post 在winforms上做同样的事情