Make an eraser

How do I make an eraser like what you find in basic image editing programs? I have recently been trying to learn more about pictures and masks, as you can probably tell by recent questions.

I have found myself, nearer the start of developing my project making an eraser by creating a picture and apply a white-filled mask. And whenever I wanted to draw to that image I would draw to the image and to the mask so that as things get drawn they become visible.

Recently, I have decided to get away from this approach because I do not want to keep manually drawing to the mask. I guess I could use a method that does all of this for me less manually but I do not think this is the best approach.

Recently, I have decided to take a different approach of creating a picture without a mask and drawing onto that. I am now trying to create an eraser. Previously I simply drew onto the mask with the colour white as an eraser but this does not work.

So what is the best approach to making an eraser?

Thanks

What about simply drawing over the picture.graphics ?

I do not think you understand my question. I have tried that. You have an example?

Use a mask, but make it completely black (opaque) to start. The only white will be your erasures as they happen. When you’re done, composite the whole thing into a final image and start next time with a completely black mask.

The problem with that is that I do not start with a blank transparent image.

Thanks

You can add a mask to any image.

Check for a mask first, of course.

// given p, a picure
dim m as picture = p.mask(false)
if m is nil then
   // create a mask
   m = p.mask(true)
   dim g as graphics = m.graphics
   g.forecolor = &c000000
   g.fillrect 0,0,g.width,g.height
end

You should also check for an alpha channel, if you don’t control the image source, and convert it to a masked image. (Convert it back when you’re done.)

How do I composite the image and what does that mean? I presume that means an image without the effects of the mask.

Also, about checking the alpha channel, is this what you meant?

  dim m as picture = p.mask(false)
  if not (m <> nil or p.HasAlphaChannel) then
    // create a mask
    m = p.mask(true)
    dim g as graphics = m.graphics
    g.forecolor = &c000000
    g.fillrect 0,0,g.width,g.height
  end

This is great what you are sharing, thanks.

By composite, I mean combine the image and mask into a non-masked image with the bits erased. You have 3 possibilities (you may or may not have to deal with them all, depending on where you get the images to begin with):

  1. The image already has a mask. Just use it as is and save it with its mask intact.
  2. The image has no alpha and no mask. Add a mask and then flatten it again when you save it.
  3. The image has an alpha channel. You have to convert it to an image with a mask and then convert it back to an alpha channel when you save it.

Just in case we aren’t hearing what you really want to do, here are a couple of interpretations of the posts I read above.
I recommend using the mask method for this work.

a/ You have an image and you want to erase some of it.

This is easy.
You keep the image in memory.
Erasing is a question of drawing on the image in white.
You paint the image to a canvas on screen in the paint event.

b/ You have an image and you want to erase some of it, with the possibility of putting the erased part back

Keep an off screen image
Give it a mask.
Start with a black mask
Erase by painting on the MASK in white
Draw the image to a canvas in the paint event of the canvas.
If you want to unerase, set the mask back to black.

c/ You have two images. Erasing one reveals the other (not erase but reveal, like a scratch card)
Thiis is the same as option b, but you have two offscreen images.
One is the background, the other is ‘on top’
In the paint event of a canvas, draw the background image, then the partially transparent image to the same canvas.

[quote=115316:@Jeff Tullin]Just in case we aren’t hearing what you really want to do, here are a couple of interpretations of the posts I read above.
I recommend using the mask method for this work.

a/ You have an image and you want to erase some of it.

This is easy.
You keep the image in memory.
Erasing is a question of drawing on the image in white.
You paint the image to a canvas on screen in the paint event.

b/ You have an image and you want to erase some of it, with the possibility of putting the erased part back

Keep an off screen image
Give it a mask.
Start with a black mask
Erase by painting on the MASK in white
Draw the image to a canvas in the paint event of the canvas.
If you want to unerase, set the mask back to black.

c/ You have two images. Erasing one reveals the other (not erase but reveal, like a scratch card)
Thiis is the same as option b, but you have two offscreen images.
One is the background, the other is ‘on top’
In the paint event of a canvas, draw the background image, then the partially transparent image to the same canvas.[/quote]
I think interpretation A is the closest. I think Tim might be understanding what I am trying to ask.

Screen capture

You cannot see the mouse on this screen recording but basically what I do is I select the different tools and then hold my mouse down on the canvas. When I rub out contents, it disallows me from drawing contents. I want to be able to erase but still have the ability to draw over areas that have been previously erased.

Thanks

When drawing paint the same area in the mask as black, when erasing paint the mask as white.

Agh, I am sorry for the confusion. I see you have answered my question Rick. This is what I was doing originally, I just thought it might be better not to draw black onto the mask like this. Oh well.

Thanks

For your needs , that’s the way.

:wink:

[quote=115393:@Rick Araujo]For your needs , that’s the way.

;)[/quote]
You have helped very much thanks. What is the most efficient way to draw a string completely black string? Thanks

For your needs, for each pixel, any color, showing in your canvas, you must have a black pixel in your mask. For each transparent (see through, erased) color in your canvas, you must have a white pixel in your mask.

So… If you want to write an opaque word in a masked canvas with transparency, you must repeat it in black in it’s mask.

[quote=115473:@Rick Araujo]For your needs, for each pixel, any color, showing in your canvas, you must have a black pixel in your mask. For each transparent (see through, erased) color in your canvas, you must have a white pixel in your mask.

So… If you want to write an opaque word in a masked canvas with transparency, you must repeat it in black in it’s mask.[/quote]
My problem is that the DrawString function does not just draw in pure black but has little variations. Thanks

You must be talking about anti-aliasing in the borders.
First thing, you must think in your erase routine. It must paint white in both canvas and mask to avoid some ghost effects of old images left in the canvas. The anti-alias will occur in the mask generating levels of gray that will reflect as levels of transparency in the border. I don’t imagine final bad effects.