protected override void Dispose(bool disposing)
        {
            if (myOptimizedTexture != null)
            {
                myOptimizedTexture.Dispose();                 // first stop this one because it does the updating
            }
            myOptimizedTexture = null;

            if (myRenderWindow != null)
            {
                myRenderWindow.Kill();
            }
            myRenderWindow = null;

            if (myAdjustMetricForm != null)
            {
                myAdjustMetricForm.Dispose();
            }
            myAdjustMetricForm = null;

            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        private void openProject(object sender, System.EventArgs e)
        {
            DialogResult r = MessageBox.Show(this, "Discard the current project and all editing done?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

            if (r == DialogResult.OK)
            {
                using (OpenFileDialog fd = new OpenFileDialog())
                {
                    fd.Filter = "Optimization Project Files|*.bilevelopt";
                    DialogResult dr = fd.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        try
                        {
                            IFormatter       formatter           = new BinaryFormatter();
                            Stream           stream              = new FileStream(fd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                            OptimizedTexture newOptimizedTexture = (OptimizedTexture)formatter.Deserialize(stream);
                            stream.Close();
                            myOptimizedTexture.Dispose();
                            myOptimizedTexture = newOptimizedTexture;
                            myOptimizedTexture.SetEditor(this, myAdjustMetricForm, myRenderWindow);
                            AutoScrollMinSize = myOptimizedTexture.GetSize(getZoom());
                            Dirty();
                            theFile = fd.FileName;
                            Text    = theFile;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, "Error on loading:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
        private void loadBitmap(object sender, System.EventArgs e)
        {
            DialogResult r = MessageBox.Show(this, "Discard the current bitmap and all editing done?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

            if (r == DialogResult.OK)
            {
                using (OpenFileDialog fd = new OpenFileDialog())
                {
                    fd.Filter = "Bitmap Files|*.bmp|JPEG Files|*.jpg|PNG Files|*.png";
                    DialogResult dr = fd.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        try
                        {
                            OptimizedTexture newOptimizedTexture = new OptimizedTexture(fd.FileName, getDownsampling());
                            if (!RenderWindow.IsPowerOfTwo(newOptimizedTexture.GetSize(1).Width) || !RenderWindow.IsPowerOfTwo(newOptimizedTexture.GetSize(1).Height))
                            {
                                throw new Exception("Width and height of bitmap must be powers of 2.");
                            }
                            myOptimizedTexture.Dispose();
                            myOptimizedTexture = newOptimizedTexture;
                            myOptimizedTexture.SetEditor(this, myAdjustMetricForm, myRenderWindow);
                            AutoScrollMinSize = myOptimizedTexture.GetSize(getZoom());
                            Dirty();
                            theFile = null;
                            Text    = "untitled";
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, "Error on loading:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
        public ContourEditor()
        {
            InitializeComponent();
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

            Assembly assem = Assembly.GetAssembly(typeof(ContourEditor));
            Stream   st    = assem.GetManifestResourceStream("BilievelTextureMagnification.Curvy.jpg");

            myOptimizedTexture = new OptimizedTexture(st, getDownsampling());
            st.Close();
            st = null;

            myAdjustMetricForm = new AdjustMetricForm();
            myRenderWindow     = RenderWindow.Create();
            myOptimizedTexture.SetEditor(this, myAdjustMetricForm, myRenderWindow);
            AutoScrollMinSize = myOptimizedTexture.GetSize(getZoom());
            Dirty();
        }