Exemplo n.º 1
0
 public Layers(LayeredImage image)
 {
     m_oImage = image;
 }
Exemplo n.º 2
0
        private void m_textBoxLogo_TextChanged(object sender, EventArgs e)
        {
            String strFontSize = m_textBoxSize.Text.Trim();

            if (strFontSize == null || strFontSize.Length < 1)
            {
                MessageBox.Show("Please select Font Size between 10 and 100", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxSize.Focus();
                return;
            }

            Int32 nSize = Convert.ToInt32(strFontSize);

            if (nSize < 10 || nSize > 100)
            {
                MessageBox.Show("Please select Font Size between 10 and 100", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxSize.Focus();
                return;
            }


            Int32 nSelectedItem = m_cmbFont.SelectedIndex;

            if (nSelectedItem == -1)
            {
                MessageBox.Show("Please select Font Name", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_cmbFont.Focus();
                return;
            }

            String strFontName = m_cmbFont.SelectedFontFamily.Name;

            if (strFontName == null || strFontName.Length < 1)
            {
                MessageBox.Show("Please select Font Name", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_cmbFont.Focus();
                return;
            }

            Int32  nOffsetX   = 4;
            String strOffsetX = m_textBoxOffsetX.Text.Trim();

            if (strOffsetX == null || strOffsetX.Length < 1)
            {
                MessageBox.Show("Please select Shadow OffsetX between -10 and 10", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxOffsetX.Focus();
                return;
            }
            else
            {
                nOffsetX = Convert.ToInt32(strOffsetX);
                if (nOffsetX < -10 || nOffsetX > 10)
                {
                    MessageBox.Show("Please select Shadow OffsetX between -10 and 10", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxOffsetX.Focus();
                    return;
                }
            }

            Int32  nOffsetY   = 4;
            String strOffsetY = m_textBoxOffsetY.Text.Trim();

            if (strOffsetY == null || strOffsetY.Length < 1)
            {
                MessageBox.Show("Please select Shadow OffsetX between -10 and 10", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxOffsetY.Focus();
                return;
            }
            else
            {
                nOffsetY = Convert.ToInt32(strOffsetY);
                if (nOffsetY < -10 || nOffsetY > 10)
                {
                    MessageBox.Show("Please select Shadow OffsetY between -10 and 10", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxOffsetY.Focus();
                    return;
                }
            }

            Int32  nOpacity   = 50;
            String strOpacity = m_textBoxOpacity.Text.Trim();

            if (strOpacity == null || strOpacity.Length < 1)
            {
                MessageBox.Show("Please select Shadow Opacity between 0 and 100%", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxOpacity.Focus();
                return;
            }
            else
            {
                nOpacity = Convert.ToInt32(strOpacity);
                if (nOpacity < 0 || nOpacity > 100)
                {
                    MessageBox.Show("Please select Shadow Opacity between 0 and 100%", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxOpacity.Focus();
                    return;
                }
            }
            if (nOpacity > 99)
            {
                nOpacity = 99;
            }

            Int32  nBlur   = 4;
            String strBlur = m_textBoxBlur.Text.Trim();

            if (strBlur == null || strBlur.Length < 1)
            {
                MessageBox.Show("Please select Shadow Blur between 0 and 50", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxBlur.Focus();
                return;
            }
            else
            {
                nBlur = Convert.ToInt32(strBlur);
                if (nBlur < 0 || nBlur > 50)
                {
                    MessageBox.Show("Please select Shadow Blur between 0 and 50", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxBlur.Focus();
                    return;
                }
            }

            Int32  nAzimuth   = 135;
            String strAzimuth = m_textBoxAzimuth.Text.Trim();

            if (strAzimuth == null || strAzimuth.Length < 1)
            {
                MessageBox.Show("Please select Text Bump Azimuth between 0 and 360 degreeses", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxAzimuth.Focus();
                return;
            }
            else
            {
                nAzimuth = Convert.ToInt32(strAzimuth);
                if (nAzimuth < 0 || nAzimuth > 360)
                {
                    MessageBox.Show("Please select Text Bump Azimuth between 0 and 360 degreeses", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxAzimuth.Focus();
                    return;
                }
            }

            Int32  nElevation   = 45;
            String strElevation = m_textBoxElevation.Text.Trim();

            if (strElevation == null || strElevation.Length < 1)
            {
                MessageBox.Show("Please select Text Bump Elevation between 0 and 100", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxElevation.Focus();
                return;
            }
            else
            {
                nElevation = Convert.ToInt32(strElevation);
                if (nElevation < 0 || nElevation > 100)
                {
                    MessageBox.Show("Please select Text Bump Elevation between 0 and 100", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxElevation.Focus();
                    return;
                }
            }

            Int32  nWidth   = 3;
            String strWidth = m_textBoxWidth.Text.Trim();

            if (strWidth == null || strWidth.Length < 1)
            {
                MessageBox.Show("Please select Text Bump Bevel Width between 1 and 10", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxWidth.Focus();
                return;
            }
            else
            {
                nWidth = Convert.ToInt32(strWidth);
                if (nWidth < 1 || nWidth > 100)
                {
                    MessageBox.Show("Please select Text Bump Bevel Width between 1 and 10", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    m_textBoxWidth.Focus();
                    return;
                }
            }

            m_progressBarProcess.Maximum = 10;
            m_progressBarProcess.Minimum = 0;
            m_progressBarProcess.Step    = 1;
            m_progressBarProcess.Value   = 0;

            Font oFont;

            try
            {
                oFont = new Font(strFontName, nSize);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }


            String strLogo = m_textBoxLogo.Text.Trim();

            if (strLogo == null || strLogo.Length < 1)
            {
                MessageBox.Show("Please select Logo Text", "Color Logo Maker", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                m_textBoxLogo.Focus();
                return;
            }

            Color[] colors = new Color[7];
            colors[0] = m_labelColor1.BackColor;
            colors[1] = m_labelColor2.BackColor;
            colors[2] = m_labelColor3.BackColor;
            colors[3] = m_labelColor4.BackColor;
            colors[4] = m_labelColor5.BackColor;
            colors[5] = m_labelColor6.BackColor;
            colors[6] = m_labelColor7.BackColor;


            // measure string

            Bitmap   bmpTest = new Bitmap(10, 10, PixelFormat.Format24bppRgb);
            Graphics g       = Graphics.FromImage(bmpTest);

            float nUnderscoreWidth = g.MeasureString(" __ ", oFont).Width;

            float nTotalWidth = 0;
            float nMaxHeight  = 0;

            float[] nCharWidths  = new float[strLogo.Length];
            float[] nCharOffsets = new float[strLogo.Length];
            for (int i = 0; i < strLogo.Length; i++)
            {
                SizeF fsCharWidth = g.MeasureString(String.Concat(" _", strLogo[i].ToString(), "_ "), oFont);
                float nCharWidth  = fsCharWidth.Width - nUnderscoreWidth;

                if (nMaxHeight < fsCharWidth.Height)
                {
                    nMaxHeight = fsCharWidth.Height;
                }

                nCharWidths[i] = nCharWidth;
                nTotalWidth   += nCharWidth;
            }

            IntPtr hdc = g.GetHdc();

            IntPtr oldFont = Gdi.SelectObject(hdc, oFont.ToHfont());

            Gdi.SelectObject(hdc, oldFont);

            g.ReleaseHdc(hdc);

            g.Dispose();
            bmpTest.Dispose();

            System.Threading.Thread.Sleep(1);
            m_progressBarProcess.PerformStep();

            LayeredImage image = new LayeredImage((int)nTotalWidth + 2 * Math.Abs(nOffsetX), (int)nMaxHeight + Math.Abs(nOffsetY));

            m_progressBarProcess.PerformStep();

            Layer layerText = image.Layers.Add();

            layerText.Clear(Color.Black);

            float nCharOffset = (float)Math.Abs(nOffsetX);

            for (int i = 0; i < strLogo.Length; i++)
            {
                layerText.DrawText((int)nCharOffset, 0, strLogo[i].ToString(), oFont, new SolidBrush(Color.White));
                nCharOffset += nCharWidths[i];
            }

            m_progressBarProcess.PerformStep();

            Layer layerBlurText = image.Layers.Copy(layerText);

            layerBlurText.Blur(nBlur, nBlur);
            m_progressBarProcess.PerformStep();

            Layer layerShadowBackground = image.Layers.Add();

            layerShadowBackground.Clear(m_labelColorBack.BackColor);

            m_progressBarProcess.PerformStep();

            Layer layerShadow = image.Layers.Copy(layerBlurText);

            layerShadow.Invert();
            layerShadow.Opacity  = (double)nOpacity / 100.0;
            layerShadow.OffsetX += nOffsetX;
            layerShadow.OffsetY += nOffsetY;

            nCharOffset = (float)Math.Abs(nOffsetX);
            Int32 nColorIndex = 0;

            m_progressBarProcess.PerformStep();

            Layer layerFinal = image.Layers.Add();

            m_progressBarProcess.PerformStep();

            for (Int32 i = 0; i < strLogo.Length; i++)
            {
                Color color = colors[nColorIndex];
                nColorIndex++;
                if (nColorIndex >= colors.Length)
                {
                    nColorIndex = 0;
                }

                SolidBrush brush = new SolidBrush(color);
                layerFinal.FillRectangle((int)nCharOffset, 0, (int)nCharWidths[i], (int)nMaxHeight, brush);
                nCharOffset += nCharWidths[i];
            }

            m_progressBarProcess.PerformStep();

            layerFinal.BumpMap(layerBlurText, nAzimuth, nElevation, nWidth, false);
            m_progressBarProcess.PerformStep();

            layerFinal.Mask = (FastBitmap)layerText.Bitmap.Clone();
            m_progressBarProcess.PerformStep();

            FastBitmap result = image.Flatten(m_labelColorBack.BackColor);

            m_pictureBoxPreview.Image = result.GetBitmap();
        }