Are there any picture mask examples?


I have been struggling with creating a picture with a mask and have not had any success. Are there any examples of how to do this that are current? i.e. using the ApplyMask method for a picture with an alpha channel rather than the old way setting transparent.

I’m trying to create a picture that has part of it transparent so when layering pictures in a canvas the bottom image will show through the transparent part of the other images on top. I have not found any examples yet and everything I have tried resulting from reading forum posts has not worked for me yet.

Thanks for any suggestions.

Exactly what are you attempting to accomplish?
What have you done, and in what way did it not produce the results you desire?

Basically a mask is pixels that match the image layer one for one, but are in shades of gray, where 0XFF is transparent, and 0x00 is opaque, and values inbetween are various levels of transparency.

I want to create a rectangle with a hole in it that allows an image/object drawn below to show through the hole. I want to be able to draw multiple pictures/objects in the canvas that have holes that when moved around allow the picture/object below to show through.

I’m trying to create the picture with a hole using a mask like this: (this is in a method that gets four parameters; total width and height and opening width and height)

[code] Dim mPic As Picture
Dim mMask As Picture
Dim x As Integer
Dim y As Integer

mPic = New Picture(matWidth, matHeight)

// draw the picture image
mPic.Graphics.ForeColor = RGB(255, 255, 255) // white
mPic.Graphics.FillRect( 0, 0, mPic.Width, mPic.Height )

mPic.Graphics.ForeColor = RGB(0, 0, 0)
mPic.Graphics.DrawRect( 0, 0, mPic.Width, mPic.Height )

x = (matWidth - openingWidth) / 2
y = (matHeight - openingHeight) / 2
mMask = New Picture(matWidth, matHeight)
mMask.Graphics.ForeColor = RGB(0, 0, 0)
mMask.Graphics.FillRect( x, y, openingWidth, openingHeight )


image = mPic

This code is in a Create method for an object that has a picture property called “image”. I have an array created where I can append all the created objects and then loop through and get the picture to draw.

This just gives me the white filled rectangle with black border. The “hole” does not show anything that is drawn below in the canvas when this picture is drawn. If I change the code and draw the black rectangle in mPic it shows the black rectangle where I want it. But if I try to use the black rectangle as a mask it does not work.

Should this work or do I not understand how drawing multiple pictures in a canvas works? Or where have I gone wrong? I have not been able to find any examples that use ApplyMask.


try this… a different approach

  Dim mPic As Picture
  Dim g as graphics
  Dim x As Integer
  Dim y As Integer
  mPic = New Picture(matWidth, matHeight,32) // Point at Main Image
  // draw the picture image
g.ForeColor = RGB(255, 255, 255) // white
  g.FillRect( 0, 0, mPic.Width, mPic.Height )
 g.ForeColor = RGB(0, 0, 0)
g .DrawRect( 0, 0, mPic.Width, mPic.Height )
// Draw Mask
// // Point at Mask

  x = (matWidth - openingWidth) / 2
  y = (matHeight - openingHeight) / 2
  g.ForeColor = RGB(0, 0, 0)
 g.FillRect( x, y, openingWidth, openingHeight )

  image = mPic

Thank you for the suggestion. I tried this but ran into an error. But rather than play around with this approach and using a mask I realized I could just clear the area of the picture that I want to see through using:

mPic.Graphics.ClearRect(x, y, openingWidth, openingHeight)
This seems to work for what I need. Thanks again.