Winforms Drag & Drop image with transparency

I have the following code:
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            Button btn = sender as Button;
            using (Bitmap bmp = new Bitmap(btn.Width, btn.Height))
            {
                btn.DrawToBitmap(bmp, new Rectangle(Point.Empty, btn.Size));
                using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    ColorMatrix matrix = new ColorMatrix();
                    matrix.Matrix33 = .5F;
                    ImageAttributes attributes = new ImageAttributes();
                    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    gfx.DrawImage(bmp, new Rectangle(Point.Empty, bmp.Size));
                    Cursor cur = new Cursor(bmp.GetHicon());
                    Cursor.Current = cur;
                }
            }
        }

Open in new window

It's supposed to draw the image with transparency but it's opaque. How do I add alpha channel info? My ultimate goal is to have an alpha channel gradient so the transparency increases as the image gets further away from a given point (usually the center or the left edge).
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
I was able to achieve transparency (without any gradients) by modifying your code to below. See comments below. Now on dragging the button I am able to see a transparent image.
       private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            Button btn = sender as Button;            
            using (Bitmap bmp = new Bitmap(btn.Width, btn.Height))
            {

                btn.DrawToBitmap(bmp, new Rectangle(Point.Empty, btn.Size));
                using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;//Karrtik: To set the alpha channel (https://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingmode%28v=VS.90%29.aspx)
                    ColorMatrix matrix = new ColorMatrix();
                    matrix.Matrix33 = .5F;
                    ImageAttributes attributes = new ImageAttributes();                                       
                    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    //Karrtik: The above image attributes, where not used in your code, I passed it along to the draw image function
                    Rectangle rect = new Rectangle(Point.Empty, bmp.Size);                    
                    gfx.DrawImage(bmp, rect,rect.X,rect.Y,rect.Width,rect.Height,GraphicsUnit.Pixel, attributes);
                    Cursor cur = new Cursor(bmp.GetHicon());
                    Cursor.Current = cur;
                }
            }
            

        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterSenior Software DeveloperAuthor Commented:
That's good enough for now. I'll look into the alpha gradient another time. Thanks.
Karrtik IyerSoftware ArchitectCommented:
Glad it helped, you are welcome. Hope you have a nice long weekend.
Russ SuterSenior Software DeveloperAuthor Commented:
More points in it for you if you can answer this question also:
http://www.experts-exchange.com/questions/28904561/DataGridView-get-current-row-rectangle.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.