Winforms Drag & Drop image with transparency

Russ Suter
Russ Suter used Ask the Experts™
on
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).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Architect
Commented:
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

Russ SuterSenior Software Developer

Author

Commented:
That's good enough for now. I'll look into the alpha gradient another time. Thanks.
Karrtik IyerSoftware Architect

Commented:
Glad it helped, you are welcome. Hope you have a nice long weekend.
Russ SuterSenior Software Developer

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial