Пример #1
0
        private void CreateOptionImagesButton_Click(object sender, EventArgs e)
        {
            if (outputFileTextBox.Text == "")
            {
                // The filename for the output file has not been set
                MessageBox.Show("Please select an output file name before creating exhibit.",
                                "No filename",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            else if (option1TextBox.Text == "" && option2TextBox.Text == "")
            {
                // no text has been added for the exhibit
                MessageBox.Show("Please add code text before creating Option Images.",
                                "No Code",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            else
            {
                string outputFileName = outputFileTextBox.Text;
                Font   font           = new Font("Courier New", Globals.FontHeightSizePX, GraphicsUnit.Pixel);
                Color  textColor      = Color.FromName("Black");

                string option1Text = option1TextBox.Text;
                string option2Text = option2TextBox.Text;
                string option3Text = option3TextBox.Text;
                string option4Text = option4TextBox.Text;
                string option5Text = option5TextBox.Text;

                var option1Code = new CodeText();
                var option2Code = new CodeText();
                var option3Code = new CodeText();
                var option4Code = new CodeText();
                var option5Code = new CodeText();

                option1Code.OriginalText = option1Text + " ";
                option1Code.TargetText   = "";
                option2Code.OriginalText = option2Text + " ";
                option2Code.TargetText   = "";
                option3Code.OriginalText = option3Text + " ";
                option3Code.TargetText   = "";
                option4Code.OriginalText = option4Text + " ";
                option4Code.TargetText   = "";
                option5Code.OriginalText = option5Text + " ";
                option5Code.TargetText   = "";

                int[] optionWidthArray    = { option1Code.MaxLineLength, option2Code.MaxLineLength, option3Code.MaxLineLength, option4Code.MaxLineLength, option5Code.MaxLineLength };
                int   maxOptionWidth      = optionWidthArray.Max();
                int   maxOptionWidthIndex = optionWidthArray.ToList().IndexOf(maxOptionWidth) + 1;

                int[] optionLinesArray    = { option1Code.NumberOfLines, option2Code.NumberOfLines, option3Code.NumberOfLines, option4Code.NumberOfLines, option5Code.NumberOfLines };
                int   maxOptionLines      = optionLinesArray.Max();
                int   maxOptionLinesIndex = optionLinesArray.ToList().IndexOf(maxOptionLines) + 1;

                int optionWidthPixels  = (int)(Math.Ceiling((maxOptionWidth * Globals.FontWidthSizePX)) + (2 * Globals.BorderPaddingPX));
                int optionHeightPixels = (maxOptionLines * (Globals.FontHeightSizePX + Globals.LineSpacingPX)) + (2 * Globals.BorderPaddingPX);
                if (optionWidthPixels > Globals.MaxOptionWidthPX)
                {
                    MessageBox.Show(String.Format("Option text is too wide to fit in image < {0} pixels", Globals.MaxOptionWidthPX));
                }
                else if (optionHeightPixels > Globals.MaxOptionHeightPX)
                {
                    MessageBox.Show(String.Format("Option text has too many lines to fit in image < {0} pixels", Globals.MaxOptionHeightPX));
                }
                else
                {
                    if (option1Text.Length > 0)
                    {
                        string option1OutputFileName = outputFileName.Replace(".png", "_Option_A.png");
                        DrawText(option1Text, font, textColor, optionWidthPixels, optionHeightPixels, option1OutputFileName);
                    }
                    if (option2Text.Length > 0)
                    {
                        string option2OutputFileName = outputFileName.Replace(".png", "_Option_B.png");
                        DrawText(option2Text, font, textColor, optionWidthPixels, optionHeightPixels, option2OutputFileName);
                    }
                    if (option3Text.Length > 0)
                    {
                        string option3OutputFileName = outputFileName.Replace(".png", "_Option_C.png");
                        DrawText(option3Text, font, textColor, optionWidthPixels, optionHeightPixels, option3OutputFileName);
                    }
                    if (option4Text.Length > 0)
                    {
                        string option4OutputFileName = outputFileName.Replace(".png", "_Option_D.png");
                        DrawText(option4Text, font, textColor, optionWidthPixels, optionHeightPixels, option4OutputFileName);
                    }
                    if (option5Text.Length > 0)
                    {
                        string option5OutputFileName = outputFileName.Replace(".png", "_Option_E.png");
                        DrawText(option5Text, font, textColor, optionWidthPixels, optionHeightPixels, option5OutputFileName);
                    }
                    string stemOutputFileName = outputFileName.Replace(".png", "_Option_X.png");
                    MessageBox.Show(String.Format("Option images created and saved with filename format {0} (where X denotes the option)", stemOutputFileName));
                }
            }
        }
Пример #2
0
        private void createDnDImageButton_Click(object sender, EventArgs e)
        {
            // Create DnD Image button pressed

            if (outputFileTextBox.Text == "")
            {
                // The filename for the output file has not been set
                MessageBox.Show("Please select an output file name before creating exhibit.",
                                "No filename",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            else if (inputDnDCodeTextBox.Text == "")
            {
                // no text has been added for the exhibit
                MessageBox.Show("Please add code text before creating exhibit.",
                                "No Code",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            else
            {
                string DnDInputText = inputDnDCodeTextBox.Text;

                var DnDCode = new CodeText();
                DnDCode.OriginalText = DnDInputText;
                DnDCode.TargetText   = DnDTargetTextBox.Text;

                //first, create a dummy bitmap just to get a graphics object
                Image    img     = new Bitmap(1, 1);
                Graphics drawing = Graphics.FromImage(img);
                Font     font    = new Font("Courier New", Globals.FontHeightSizePX, GraphicsUnit.Pixel);



                Color  textColor = Color.FromName("Black");
                string path      = outputFileTextBox.Text;

                // Check expected width and height are below maximum allowed
                DnDCode.WidthPixels  = (int)(Math.Ceiling((DnDCode.MaxLineLength * Globals.FontWidthSizePX)) + (2 * Globals.BorderPaddingPX));
                DnDCode.HeightPixels = (DnDCode.NumberOfLines * (Globals.FontHeightSizePX + Globals.LineSpacingPX)) + (2 * Globals.BorderPaddingPX) + (DnDCode.NumberOfItemLines * Globals.OptionLinePaddingPX * 2) + (DnDCode.NumberOfTargetLines * Globals.OptionLinePaddingPX * 2);
                if (DnDCode.WidthPixels > Globals.MaxDnDWidthPX)
                {
                    // Error: Final image is too wide for DnD area
                    MessageBox.Show(string.Format("This code is too wide to generate an image equal to or less than {0} pixels.", Globals.MaxDnDWidthPX),
                                    "Too wide",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                else if (DnDCode.HeightPixels > Globals.MaxDnDHeightPX)
                {
                    // Error: Final image is too high/too many lines for DnD area
                    MessageBox.Show(string.Format("This code has too many lines to generate an image equal to or less than {0} pixels.", Globals.MaxDnDHeightPX),
                                    "Too high",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                else
                {
                    // Create exhibit
                    DrawText(DnDCode.FinalText, font, textColor, DnDCode.WidthPixels, DnDCode.HeightPixels, path);
                    MessageBox.Show(String.Format("Image successfully created as {0}.", path),
                                    "Completed!",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }
            }
        }