Hi Guys,

Right now I'm building an application that draws images to the screen. What I would like is to be able to 'cut' portions of those images away (this is going to always be a circle).

This is the code I'm using. Is it possible to make a clipping region with circles?

g.DrawImage(planetImage, _x - radius, _y - radius, radius * 2, radius * 2);

Obviously I'd like a relatively computationally efficient solution. If necessary I could probably modify the bitmap on the fly (is this possible?) and paint the modified one onto the screen, but preferably I'd like a solution that paints to the screen, except for certain areas (again, these area's are always circles).

Anyway, if you need more info, let me know, thanks,
-Pokenerd

Recommended Answers

All 6 Replies

Winforms has broken support for transparent images. Unless you are willing to write your own control to deal with it, you'd be better off moving to WPF or XNA depending on what you are trying to do.

Xna won't work for this application. Can I integrate WPF with GDI+? Because most of what I have is already in gdi+...

The transparent image doesn't need to be saved or anything, just blitted to the screen with part of it missing.

If you do your own blit routine, you can just skip the transparent parts. Still a lot of work as you'll have to take over the Draw method.

And I'm not sure about using gdi+ with WPF, as I've barely touched on what WPF can do (it has a steep learning curve). It's next on my list of books to read :)

Alright, what would taking over the Draw method entail? Any decent links where I can check more of this out?

Here is someone who's done some transparent work using GDI. It actually doesn't look that bad :)

That's a cool link, thanks for pointing that out.

I did however solve it slightly differently. Since I needed 100% transparency, I could just delete pixels from the image that I'm working with. My solution is all GDI+.


It obviously could be made more efficient (probably by implementing the midpoint circle algorithm), but it works.

public void Hit(Point xy, int rad)
        {
int lastY = 0;

                        Point centerOffset = new Point(xy.X - X + (int)radius, xy.Y-Y+(int)radius);
                        Color trans = Color.FromArgb(0,0,0,0);

                        
for (int x = -rad; x < 1; x++)
                        {
                            for (int y = -rad; y < 1; y++)
                            {
                                if (Game.withinDist(x, y, 0, 0, rad))//Found an edge.
                                {
                                    lastY = y;
                                    for (int drawY = y; drawY < -y; drawY++)
                                    {
                                        if (centerOffset.X + x < 0 || centerOffset.X - x >= planetImage.Width)
                                            break;
                                        

                                        if(!(centerOffset.Y + drawY < 0 || centerOffset.Y + drawY >= planetImage.Height)){
                                            //Drawing down from the point..
                                            planetImage.SetPixel(centerOffset.X + x, centerOffset.Y + drawY, trans);
                                        }
                                        if (!(centerOffset.Y - drawY < 0 || centerOffset.Y - drawY >= planetImage.Height))
                                        {
                                            //Drawing up from the opposit side...
                                            planetImage.SetPixel(centerOffset.X - x, centerOffset.Y - drawY, trans);
                                        }
                                    }
                                    break;
                                }
                            }
                        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.