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(); }
public Layer(Int32 nWidth, Int32 nHeight) { m_bmpImage = new FastBitmap(nWidth, nHeight, PixelFormat.Format32bppArgb); Clear(Color.Transparent); m_nOpacity = 1.0; }
internal FastBitmap Flatten(Color crBack) { // create a bitmap for result image FastBitmap bmpFinal = new FastBitmap(m_nWidth, m_nHeight, PixelFormat.Format24bppRgb); // lock all bitmaps bmpFinal.Lock(); for (Int32 i = 0; i < m_arLayers.Count; i++) { Layer layer = m_arLayers[i]; layer.m_bmpImage.Lock(); if (layer.Mask != null) { layer.Mask.Lock(); } } // calculate colors of flattened image // 1. take offsetx, offsety into consideration // 2. calculate alpha of color (alpha, opacity, mask) // 3. mix colors of current layer and layer below for (Int32 y = 0; y < m_nHeight; y++) { for (Int32 x = 0; x < m_nWidth; x++) { Color c0 = m_arLayers[0].m_bmpImage.GetPixel(x, y); for (Int32 i = 1; i < m_arLayers.Count; i++) { Layer layer = m_arLayers[i]; Color c1 = Color.Transparent; if (x >= layer.OffsetX && x <= layer.OffsetX + layer.m_bmpImage.Width - 1 && y >= layer.OffsetY && y <= layer.OffsetY + layer.m_bmpImage.Height - 1) { c1 = layer.m_bmpImage.GetPixel(x - layer.OffsetX, y - layer.OffsetY); } if (c1.A == 255 && layer.Opacity == 1.0 && layer.Mask == null) { c0 = c1; } else { Double tr, tg, tb, a; a = (c1.A / 255.0) * layer.Opacity; if (layer.Mask != null) { a *= layer.Mask.GetIntensity(x, y) / 255.0; } tr = c1.R * a + c0.R * (1.0 - a); tg = c1.G * a + c0.G * (1.0 - a); tb = c1.B * a + c0.B * (1.0 - a); tr = Math.Round(tr); tg = Math.Round(tg); if (i == 4) // shadow layer { tb = Math.Round(tb); tr = Math.Min(tr, crBack.R); tg = Math.Min(tg, crBack.G); tb = Math.Min(tb, crBack.B); } else { tr = Math.Min(tr, 255); tg = Math.Min(tg, 255); tb = Math.Min(tb, 255); } c0 = Color.FromArgb((Byte)tr, (Byte)tg, (Byte)tb); } } bmpFinal.SetPixel(x, y, c0); } } // unlock all bitmaps for (Int32 i = 0; i < m_arLayers.Count; i++) { Layer layer = m_arLayers[i]; layer.m_bmpImage.Unlock(); if (layer.Mask != null) { layer.Mask.Unlock(); } } bmpFinal.Unlock(); return(bmpFinal); }
public void Blur(Int32 nHorz, Int32 nVert) { Single nWeightSum; Single[] nWeights; FastBitmap bmpBlur = (FastBitmap)m_bmpImage.Clone(); m_bmpImage.Lock(); bmpBlur.Lock(); // horizontal blur nWeights = new Single[nHorz * 2 + 1]; for (Int32 i = 0; i < nHorz * 2 + 1; i++) { Single y = Gauss(-nHorz + i, 0, nHorz); nWeights[i] = y; } for (Int32 row = 0; row < m_bmpImage.Height; row++) { for (Int32 col = 0; col < m_bmpImage.Width; col++) { Double r = 0; Double g = 0; Double b = 0; nWeightSum = 0; for (Int32 i = 0; i < nHorz * 2 + 1; i++) { Int32 x = col - nHorz + i; if (x < 0) { i += -x; x = 0; } if (x > m_bmpImage.Width - 1) { break; } Color c = m_bmpImage.GetPixel(x, row); r += c.R * nWeights[i]; g += c.G * nWeights[i]; b += c.B * nWeights[i]; nWeightSum += nWeights[i]; } r /= nWeightSum; g /= nWeightSum; b /= nWeightSum; Byte br = (Byte)Math.Round(r); Byte bg = (Byte)Math.Round(g); Byte bb = (Byte)Math.Round(b); if (br > 255) { br = 255; } if (bg > 255) { bg = 255; } if (bb > 255) { bb = 255; } bmpBlur.SetPixel(col, row, Color.FromArgb(br, bg, bb)); } } // vertical blur nWeights = new Single[nVert * 2 + 1]; for (Int32 i = 0; i < nVert * 2 + 1; i++) { Single y = Gauss(-nVert + i, 0, nVert); nWeights[i] = y; } for (Int32 col = 0; col < m_bmpImage.Width; col++) { for (Int32 row = 0; row < m_bmpImage.Height; row++) { Double r = 0; Double g = 0; Double b = 0; nWeightSum = 0; for (Int32 i = 0; i < nVert * 2 + 1; i++) { Int32 y = row - nVert + i; if (y < 0) { i += -y; y = 0; } if (y > m_bmpImage.Height - 1) { break; } Color c = bmpBlur.GetPixel(col, y); r += c.R * nWeights[i]; g += c.G * nWeights[i]; b += c.B * nWeights[i]; nWeightSum += nWeights[i]; } r /= nWeightSum; g /= nWeightSum; b /= nWeightSum; Byte br = (Byte)Math.Round(r); Byte bg = (Byte)Math.Round(g); Byte bb = (Byte)Math.Round(b); if (br > 255) { br = 255; } if (bg > 255) { bg = 255; } if (bb > 255) { bb = 255; } m_bmpImage.SetPixel(col, row, Color.FromArgb(br, bg, bb)); } } bmpBlur.Dispose(); // will unlock m_bmpImage.Unlock(); }