Beispiel #1
0
        // Convert image to ascii text-image using ImageToText object.
        private string GetAsciiImage(bool useBlock)
        {
            if (AsciiArtist == null)
            {
                return(null);
            }
            ;
            int width  = -1;
            int height = -1;
            int scale  = 1;

            // Retrieve image width.
            if (txtWidth.Text != "")
            {
                string val       = txtWidth.Text;
                bool   isPercent = false;

                if (val.EndsWith("%"))
                {
                    isPercent = true;
                    val       = val.Substring(0, val.Length - 1);
                }

                try
                {
                    width = (int)UInt16.Parse(val);

                    if (isPercent)
                    {
                        width         = AsciiArtist.ImageWidth * width / 100;
                        txtWidth.Text = width.ToString();
                    }
                }
                catch
                {
                    width         = AsciiArtist.ImageWidth;
                    txtWidth.Text = width.ToString();
                    MessageBox.Show("Invalid image width! Reset to default value.", "Error");
                }
            }

            // Retrieve image height.
            if (txtHeight.Text != "")
            {
                string val       = txtHeight.Text;
                bool   isPercent = false;

                if (val.EndsWith("%"))
                {
                    isPercent = true;
                    val       = val.Substring(0, val.Length - 1);
                }

                try
                {
                    height = (int)UInt16.Parse(val);

                    if (isPercent)
                    {
                        height         = AsciiArtist.ImageHeight * height / 100;
                        txtHeight.Text = height.ToString();
                    }
                }
                catch
                {
                    height         = AsciiArtist.ImageHeight;
                    txtHeight.Text = height.ToString();
                    MessageBox.Show("Invalid image height! Reset to default value.", "Error");
                }
            }

            // Validate image width & height.
            if ((txtWidth.Text == "") && (txtHeight.Text == ""))
            {
                width          = AsciiArtist.ImageWidth;
                height         = AsciiArtist.ImageHeight;
                txtWidth.Text  = width.ToString();
                txtHeight.Text = height.ToString();
            }
            else if (txtWidth.Text == "")
            {
                width         = height * AsciiArtist.ImageWidth / AsciiArtist.ImageHeight;
                txtWidth.Text = width.ToString();
            }
            else if (txtHeight.Text == "")
            {
                height         = width * AsciiArtist.ImageHeight / AsciiArtist.ImageWidth;
                txtHeight.Text = height.ToString();
            }

            // Retrieve scaling factor.
            if (radScale.SelectedItem != null)
            {
                scale = Int32.Parse(radScale.SelectedItem.ToString());
            }
            else
            {
                scale = 1;
            }

            /*
             * foreach (Control comp in Form1.Controls)
             * {
             *      RadioButton radScale = comp as RadioButton;
             *      if (radScale == null) continue;
             *      if (!radScale.Checked) continue;
             *      if (radScale.ID.Length < 9) continue;
             *      if (!radScale.ID.StartsWith ("radScale")) continue;
             *
             *      try
             *      {
             *              scale = Int32.Parse (radScale.ID.Substring (8));
             *              break;
             *      }
             *      catch {}
             * }
             */

            // Ascii-fy image.
            try
            {
                if (radScale.SelectedItem != null)
                {
                    return(AsciiArtist.GetAsciiImage(scale, useBlock));
                }
                else
                {
                    return(AsciiArtist.GetAsciiImage(width, height, useBlock));
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error");
                return(null);
            }
        }