 |
|
| |
|
|
|
 |
|
This example shows how to matte one image onto another using an
alpha channel. It also demonstrates a number of other useful techniques.
|
|
|
|
|
|
|
| |
|
| |
|
|
We start by setting up variables pointing to the files we are going
to use.
[Visual Basic]
Dim imagefile, maskfile, bgfile, dstfile As String
imagefile = Server.MapPath("rez/image.bmp")
maskfile = Server.MapPath("rez/imagemask.bmp")
bgfile = Server.MapPath("rez/background.bmp")
dstfile = Server.MapPath("newfile.jpg")
[C#]
string imagefile = Server.MapPath("rez/image.bmp");
string maskfile = Server.MapPath("rez/imagemask.bmp");
string bgfile = Server.MapPath("rez/background.bmp");
string dstfile = Server.MapPath("newfile.jpg");
|
|
|
|
|
|
|
| |
|
| |
|
|
We create a Canvas object and draw our background onto it. Because
neither the height nor width of the Canvas was defined the Canvas
is resized to the natural size of the image.
[Visual Basic]
Dim canvas As New Canvas
canvas.DrawFile(bgfile, "")
[C#]
Canvas canvas = new Canvas();
canvas.DrawFile(bgfile, "");
|
|
|
|
|
|
|
| |
|
| |
|
|
Next we create a Canvas for the image we are going to draw onto
our background. There are two parts for this image - the visible
part and the alpha channel which defines a mask for the image. We
draw each of these pictures onto a Canvas and then insert the alpha
channel into our visible image. Finally we Clear the mask seeing
as we are not going to be using it again and it is occupying memory
which could be more valuably utilized.
[Visual Basic]
Dim src As New Canvas
Dim mask As New Canvas
src.DrawFile(imagefile, "")
mask.DrawFile(maskfile, "")
src.SetChannel("alpha", mask.Image)
mask.Clear()
[C#]
Canvas src = new Canvas();
Canvas mask = new Canvas();
src.DrawFile(imagefile, "");
mask.DrawFile(maskfile, "");
src.SetChannel("alpha", mask.Image);
mask.Clear();
|
|
|
|
|
|
|
| |
|
| |
|
|
We are going to draw five images matted into the background. To
do this we have to draw using transparent mode. The first image
is offset 30 pixels down and 30 to the right from the top left.
Because DrawCanvas returns the rectangle used for drawing each image
will be drawn offset from the last one. We end up with a diagonal
line of images stretching down from the top left of the Canvas.
[Visual Basic]
Dim pars As String
Dim i As Integer
Dim rect As New XRect
pars = "Mode=transparent Move=30,30 Rect="
rect.Right = src.Width
rect.Bottom = src.Height
For i = 1 To 5
rect.Rectangle = canvas.DrawCanvas(src.Image, pars +
rect.String)
Next
[C#]
XRect rect = new XRect();
string pars = "Mode=transparent Move=30,30 Rect=";
rect.Right = src.Width;
rect.Bottom = src.Height;
for (int i = 1; i <= 5; i++) {
rect.Rectangle = canvas.DrawCanvas(src.Image, pars +
rect.String);
}
|
|
|
|
|
|
|
| |
|
| |
|
|
Finally we save the Canvas out as a file.
[Visual Basic]
canvas.SaveAs(dstfile, "")
[C#]
canvas.SaveAs(dstfile, "");
|
|
|
|
|
|
|
|