A Bitmap fill style allows you to add a bitmap image to fill a shape. We’ll use a JPEG image to create a bitmap object and then add this bitmap object to the flash movie using BitmapFill style. We first need to load an image and convert it into Bitmap, and then we’ll have to convert it into FlashBitmap to be able to fill the shape. While creating FlashBitmap we’ll use FlashBitmapJpeg from the FlashBitmapType enumeration. You can do this with the help of the following steps:
- Define a FlashContainer object and set FrameRate and FrameSize properties
- Create a Bitmap object using an image file
- Create a FlashBitmap object from the Bitmap object define in previous step
- Create FillStyle array list and add FlashBitmap object as BitmapFill style
- Define a new rectangle shape object
- Create DefineShape2 object to apply FillStyle and LineStyle on the Shape object
- Create PlaceObject2 object to place DefineShape2 object on the display list
- Add ShowFrame object in the container and save the movie
The example below will show you how to use an image as BitmapFill style.
[C#]
//create flash container object
FlashContainer flash = new FlashContainer();
//set FrameRate and FrameSize values
flash.FrameRate = 35.0F;
flash.FrameSize = new Rect(0, 0, 4000, 4000);
//create a bitmap image from an image file
Image newImage = Image.FromFile("forest.jpg");
Bitmap bitmap = new Bitmap(newImage, newImage.Size);
int w = bitmap.Width;
int h = bitmap.Height;
//create a FlashBitmap image object from the bitmap and add it to the flash container
FlashBitmap defineImg = new FlashBitmap(flash.NewIdentifier(), FlashBitmapType.FlashBitmapJpeg, false, bitmap);
flash.Add(defineImg);
//create fill style array list
ArrayList FillStyle = new ArrayList();
MatrixTransform trnMatrix = new MatrixTransform(0, 0, 20, 20);
//add BitmapFill style in the fill styles array list
FillStyle.Add(new BitmapFill(FillStyleType.ClippedBitmapFill, defineImg.Id, trnMatrix));
//create an empty line styles array list
ArrayList LineStyle = new ArrayList();
int nWidth = 4000;
int nHeight = 4000;
int depth = 1;
//define a shape object and create a rectangle shape
Shape shape = new Shape();
//create style change record
StyleChangeRecord scr = new StyleChangeRecord(nWidth - 10, nHeight - 10);
//set fill style index
scr.OverlapFillStyle = 1;
//set line style index
scr.LineStyle = 1;
shape.Add(scr);
//create four straight edge records to define four edges of the rectangle
shape.Add(new StraightEdgeRecord(-(nWidth - 10), 0));
shape.Add(new StraightEdgeRecord(0, -(nHeight - 10)));
shape.Add(new StraightEdgeRecord(nWidth - 10, 0));
shape.Add(new StraightEdgeRecord(0, nHeight - 10));
//create a DefineShape2 type of object to define fill style and line style of the shapes
//along with the shape bounds
DefineShape2 defineShape =
new DefineShape2(flash.NewIdentifier(), new Rect(0, 0, nWidth, nHeight), FillStyle, LineStyle, shape);
flash.Add(defineShape);
//use PlaceObject2 to add the DefineShape2 object in the display list
PlaceObject2 po2 = new PlaceObject2(defineShape.Id, 1);
//add the place object 2 instance in the flash container
flash.Add(po2);
//add ShowFrame object to display the frame of the movie
flash.Add(new ShowFrame());
//save the output flash movie
flash.Write("AddImageToFlashMovie.swf");
[VB.NET]
'create flash container object
Dim flash As New FlashContainer()
'set FrameRate and FrameSize values
flash.FrameRate = 35.0F
flash.FrameSize = New Rect(0, 0, 4000, 4000)
'create a bitmap image from an image file
Dim newImage As Image = Image.FromFile("forest.jpg")
Dim bitmap As New Bitmap(newImage, newImage.Size)
Dim w As Integer = bitmap.Width
Dim h As Integer = bitmap.Height
'create a FlashBitmap image object from the bitmap and add it to the flash container
Dim defineImg As New FlashBitmap(flash.NewIdentifier(), FlashBitmapType.FlashBitmapJpeg, False, bitmap)
flash.Add(defineImg)
'create fill style array list
Dim FillStyle As New ArrayList()
Dim trnMatrix As New MatrixTransform(0, 0, 20, 20)
'add BitmapFill style in the fill styles array list
FillStyle.Add(New BitmapFill(FillStyleType.ClippedBitmapFill, defineImg.Id, trnMatrix))
'create an empty line styles array list
Dim LineStyle As New ArrayList()
Dim nWidth As Integer = 4000
Dim nHeight As Integer = 4000
Dim depth As Integer = 1
'define a shape object and create a rectangle shape
Dim shape As New Shape()
'create style change record
Dim scr As New StyleChangeRecord(nWidth - 10, nHeight - 10)
'set fill style index
scr.OverlapFillStyle = 1
'set line style index
scr.LineStyle = 1
shape.Add(scr)
'create four straight edge records to define four edges of the rectangle
shape.Add(New StraightEdgeRecord(-(nWidth - 10), 0))
shape.Add(New StraightEdgeRecord(0, -(nHeight - 10)))
shape.Add(New StraightEdgeRecord(nWidth - 10, 0))
shape.Add(New StraightEdgeRecord(0, nHeight - 10))
'create a DefineShape2 type of object to define fill style and line style of the shapes
'along with the shape bounds
Dim defineShape As New DefineShape2(flash.NewIdentifier(), New Rect(0, 0, nWidth, nHeight), FillStyle, LineStyle, shape)
flash.Add(defineShape)
'use PlaceObject2 to add the DefineShape2 object in the display list
Dim po2 As New PlaceObject2(defineShape.Id, 1)
'add the place object 2 instance in the flash container
flash.Add(po2)
'add ShowFrame object to display the frame of the movie
flash.Add(New ShowFrame())
'save the output flash movie
flash.Write("AddImageToFlashMovie.swf")
The output is a rectangle filled with an image as Bitmap fill.