WPF中GDI+图形图像的绘制:(二)绘制文本——字体描边、渐变、图片叠加

先放效果:

WPF中GDI+图形图像的绘制:(二)绘制文本——字体描边、渐变、图片叠加_第1张图片

1、在上一节WPF中GDI+图形图像的绘制:(一)绘制文本——动态设置字体、大小、颜色 的基础上继续添加字体描边、渐变和图片叠加相关控件,窗体xaml如下:


    
        
            
            
        
        
            
            
        
        
            
        
        
            
                
                    
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                
                
                
                
                
                
                
                    
                
                
                
                
                
                
                
                    
                
                
                
                
                
                
                
            
        
    

2、修改FontItem类添加相关属性:

        private Color gradientColor1;       // 渐变颜色1
        private Color gradientColor2;       // 渐变颜色2
        private BitmapImage overlayImage;   // 叠加图片
        private Color strokeColor;          // 描边颜色
        private int strokeColorLength;      // 描边深度

        public Color GradientColor1
        {
            get
            {
                return gradientColor1;
            }

            set
            {
                gradientColor1 = value;
            }
        }

        public Color GradientColor2
        {
            get
            {
                return gradientColor2;
            }

            set
            {
                gradientColor2 = value;
            }
        }

        public BitmapImage OverlayImage
        {
            get
            {
                return overlayImage;
            }

            set
            {
                overlayImage = value;
            }
        }

        public Color StrokeColor
        {
            get
            {
                return strokeColor;
            }

            set
            {
                strokeColor = value;
            }
        }

        public int StrokeColorLength
        {
            get
            {
                return strokeColorLength;
            }

            set
            {
                strokeColorLength = value;
            }
        }

3、接着修改设置与绘制字体的方法:

        /// 
        /// 设置需要绘制的字体
        /// 
        private void GetFontItem(string text)
        {
            if (text.Length <= 0)
            {
                fontItem = null;
                return;
            }
            fontItem = new FontItem();
            fontItem.FontColor = ((SolidColorBrush)this.rStrokeColor.Fill).Color;
            fontItem.FontName = this.cbFontName.SelectedValue.ToString();
            fontItem.FontSize = (int)this.numFontSize.Value;
            fontItem.Text = text;
        }
        /// 
        /// 绘制字体
        /// 
        private void DrawText()
        {
            try
            {
                if (fontItem == null)
                {
                    this.imgFont.Source = null;
                    return;
                }

                System.Drawing.Font fontText = new System.Drawing.Font(fontItem.FontName, fontItem.FontSize);
                System.Drawing.Size sizeText = System.Windows.Forms.TextRenderer.MeasureText(fontItem.Text, fontText, new System.Drawing.Size(0, 0), System.Windows.Forms.TextFormatFlags.NoPadding);
                Rect viewport = new Rect(0, 0, sizeText.Width, sizeText.Height);

                if ((int)viewport.Width == 0 || (int)viewport.Height == 0)
                    return;

                System.Drawing.Bitmap tempMap = new System.Drawing.Bitmap((int)viewport.Width, (int)viewport.Height);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempMap);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, sizeText.Width, sizeText.Height);
                System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
                path.AddString(fontItem.Text, fontText.FontFamily, (int)fontText.Style, fontText.Size, rect, System.Drawing.StringFormat.GenericDefault);

                //描边
                g.DrawPath(new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(fontItem.StrokeColor.A, fontItem.StrokeColor.R, fontItem.StrokeColor.G, fontItem.StrokeColor.B)), fontItem.StrokeColorLength), path);
                //颜色
                g.FillPath(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(fontItem.FontColor.A, fontItem.FontColor.R, fontItem.FontColor.G, fontItem.FontColor.B)), path);
                //渐变
                g.FillPath(new System.Drawing.Drawing2D.LinearGradientBrush(rect, System.Drawing.Color.FromArgb(fontItem.GradientColor1.A, fontItem.GradientColor1.R, fontItem.GradientColor1.G, fontItem.GradientColor1.B), System.Drawing.Color.FromArgb(fontItem.GradientColor2.A, fontItem.GradientColor2.R, fontItem.GradientColor2.G, fontItem.GradientColor2.B), System.Drawing.Drawing2D.LinearGradientMode.Vertical), path);
                //图片叠加
                if (fontItem.OverlayImage != null)
                {
                    System.Drawing.TextureBrush brush = new System.Drawing.TextureBrush(ImageHelper.BitmapImageToIamge(fontItem.OverlayImage), System.Drawing.Drawing2D.WrapMode.TileFlipXY);//可改变渐变方式
                    g.FillPath(brush, path);
                }

                path.Dispose();

                BitmapImage tempImage = ImageHelper.BitmapToBitmapImage(tempMap,System.Drawing.Imaging.ImageFormat.Png);
                g.Dispose();
                tempMap.Dispose();

                if (tempImage != null)
                {
                    this.imgFont.Source = tempImage;
                    this.imgFont.Width = tempImage.Width;
                    this.imgFont.Height = tempImage.Height;
                    Canvas.SetLeft(this.imgFont, (this.mainCanvas.ActualWidth - tempImage.Width) / 2);
                    Canvas.SetTop(this.imgFont, (this.mainCanvas.ActualHeight - tempImage.Height) / 2);
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

4、添加相关事件:

        /// 
        /// 描边颜色
        /// 
        private void rStrokeColor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ColorSelectorWindow csw = new ColorSelectorWindow();
            csw.ShowDialog();
            if (fontItem != null)
            {
                fontItem.StrokeColor = csw.returnSelectColor;
                this.rStrokeColor.Fill = new SolidColorBrush(csw.returnSelectColor);
                DrawText();
            }
        }
        /// 
        /// 描边深度
        /// 
        private void numStrokeLength_ValueChanged(object sender, EventArgs e)
        {
            if (fontItem != null)
            {
                fontItem.StrokeColorLength = (int)this.numStrokeLength.Value;
                DrawText();
            }
        }
        /// 
        /// 渐变起始颜色
        /// 
        private void rGradientColor1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ColorSelectorWindow csw = new ColorSelectorWindow();
            csw.ShowDialog();
            if (fontItem != null)
            {
                fontItem.GradientColor1 = csw.returnSelectColor;
                this.rGradientColor1.Fill = new SolidColorBrush(csw.returnSelectColor);
                DrawText();
            }
        }
        /// 
        /// 渐变结束颜色
        /// 
        private void rGradientColor2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ColorSelectorWindow csw = new ColorSelectorWindow();
            csw.ShowDialog();
            if (fontItem != null)
            {
                fontItem.GradientColor2 = csw.returnSelectColor;
                this.rGradientColor2.Fill = new SolidColorBrush(csw.returnSelectColor);
                DrawText();
            }
        }
        /// 
        /// 图片叠加
        /// 
        private void btnOverlayImg_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择图片";
            openFileDialog.Filter = "图片(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp"; 
            openFileDialog.FileName = string.Empty;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BitmapImage bi = ImageHelper.LoadBitmapImageByPath(openFileDialog.FileName);
                if(bi!=null)
                {
                    if (fontItem != null)
                    {
                        fontItem.OverlayImage = bi;
                        DrawText();
                    }
                }
            }
        }

 

你可能感兴趣的:(WPF,GDI+,描边,渐变,叠加,WPF,GDI+)