2 // System.Drawing.TextureBrush.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Ravindra (rkumar@novell.com)
8 // (C) 2002 Ximian, Inc
9 // (C) 2004 Novell, Inc.
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Drawing.Drawing2D;
37 using System.Drawing.Imaging;
40 using geom = java.awt.geom;
41 using image = java.awt.image;
43 namespace System.Drawing {
45 /// Summary description for TextureBrush.
47 public sealed class TextureBrush : Brush {
48 readonly awt.TexturePaint _nativeObject;
49 RectangleF _sourceRectangle;
50 Image _texture = null;
53 protected override java.awt.Paint NativeObject {
61 public TextureBrush (Image image) : this (image, WrapMode.Tile) {
64 public TextureBrush (Image image, WrapMode wrapMode) :
65 this( image, wrapMode, new RectangleF(0, 0, image.Width, image.Height )){
68 public TextureBrush (Image image, Rectangle dstRect) :
69 this( image, WrapMode.Tile, dstRect ) {
72 public TextureBrush (Image image, RectangleF dstRect) :
73 this( image, WrapMode.Tile, dstRect ) {
77 public TextureBrush (Image image, Rectangle dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
78 // TBD: Implement ImageAttributes
82 public TextureBrush (Image image, RectangleF dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
83 // TBD: Implement ImageAttributes
86 public TextureBrush (Image image, WrapMode wrapMode, Rectangle dstRect) :
87 this( image, wrapMode, new RectangleF(dstRect.X, dstRect.Y, dstRect.Width, dstRect.Height )){
91 public TextureBrush (Image image, WrapMode wrapMode, RectangleF dstRect) {
92 // TBD: check if not metafile
93 _sourceRectangle = dstRect;
94 _texture = (Image)((Bitmap)image).Clone(dstRect, image.PixelFormat);
97 if (wrapMode != Drawing2D.WrapMode.Tile)
98 image = CreateWrappedImage(_texture, wrapMode);
102 _nativeObject = new awt.TexturePaint((image.BufferedImage)image.NativeObject.CurrentImage.NativeImage,
103 new geom.Rectangle2D.Float(0, 0, image.Width, image.Height));
108 #region CreateWrappedImage
110 private Image CreateWrappedImage(Image image, WrapMode wrapMode) {
115 case Drawing2D.WrapMode.TileFlipX :
116 b = new Bitmap(image.Width * 2, image.Height);
117 g = Graphics.FromImage( b );
118 g.DrawImage(image, new Matrix());
119 g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
121 case Drawing2D.WrapMode.TileFlipY :
122 b = new Bitmap(image.Width, image.Height * 2);
123 g = Graphics.FromImage( b );
124 g.DrawImage(image, new Matrix());
125 g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
127 case Drawing2D.WrapMode.TileFlipXY :
128 b = new Bitmap(image.Width * 2, image.Height * 2);
129 g = Graphics.FromImage( b );
130 g.DrawImage(image, new Matrix());
131 g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
132 g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
133 g.DrawImage(image, new Matrix(-1, 0, 0, -1, image.Width * 2 - 1, image.Height * 2 - 1));
135 case Drawing2D.WrapMode.Clamp :
136 // TBD: Implement WrapMode.Clamp
152 return (Image)_texture.Clone();
156 public Matrix Transform {
158 return BrushTransform;
161 BrushTransform = value;
166 public WrapMode WrapMode {
177 #region public methods
179 public override object Clone () {
180 TextureBrush copy = (TextureBrush)InternalClone();
182 if (_texture != null)
183 copy._texture = (Image)_texture.Clone();
188 public void MultiplyTransform (Matrix matrix) {
189 base.BrushMultiplyTransform( matrix );
192 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
193 base.BrushMultiplyTransform( matrix, order );
196 public void ResetTransform () {
197 base.BrushResetTransform();
200 public void RotateTransform (float angle) {
201 base.BrushRotateTransform( angle );
204 public void RotateTransform (float angle, MatrixOrder order) {
205 base.BrushRotateTransform( angle, order );
208 public void ScaleTransform (float sx, float sy) {
209 base.BrushScaleTransform( sx, sy );
212 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
213 base.BrushScaleTransform( sx, sy, order );
216 public void TranslateTransform (float dx, float dy) {
217 base.BrushTranslateTransform( dx, dy );
220 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
221 base.BrushTranslateTransform( dx, dy, order );