Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
whats the best way to make a simple erase brush / pen to erase parts of a transparency bitmap (with erase opacity)?
Posted
Comments
Sergey Alexandrovich Kryukov 23-May-11 14:41pm    
I answered, but some information on your technique could be helpful. I had to use some assumptions un my answer.
--SA

How to do it totally depends on the data presentation of your image being edited. If this is just a bitmap and not a collection of Z-ordered bit-mapped objects (as in mature editors like GIMP and Photoshop), you have only two possibilities. Let's assume you render all your graphics on a bitmap in each step using the instance of Graphics obtained from you bitmap via Graphics.FromImage.

If you do something more complex, please explain it; I'll give a different answer.

In both cases, you need to get your input device (mouse) stroke and calculate the sub-set of the image bit sets, using the brush size and shape. For example, it could be a segment of straight line (actually, a rectangle) with rounded ends, according the the rounding shape. You need to calculate an object of the type Region our of it.

Variant #1: You need to assume there is a background color as a property of your data model of the image. You need to use to use Graphic.FillRegion as a rendering method.

Variant #2: You need to use to clip the region of your Graphics instance to empty region and than include the region calculated on the previous step. Use Graphics.ExcludeClip/IncludeClip. Now you need to reduce opacity of the image in the clip region. To do it, use Image.LockBits and Marshal to convert the image to a bit array. You will find the code sample here: http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^]. When this is done, modify the alpha channel in all the image to reduce opacity (to zero or more); when the resulting data is applied to the same bitmap, it will be applied only to a region that was covered with you brush. Don't forget to restore the clip region to the full image when you're done.

If you have layers, it does not change much, but only transparency bitmap is applicable.

—SA
 
Share this answer
 
i found another solution
create a texture brush based on the texture you are drawing with opacity (using a ColorMatrix) and set the graphics option CompositingMode to SourceCopy and draw your circle or whatever.

some code:
float[][] matrixItems ={ 
                               new float[] {1, 0, 0, 0, 0},
                               new float[] {0, 1, 0, 0, 0},
                               new float[] {0, 0, 1, 0, 0},
                               new float[] {0, 0, 0, opacity, 0}, //opacity is between 0 and 1
                               new float[] {0, 0, 0, 0, 1}};
            System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(matrixItems);

            // Create an ImageAttributes object and set its color matrix.
            System.Drawing.Imaging.ImageAttributes imageAtt = new System.Drawing.Imaging.ImageAttributes();
            imageAtt.SetColorMatrix(colorMatrix,
                System.Drawing.Imaging.ColorMatrixFlag.Default,
                System.Drawing.Imaging.ColorAdjustType.Bitmap);
            RectangleF newRectangleF = new RectangleF(0.0f, 0.0f, YourImage.Width, YourImage.Height);
            // Create a TextureBrush using the bitmap and the specified ImageAttributes 
            TextureBrush newTextureBrush = new TextureBrush(YourImage, newRectangleF, imageAtt);


based on the msdn code for a texturebrush with opacity
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900