Advanced color blending with GDI+

Using GDI+ with Windows Forms, I want to be able to draw with a pen and blend color based on the destination pixel color.

For example, if I draw a line and it passes over black pixels, I want it to be a lighter color (like white for example) so that it's visible. When that same line passes over white pixels, it should be a darker color (black for example) so that it's still clearly visible.

Is there any way to do this with GDI+?


As Hans Passant proposed, you could paint using what's currently in the canvas as the image for a texture brush (you might need double buffering for that to work correctly) and use a ColorMatrix to modify the colors being painted on the canvas.

There is a color matrix that inverts the colors similar to a XOR, the problem is it won't work with the middle gray. A color matrix that inverts RGB and leaves alpha intact would be:

-1, 0, 0, 0, 0
 0,-1, 0, 0, 0
 0, 0,-1, 0, 0
 0, 0, 0, 1, 0
 1, 1, 1, 0, 1

Something similar, albeit slower would be to copy the canvas to an image and process that image pixel per pixel with rules such as if the color is brighter than 0.5, make it slightly darker else, make it slightly brighter. Then, you paint with that processed image as a texture brush. This would give a better result but it would be significantly slower than using a ColorMatrix .


You could try XORing the pen color. Paint.NET does this with the selection border to make it visible on any color.


Oh, I don't think this is too difficult. You could create a pen that automatically changes colors based on wherever it is. Simply read the pixel at where the location of the pen is (See example), Get the Alpha component and set the pen color to black or white if it's greater than or less than 255/2 respectively :)

链接地址: http://www.djcxy.com/p/67762.html

上一篇: 一组字符串并重新打开String

下一篇: 与GDI +进行高级颜色混合