天天看点

WPF中GDI+图形图像的绘制:(五)绘制图像——蒙板效果

这里要实现的效果指的是对图片像素点区域进行颜色叠加,首先要做的是得到图片非透明部分的区域,效果如图:

WPF中GDI+图形图像的绘制:(五)绘制图像——蒙板效果

实现步骤:1、在主窗体添加控制控件:

<Label Grid.Row="4" Grid.Column="0"  Content="蒙板" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <Rectangle x:Name="rPicMaskColor" Grid.Row="4" Grid.Column="1" Fill="Black" Width="30" Height="30" VerticalAlignment="Center" HorizontalAlignment="Left" MouseLeftButtonDown="rPicMaskColor_MouseLeftButtonDown"></Rectangle>
           

2、图片处理类型枚举添加蒙板类型:

//图片处理类型枚举
        public enum ImageProcessingEffect
        {
            Normal = 0,//原始图片
            Emboss = 1,//浮雕
            Sharpening = 2,//锐化
            BlackAndWhite = 3,//黑白
            MirrorHorizontal = 4,//水平镜像
            MirrorVertical = 5,//垂直镜像
            CenterRotate = 6,//中心旋转
            Mask = 7//蒙板
        }
           

3、绘制图片方法DrawImage里添加设置蒙板:

......
case ImageProcessingEffect.Mask://蒙板
bitmap = Mask(((SolidColorBrush)this.rPicMaskColor.Fill).Color);
this.imgPic.Source = bitmap;
break;
......
           

4、添加绘制蒙板方法,这里为了对像素点进行操作,添加unsafe标识,同时需要在工程属性-生成-勾选允许不安全代码:

/// <summary>
        /// 绘制蒙板
        /// </summary>
        private unsafe BitmapImage Mask(Color maskColor)
        {
            System.Drawing.Bitmap tempImage = new System.Drawing.Bitmap(this.imgSource.StreamSource);
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            int w = tempImage.Width;
            int h = tempImage.Height;
            System.Drawing.Imaging.BitmapData bckdata = null;
            //目标位图
            System.Drawing.Region region = null;
            System.Drawing.Bitmap destImage = null;
            System.Drawing.Graphics graphics = null;
            try
            {
                // 根据图片得到一个图片非透明部分的区域
                bckdata = tempImage.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                uint* bckInt = (uint*)bckdata.Scan0;
                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        if ((*bckInt & 0xff000000) != 0)
                        {
                            path.AddRectangle(new System.Drawing.Rectangle(i, j, 1, 1));
                        }
                        bckInt++;
                    }
                }
                tempImage.UnlockBits(bckdata);
                bckdata = null;

                region = new System.Drawing.Region(path);

                //定义画布,宽高为图像原始的宽高
                destImage = new System.Drawing.Bitmap(w, h);
                graphics = System.Drawing.Graphics.FromImage(destImage);
                System.Drawing.RectangleF newRect = region.GetBounds(graphics);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                //图像蒙板
                if(maskColor != null)
                    graphics.FillPath(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(maskColor.A, maskColor.R, maskColor.G, maskColor.B)), path);

                path.Dispose();
                path = null;

                BitmapImage bi = ImageHelper.BitmapToBitmapImage(destImage, System.Drawing.Imaging.ImageFormat.Png);
                destImage.Dispose();
                return bi;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (bckdata != null)
                {
                    tempImage.UnlockBits(bckdata);
                    bckdata = null;
                }
                if (graphics != null)
                    graphics.Dispose();
            }
        }
           

5、实现控件事件:

/// <summary>
        /// 蒙板
        /// </summary>
        private void rPicMaskColor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ColorSelectorWindow csw = new ColorSelectorWindow();
            csw.ShowDialog();
            this.rPicMaskColor.Fill = new SolidColorBrush(csw.returnSelectColor);
            DrawImage(ImageProcessingEffect.Mask);
        }
           

相关博文:

WPF中GDI+图形图像的绘制:(一)绘制文本——动态设置字体、大小、颜色

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

WPF中GDI+图形图像的绘制:(三)绘制图像——实现黑白、浮雕、锐化效果

WPF中GDI+图形图像的绘制:(四)绘制图像——水平垂直镜像与中心旋转

WPF中GDI+图形图像的绘制:(五)绘制图像——蒙板效果

WPF中GDI+图形图像的绘制:(六)绘制图像——鼠标选中后绘制边框

WPF中GDI+图形图像的绘制:(七)绘制图像——鼠标拖动改变位置和大小

WPF中GDI+图形图像的绘制:(八)位置坐标和宽高与控件绑定展示

继续阅读