Help guys,

I am trying to make a paint program similar to Paint.net.

the problem im having is like i paint on the picturebox but when i click the minimize button and maximize it, the lines i drew disappear. here is my code:

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;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;


namespace FireFrame
{
    


    public partial class Form1 : Form
    {
        

        class hwndAPI
        {
            [DllImport("user32")]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        }

      
         
        
        enum myTools
        {

            eLine, eFreeHand, eRectangle, eElipse,
            eSlinky, eSelectionRectangle, eABC, eErarser, eBrush, eTextureBrush
        };

        myTools toolselect;

        public struct Points
        {
            public int startX;

            public int startY;

            public int endX;

            public int endY;




            public Points(int startx, int starty, int endx, int endy)
            {

                startX = startx;

                startY = starty;

                endX = endx;
                endY = endy;

            }
        }
        

           int county = 0;

        List<Points> points = new List<Points>();

        
        public static Pen myPen = new Pen(Color.Black, 2);
        int startX;
        int startY;
        int endX;
        int endY;


        bool down = false;

        private Graphics graphObj;
        private void paintCurrentPosition(Point loc, int thickness, Color colorPen)
        {
            /** this method is responsible for creating a single spot on the current position of the cursor.
            * The cursor position as indicated
            * by Cursor.Position is not accurately placed at the tip, so an offset is being employed to make sure that the spot appears at the tip of
            * the cursor*/
            SolidBrush brush = new SolidBrush(colorPen);
            graphObj = pictureBox1.CreateGraphics();
            graphObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphObj.DrawLine(new Pen(brush, thickness), _lastLoc, loc);
            _lastLoc = loc;
        }
        private Bitmap buffer;
        private Point _lastLoc;
        public Form1()
        {

            InitializeComponent();

        

        }

        private void splitContainer3_Panel2_Resize(object sender, EventArgs e)
        {
            // Resize the buffer, if it is growing
            if (buffer == null ||
                buffer.Width < splitContainer3.Panel2.Width ||
                buffer.Height < splitContainer3.Panel2.Height)
            {
                Bitmap newBuffer = new Bitmap(splitContainer3.Panel2.Width, splitContainer3.Panel2.Height);
                if (buffer != null)
                    using (Graphics bufferGrph = Graphics.FromImage(newBuffer))
                        bufferGrph.DrawImageUnscaled(buffer, Point.Empty);
                buffer = newBuffer;
            }
        }



        private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog o = new OpenFileDialog();
                o.Title = "Open File";
                if (o.ShowDialog() == DialogResult.OK)
                {

                    pictureBox1.Image = new Bitmap(o.FileName);
                    pictureBox1.Enabled = true;

                }
            }


            catch (Exception)
            {
                throw new ApplicationException("Failed loading image");
            }
        }

        
        private void clearImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
 

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            _lastLoc = e.Location;
            startX = e.X;
            startY = e.Y;
           
            down = true;

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

            down = false;

        }




        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            
            Graphics g = pictureBox1.CreateGraphics();
            if (down == true)
            {
                switch (toolselect)
                {
                    case myTools.eFreeHand:
                        paintCurrentPosition(e.Location, 2, Color.Black);
                        break;
                    case myTools.eLine:
                        g.DrawLine(myPen, startX, startY, endX, endY);
                        endX = e.X;
                        endY = e.Y;
                        break;

                }
            }
        }

       

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Invalidate();
           // imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;

       // pictureBox1.Image = imageList1.Images[county];

        }

       
      

        private void button1_Click_1(object sender, EventArgs e)
        {
            toolselect = myTools.eFreeHand;
           // county += 1;

 //           if (county + 1 >imageList1.Images.Count)
   //         {
     //           county = 0;
       //     }

         //   pictureBox1.Image = imageList1.Images[county];
            //imageList1.ImageSize = new System.Drawing.Size(256, 256);
            //imageList1.Images.Add(pictureBox1.Image);
            //for (int j = 0; j < this.imageList1.Images.Count; j++)
            //{

              //  ListViewItem item = new ListViewItem();

                //item.ImageIndex = j;

                //this.listView1.Items.Add(item);

            //}
            
           
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            Point p = new Point(10,10);
           
        }

    
    }
}

Also i need help with the resize of the picture box like in paint.net because if i draw on the picturebox then resize the form the picture box removes the draw objects.

eg. i draw a line diagonally from the top left to bottom right, if i resize form (assuming i have picture box docked as fill) to about half the size diagonally, then half of the dianal line i drew would be gone, even if i try restoring the window to the previous size.

thank you

hope you can help me

When you resize (minimizing and restoring is a form of resizing) you'll have to redraw all the lines as they aren't part of the form. One way to handle this is to store the graphics object you are using to draw on and restore it when the draw event occurs.

Thank you, this fixes the problem, but i have another one with the Graphicspath i can't get a circular shape(brush) i used addline, also when you increase the size it starts acting all wierd and you dont get a smooth line, help someone?

i use this in the mouse move

mousePath.AddLine(e.X, e.Y, e.X, e.Y);

and this in paint

e.Graphics.DrawPath(p, mousePath);

thank you

A paint program normally uses something what is called vectorgraphics. It is like Truefonts, when you resize them they keep their shape as opposite to "normal" fonts which start to look weird. Sorry I can't help you on this any further, I have no experience in this matter.
You could also switch to WPF, this may be a good starting point on drawing: http://msdn.microsoft.com/en-us/library/ms747393.aspx

Uhm if i switch to WPF would i be able to make an Animation program ( something similar to pivot)?

I myself I'm trying right now, to get a grasp on WPF and for what I allready know of it, you can even do 3D animation in it. But I also know that it is not something you do in a weekend or so...:(

Edited 5 Years Ago by ddanbe: n/a

This article has been dead for over six months. Start a new discussion instead.