public void DrawLabelWithEffects(LabelEffectSetting effectSettings, Graphics g, string labelText, SolidBrush orginalBrush, Font FontLabel, PointF xyCoordinates, StringFormat _StringFormat, LabelCase labelCase, int labelCharacterSpaceInPixels) { SolidBrush brush = null; LinearGradientBrush LinearGBrush = null; if (effectSettings != null && string.IsNullOrEmpty(labelText) == false) { brush = new SolidBrush(effectSettings.EffectColor); switch (effectSettings.Effect) { case LabelEffect.Shadow: //g.DrawString(labelText, FontLabel, brush, xyCoordinates.X + effectSettings.Depth, xyCoordinates.Y + effectSettings.Depth, _StringFormat); //g.DrawString(labelText, FontLabel, orginalBrush, xyCoordinates, _StringFormat); this.DrawString(g, labelText, FontLabel, brush, new PointF(xyCoordinates.X + effectSettings.Depth, xyCoordinates.Y + effectSettings.Depth), _StringFormat, labelCase, labelCharacterSpaceInPixels); this.DrawString(g, labelText, FontLabel, orginalBrush, xyCoordinates, _StringFormat, labelCase, labelCharacterSpaceInPixels); break; case LabelEffect.Embossed: //g.DrawString(labelText, FontLabel, brush, xyCoordinates.X + effectSettings.Depth, xyCoordinates.Y + effectSettings.Depth, _StringFormat); //g.DrawString(labelText, FontLabel, orginalBrush, xyCoordinates, _StringFormat); this.DrawString(g, labelText, FontLabel, brush, new PointF(xyCoordinates.X + effectSettings.Depth, xyCoordinates.Y + effectSettings.Depth), _StringFormat, labelCase, labelCharacterSpaceInPixels); this.DrawString(g, labelText, FontLabel, orginalBrush, xyCoordinates, _StringFormat, labelCase, labelCharacterSpaceInPixels); break; case LabelEffect.Block: for (int i = effectSettings.Depth; i >= 0; i--) { //g.DrawString(labelText, FontLabel, brush, xyCoordinates.X - i, xyCoordinates.Y + i, _StringFormat); this.DrawString(g, labelText, FontLabel, brush, new PointF(xyCoordinates.X - i, xyCoordinates.Y + i), _StringFormat, labelCase, labelCharacterSpaceInPixels); } //g.DrawString(labelText, FontLabel, orginalBrush, xyCoordinates, _StringFormat); this.DrawString(g, labelText, FontLabel, orginalBrush, xyCoordinates, _StringFormat, labelCase, labelCharacterSpaceInPixels); break; case LabelEffect.Gradient: //Find the Size required to draw the Sample Text. SizeF textSize = g.MeasureString(labelText, FontLabel); //Create a Diagonal Gradient LinearGradientBrush. RectangleF gradientRectangle = new RectangleF(new PointF(0, 0), textSize); LinearGBrush = new LinearGradientBrush(gradientRectangle, effectSettings.EffectColor, orginalBrush.Color, LinearGradientMode.ForwardDiagonal); //g.DrawString(labelText, FontLabel, LinearGBrush, xyCoordinates, _StringFormat); this.DrawString(g, labelText, FontLabel, LinearGBrush, xyCoordinates, _StringFormat, labelCase, labelCharacterSpaceInPixels); break; case LabelEffect.Reflect: //Find the Size required to draw the Sample Text. textSize = g.MeasureString(labelText, FontLabel); // Because we will be scaling, and scaling effects the ENTIRE // graphics object, not just the text, we need to reposition // the Origin of the Graphics object (0,0) to the (xLocation, // yLocation) point. If we don't, when we attempt to flip // the text with a scaling transform, it will merely draw the // reflected text at (xLocation, -yLocation) which is outside // the viewable area. g.TranslateTransform(xyCoordinates.X, xyCoordinates.Y); // Reflecting around the Origin still poses problems. The // origin represents the upper left corner of the rectangle. // This means the reflection will occur at the TOP of the // original drawing. This is not how people are used to // seing reflected text. Thus, we need to determine where to // draw the text. This can be done only when we have calculated // the height required by the Drawing. // This is not as simple as it may seem. The Height returned // from the MeasureString method includes some extra spacing // for descenders and whitespace. We want ONLY the height from // the BASELINE (which is the line which all caps sit). Any // characters with descenders drop below the baseline. To // calculate the height above the baseline, use the // GetCellAscent method. Since GetCellAscent returns a Design // Metric value it must be converted to pixels, and scaled for // the font size. // Note: this looks best with characters that can be reflected // over the baseline nicely -- like caps. Characters with descenders // look odd. To fix that uncomment the two lines below, which // then reflect across the lowest descender height. int lineAscent = 0; int lineSpacing = 0; float lineHeight = 0; float textHeight = 0; lineAscent = FontLabel.FontFamily.GetCellAscent(FontLabel.Style); lineSpacing = FontLabel.FontFamily.GetLineSpacing(FontLabel.Style); lineHeight = FontLabel.GetHeight(g); textHeight = lineHeight * lineAscent / lineSpacing; //' Uncomment these lines to reflect over lowest portion //' of the characters. //Dim lineDescent As Integer ' used for reflecting descending characters //lineDescent = myFont.FontFamily.GetCellDescent(myFont.Style) //textHeight = lineHeight * (lineAscent + lineDescent) / lineSpacing // Draw the reflected one first. The only reason to draw the // Reflected one first is to demonstrate the use of the // GraphicsState object. // A GraphicsState object maintains the state of the Graphics // object as it currently stands. You can then scale, resize and // otherwise transform the Graphics object. You can // immediately go back to a previous state using the Restore // method of the Graphics object. // Had we drawn the main one first, we would not have needed the // Restore method or the GraphicsState object. // First Save the graphics state GraphicsState myState = g.Save(); // To draw the reflection, use the ScaleTransform with a negative // value. Using -1 will reflect the Text with no distortion. // Remember to account for the fact that the origin has been reset. g.ScaleTransform(1, -1f); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; // Only reflecting in the Y direction if (labelCharacterSpaceInPixels > 0) { this.DrawString(g, labelText, FontLabel, brush, new PointF(0, -(textHeight + 1 - 2 - textHeight / 2)), _StringFormat, labelCase, labelCharacterSpaceInPixels); } else { g.DrawString(labelText, FontLabel, brush, 0, -(textHeight + 1), sf); } // Reset the graphics state to before the transform g.Restore(myState); // Draw the main text if (labelCharacterSpaceInPixels > 0) { this.DrawString(g, labelText, FontLabel, orginalBrush, new PointF(0, -(textHeight - 2 - textHeight / 2)), _StringFormat, labelCase, labelCharacterSpaceInPixels); } else { g.DrawString(labelText, FontLabel, orginalBrush, 0, -textHeight, sf); } g.TranslateTransform(-xyCoordinates.X, -xyCoordinates.Y); sf.Dispose(); break; default: break; } } }
private void DrawString(Graphics g, string labelCaption, Font labelFont, Brush labelBrush, PointF xyLocation, StringFormat stringFormat, LabelCase labelCase, int characterSpaceInPixels) { if (labelCase == LabelCase.LowerCase) { labelCaption = labelCaption.ToLower(); } else if (labelCase == LabelCase.UpperCase) { labelCaption = labelCaption.ToUpper(); } if (characterSpaceInPixels > 0) { StringFormat GenericTypoGraphicFormat = StringFormat.GenericTypographic; GenericTypoGraphicFormat.Alignment = StringAlignment.Near; GenericTypoGraphicFormat.LineAlignment = StringAlignment.Center; GenericTypoGraphicFormat.FormatFlags = StringFormatFlags.NoClip; //- Starting position of first Letter. float x = xyLocation.X; // Adjust starting position of first Letter // so that central Letter comes at the centroid of Area. SizeF TotalLength = g.MeasureString(labelCaption, labelFont); TotalLength.Width += (labelCaption.Length - 1) * characterSpaceInPixels; x = x - (TotalLength.Width / 2); //- iterate each letter in Label and Draw letter with specified spacing. for (int i = 0; i < labelCaption.Length; i++) { g.DrawString(labelCaption.Substring(i, 1), labelFont, labelBrush, new PointF(x, xyLocation.Y), GenericTypoGraphicFormat); SizeF sf = g.MeasureString(labelCaption.Substring(i, 1), labelFont, new PointF(x, xyLocation.Y), GenericTypoGraphicFormat); x += (float)(sf.Width + characterSpaceInPixels); } GenericTypoGraphicFormat.Dispose(); } else { //- Draw String in Simple way. g.DrawString(labelCaption, labelFont, labelBrush, xyLocation, stringFormat); } }