private void AfxRgbaLutVoronoiGenerator_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (null != colorLutTools)
     {
         colorLutTools.Dispose();
         colorLutTools = null;
     }
 }
        private void buttonGenerateLut_Click(object sender, EventArgs e)
        {
            if (!GetColors())
            {
                return;
            }

            UpdateAxis();

            suspendPreview = true;

            comboX.SelectedIndex   = 0;
            comboY.SelectedIndex   = 1;
            trackZ.Value           = 0;
            trackW.Value           = 0;
            drawMode.SelectedIndex = 0;

            if (preview.Image != null)
            {
                preview.Image.Dispose();
                preview.Image = null;
            }
            if (previewBitmap != null)
            {
                preview.Image = null;
                previewBitmap.Dispose();
            }
            previewBitmap = new Bitmap((int)resR.Value, (int)resG.Value);
            preview.Image = new Bitmap(preview.Width, preview.Height);

            if (null == colorLutTools)
            {
                colorLutTools = new AfxCppCli.ColorLutTools();
            }

            if (!colorLutTools.New(
                    (uint)resR.Value,
                    (uint)resG.Value,
                    (uint)resB.Value,
                    (uint)resA.Value
                    ))
            {
                MessageBox.Show(this,
                                L10n._("Error when creating / re-sizing color map."),
                                L10n._("Error"),
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                colorLutTools.Dispose();
                colorLutTools = null;
            }

            if (null != colorLutTools && !colorLutTools.IteratePut(Iterate))
            {
                MessageBox.Show(this,
                                L10n._("Iteration aborted."),
                                L10n._("Error"),
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                colorLutTools.Dispose();
                colorLutTools = null;
            }

            trackZ.Value = 255;
            trackW.Value = 255;

            suspendPreview = false;

            UpdatePreview();

            if (null != colorLutTools && colorLutTools.IsValid())
            {
                SaveToFile();
            }
        }