如何将面板上的图像保存到图像文件?

时间:2014-01-07 08:59:20

标签: image save

这个程序应该像加入点一样工作,用户选择要显示的点数并且能够在画布上绘制,问题在于保存,每当我尝试保存它根本不工作时,它给出我是一个黑色的矩形..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Painting2
{
    public partial class Form1 : Form
    {
        Bitmap drawing;
        bool draw = false;
        Color C = Color.Black;
        int size = 14;
        bool startdraw = false;

        public Form1(string parameter1)
        {
            InitializeComponent();
            this.Text = parameter1;

        }

        private void Form1_Load(object sender, EventArgs e)
        {


            drawing = new Bitmap (pnlcanvas.Width, pnlcanvas.Height, pnlcanvas.CreateGraphics());

            cbdots.Items.Add("2");
            cbdots.Items.Add("3");
            cbdots.Items.Add("4");
            cbdots.Items.Add("5");
            cbdots.Items.Add("6");
            cbdots.Items.Add("7");
            cbdots.Items.Add("8");
            cbdots.Items.Add("9");
            cbdots.Items.Add("10");
            cbdots.Items.Add("11");
            cbdots.Items.Add("12");
            cbdots.Items.Add("13");
            cbdots.Items.Add("14");
            cbdots.Items.Add("15");

        }

        private void pnlcanvas_MouseDown(object sender, MouseEventArgs e)
        {
            draw = true;
            Graphics g = Graphics.FromImage(drawing);
            SolidBrush b = new SolidBrush(C);

            if (startdraw == true)
            {
                g.FillEllipse(b, e.X, e.Y, size, size);


                pnlcanvas.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
            }
        }

        private void pnlcanvas_MouseUp(object sender, MouseEventArgs e)
        {
            draw = false;
        }

        private void pnlcanvas_Move(object sender, EventArgs e)
        {

        }

        private void pnlcanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (draw == true && startdraw == true)
            {

                Graphics g = Graphics.FromImage(drawing);
                SolidBrush b = new SolidBrush(C);


                g.FillEllipse(b, e.X, e.Y, size, size);



                    pnlcanvas.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
            }

        }

        private void btncolor_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            C = Color.White;
            size = 25;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            drawing = new Bitmap(pnlcanvas.Width, pnlcanvas.Height, pnlcanvas.CreateGraphics());
            pnlcanvas.CreateGraphics().Clear(Color.White);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        { 
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            //if (saveDialog.ShowDialog() == DialogResult.OK)
            //{
            //    Bitmap tosave = new Bitmap(pnlcanvas.Width, pnlcanvas.Height);
            //    pnlcanvas.DrawToBitmap(tosave, new Rectangle(0, 0, pnlcanvas.Width, pnlcanvas.Height));
            //    tosave.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            //}



            using (Bitmap bitmap = new Bitmap(pnlcanvas.ClientSize.Width,
                                  pnlcanvas.ClientSize.Height))
            {
                pnlcanvas.DrawToBitmap(bitmap, pnlcanvas.ClientRectangle);
                bitmap.Save(@"C:\Users\xejns_000\Desktop\TestDrawToBitmap.bmp", ImageFormat.Bmp);
            }

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btnstart_Click(object sender, EventArgs e)

        {
            DialogResult result = cd.ShowDialog();
            if (result == DialogResult.OK)
            {
                C = cd.Color;
            }
            startdraw = true;

        }

        private void cbdots_SelectedIndexChanged(object sender, EventArgs e)
        {
            drawing = new Bitmap(pnlcanvas.Width, pnlcanvas.Height, pnlcanvas.CreateGraphics()); //clearing
            pnlcanvas.CreateGraphics().Clear(Color.White); //clearing
            Random r = new Random();
            for (int i = 0; i <= Convert.ToInt32(cbdots.SelectedItem.ToString()) -1; i++)
            {
                Color RandC = Color.Black; ;
                int randomcolor = r.Next(1, 11);
                int randomx = r.Next(10, 730);
                int randomy = r.Next(10, 390);

                switch (randomcolor) // choosing random color
                {
                    case 1:
                        RandC = Color.PaleVioletRed;
                        break;
                    case 2:
                        RandC = Color.PowderBlue;
                        break;
                    case 3:
                        RandC = Color.Fuchsia;
                        break;
                    case 4:
                        RandC = Color.DarkGoldenrod;
                        break;
                    case 5:
                        RandC = Color.LavenderBlush;
                        break;
                    case 6:
                        RandC = Color.Tomato;
                        break;
                    case 7:
                        RandC = Color.SpringGreen;
                        break;
                    case 8:
                        RandC = Color.Wheat;
                        break;
                    case 9:
                        RandC = Color.Salmon;
                        break;
                    case 10:
                        RandC = Color.LightSteelBlue;
                        break;

                }

                Graphics g = Graphics.FromImage(drawing); // painting dots
                SolidBrush b = new SolidBrush(RandC); // painting dots


                g.FillEllipse(b, randomx, randomy, 40, 40); // painting dots



                pnlcanvas.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));

            }
        }
    }
}

0 个答案:

没有答案