Problem with overlapping alphablended lines in GDI+
-
Hey people! Here's the problem: i have some code that draws a whole bunch of lines, nothing special, just the usual CDC::MoveTo, CDC::LineTo and CDC::DrawBezierTo calls. This works fine. But now the need arose to have these lines alphablended so the background they are drawn onto is visible. So i re-created the drawing code using Gdi+'s Graphics::DrawLine, Graphics::DrawBeziers specifying a Color with an alpha value for the pen that is used to draw the lines. The lines are all of the same color and have the same alpha but they can be really thick. And here comes the catch, of course, everywhere where the lines overlap they "draw over" each other creating a darker area on the picture which is not good. Any ideas how to avoid this? I could try creating a bitmap, drawing the lines onto it, trying to render an alpha channel somehow and then alphablend this onto the original image but i wouldn't like to do this because it sounds a lot like wasting resources and not even sure how to render the alpha. Alternatively could try to fill the bitmap with a color dedicated for transparency, draw lines normally onto it and then blend it with a constant alpha + transparency. Still sounds ugly. My second tought was to use BeginPath/EndPath/WidenPath to generate a path or region from the lines and then fill this blended onto the background but WidenPath for some reason produces holes in the lines. Any ideas? Thanks in advance.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<
-
Hey people! Here's the problem: i have some code that draws a whole bunch of lines, nothing special, just the usual CDC::MoveTo, CDC::LineTo and CDC::DrawBezierTo calls. This works fine. But now the need arose to have these lines alphablended so the background they are drawn onto is visible. So i re-created the drawing code using Gdi+'s Graphics::DrawLine, Graphics::DrawBeziers specifying a Color with an alpha value for the pen that is used to draw the lines. The lines are all of the same color and have the same alpha but they can be really thick. And here comes the catch, of course, everywhere where the lines overlap they "draw over" each other creating a darker area on the picture which is not good. Any ideas how to avoid this? I could try creating a bitmap, drawing the lines onto it, trying to render an alpha channel somehow and then alphablend this onto the original image but i wouldn't like to do this because it sounds a lot like wasting resources and not even sure how to render the alpha. Alternatively could try to fill the bitmap with a color dedicated for transparency, draw lines normally onto it and then blend it with a constant alpha + transparency. Still sounds ugly. My second tought was to use BeginPath/EndPath/WidenPath to generate a path or region from the lines and then fill this blended onto the background but WidenPath for some reason produces holes in the lines. Any ideas? Thanks in advance.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<
Code-o-mat wrote:
I could try creating a bitmap, drawing the lines onto it, trying to render an alpha channel somehow and then alphablend this onto the original image but i wouldn't like to do this because it sounds a lot like wasting resources and not even sure how to render the alpha.
I would use the bitmap method. Render your lines onto a white background and use AlphaBlend()[^] to render it onto your DC. In the BLENDFUNCTION structure (the last parameter to AlphaBlend) set BlendOp to AC_SRC_OVER and play with SourceConstantAlpha.
Independent ACN Business Owner
- Check out the possibilities for your future!
- Financial independance
- Full time or Part time
- In more than 20 countries through North America, Europe, Asia and the Pacific
- Featuring the ACN IRIS 5000 video phone. See the person you are talking to.
Within you lies the power for good - Use it!
-
Code-o-mat wrote:
I could try creating a bitmap, drawing the lines onto it, trying to render an alpha channel somehow and then alphablend this onto the original image but i wouldn't like to do this because it sounds a lot like wasting resources and not even sure how to render the alpha.
I would use the bitmap method. Render your lines onto a white background and use AlphaBlend()[^] to render it onto your DC. In the BLENDFUNCTION structure (the last parameter to AlphaBlend) set BlendOp to AC_SRC_OVER and play with SourceConstantAlpha.
Independent ACN Business Owner
- Check out the possibilities for your future!
- Financial independance
- Full time or Part time
- In more than 20 countries through North America, Europe, Asia and the Pacific
- Featuring the ACN IRIS 5000 video phone. See the person you are talking to.
Within you lies the power for good - Use it!
Thanks for your reply. What i ended up with was to use Gdiplus' GraphicsPath and its WidenPath (at the time i wrote the question i didn't realize Gdiplus had this too, i only knew about the GDI one). This one seems to work much better then the "simple" GDI implementation. Once again, thanks for the reply.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<