【问题标题】:saving my graphic drawing panel as png or jpg image format将我的图形绘图面板保存为 png 或 jpg 图像格式
【发布时间】:2015-11-09 17:12:24
【问题描述】:

我制作了一个图形绘图面板 [我自己的绘图程序],我想保存我自己的绘图。这是我所有的代码:

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

namespace pezeshk
{
    public partial class pen : Form
    {
        private SolidBrush myBrush;
        private Graphics myGraphics;
        private bool isDrawing = false;
        public pen()
        {
             InitializeComponent();
        }

        private void pen_Load(object sender, EventArgs e)
        {
            myBrush = new SolidBrush(panel2.BackColor);
            myGraphics = panel1.CreateGraphics();

        }

        private void panel2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                panel2.BackColor = colorDialog1.Color;
                myBrush.Color = panel2.BackColor;  
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing == true)
            {
                myGraphics.FillEllipse(myBrush, e.X, e.Y, trackBar1.Value, trackBar1.Value);
            }
        }


}

【问题讨论】:

    标签: c# panel graphic


    【解决方案1】:

    适用于所有需要此代码的人 此代码有效[感谢@Taw]

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace pezeshk
    {
        public partial class pen : Form
        {
            private SolidBrush myBrush;
            private Pen myPen;
            private Graphics myGraphics;
            private bool isDrawing = false;
            public pen()
            {
                InitializeComponent();
                myPen = new Pen(Color.ForestGreen);
                myBrush = new SolidBrush(Color.DarkSlateBlue);
            }
            void panel1_paint(object sender, PaintEventArgs e)
            {
                if (rb_pen.Checked)
                {
                    if (points.Count > 1) e.Graphics.DrawCurve(myPen, points.ToArray());
                    foreach (List<Point> lp in curves)
                        if (lp.Count > 1)
                            e.Graphics.DrawCurve(myPen, lp.ToArray());
    
    
                }
            }
            private void pen_Load(object sender, EventArgs e)
            {
                myBrush = new SolidBrush(panel2.BackColor);
                myGraphics = panel1.CreateGraphics();
    
    
            }
    
            private void panel2_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    panel2.BackColor = colorDialog1.Color;
                    myBrush.Color = panel2.BackColor;
                }
            }
            Point mDown = Point.Empty;
            private void panel1_MouseDown(object sender, MouseEventArgs e)
            {
                mDown = e.Location;
            }
    
            private void panel1_MouseUp(object sender, MouseEventArgs e)
            {
                if (rb_pen.Checked)
                {
                    if (points.Count > 1) curves.Add(points.ToList());  // copy!!
                    points.Clear();
                    panel1.Invalidate();
                }
    
    
                panel1.Invalidate();
            }
            private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
    
                    if (rb_pen.Checked)
                    {
                        points.Add(e.Location);
    
                    }
                    panel1.Invalidate();
                }
            }
    
            List<Rectangle> circles = new List<Rectangle>();
            List<Point> points = new List<Point>();
            List<List<Point>> curves = new List<List<Point>>();
            private void button1_Click(object sender, EventArgs e)
            {
                string somefolder = "D:\\"; using (Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height))
                {
                    panel1.DrawToBitmap(bmp, panel1.ClientRectangle); bmp.Save(somefolder + panel1.Name + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                } 
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                curves.Clear(); points.Clear(); panel1.Invalidate();
            }
    
        }
        class DrawPanel : Panel
        {
            public DrawPanel() { DoubleBuffered = true; }
        }
    
    }
    

    【讨论】:

    • 警告:该代码包含一个多余且不合逻辑的 CreateGraphics!
    • @TaW 我需要解决这个程序的一个问题,我如何放置一个按钮,当我点击按钮时,鼠标 [pen] 会在绘图面板的第一行从第一行写一些东西这么容易!!!希望你能理解
    猜你喜欢
    • 1970-01-01
    • 2011-03-07
    • 2020-12-16
    • 2011-07-26
    • 1970-01-01
    • 2023-03-05
    • 2020-09-14
    • 2017-03-01
    • 1970-01-01
    相关资源
    最近更新 更多