直接上源码

        注:碰到的问题是画刷会以整个图形的最左侧为基础,不是以Rectangle为基础的

        /// <summary>
        /// 设置图片边缘透明
        /// </summary>
        /// <param name="oriBitmap">图片</param>       
        /// <returns></returns>
        public Bitmap SetBitmapLeftEdgeTransparent(Bitmap oriBitmap, Color backColor)
        {
            //渐变范围
            int gradientRange = oriBitmap.Width / 5;
            //建立图像对象
            Bitmap newBitmap = new Bitmap(oriBitmap.Width, oriBitmap.Height);
            Graphics graphics = Graphics.FromImage(newBitmap);           
            graphics.DrawImage(oriBitmap, new Rectangle(0, 0, oriBitmap.Width, oriBitmap.Height));

            //将后边部分200范围内 设置透明渐变
            Bitmap gradientBitmap = new Bitmap(gradientRange, newBitmap.Height);
            Graphics gradientGraphics = Graphics.FromImage(gradientBitmap);
            gradientGraphics.DrawImage(oriBitmap, 0, 0, new RectangleF(newBitmap.Width - gradientRange, 0, gradientRange, newBitmap.Height), GraphicsUnit.Pixel);
            Rectangle rect = new Rectangle(0, 0, gradientRange, newBitmap.Height);
            LinearGradientBrush lineBrush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor.R, backColor.G, backColor.B), backColor, (float)0);           
            gradientGraphics.FillRectangle(lineBrush, rect);
            gradientGraphics.Dispose();
            //将渐变内容放置回来
            graphics.DrawImage(gradientBitmap, newBitmap.Width - gradientRange, 0);           
            graphics.Dispose();           
            return newBitmap;
        }

 

posted on 2016-06-23 16:48  jason-chen  阅读(1080)  评论(0编辑  收藏  举报