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 ) {
76 public TextureBrush (Image image, Rectangle dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
77 // TBD: Implement ImageAttributes
80 public TextureBrush (Image image, RectangleF dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
81 // TBD: Implement ImageAttributes
84 public TextureBrush (Image image, WrapMode wrapMode, Rectangle dstRect) :
85 this( image, wrapMode, new RectangleF(dstRect.X, dstRect.Y, dstRect.Width, dstRect.Height )){
88 public TextureBrush (Image image, WrapMode wrapMode, RectangleF dstRect) {
89 // TBD: check if not metafile
90 _sourceRectangle = dstRect;
91 _texture = (Image)((Bitmap)image).Clone(dstRect, image.PixelFormat);
94 if (wrapMode != Drawing2D.WrapMode.Tile)
95 image = CreateWrappedImage(_texture, wrapMode);
99 _nativeObject = new awt.TexturePaint((image.BufferedImage)image.NativeObject.CurrentImage.NativeImage,
100 new geom.Rectangle2D.Float(0, 0, image.Width, image.Height));
105 #region CreateWrappedImage
107 private Image CreateWrappedImage(Image image, WrapMode wrapMode) {
112 case Drawing2D.WrapMode.TileFlipX :
113 b = new Bitmap(image.Width * 2, image.Height);
114 g = Graphics.FromImage( b );
115 g.DrawImage(image, new Matrix());
116 g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
118 case Drawing2D.WrapMode.TileFlipY :
119 b = new Bitmap(image.Width, image.Height * 2);
120 g = Graphics.FromImage( b );
121 g.DrawImage(image, new Matrix());
122 g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
124 case Drawing2D.WrapMode.TileFlipXY :
125 b = new Bitmap(image.Width * 2, image.Height * 2);
126 g = Graphics.FromImage( b );
127 g.DrawImage(image, new Matrix());
128 g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
129 g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
130 g.DrawImage(image, new Matrix(-1, 0, 0, -1, image.Width * 2 - 1, image.Height * 2 - 1));
132 case Drawing2D.WrapMode.Clamp :
133 // TBD: Implement WrapMode.Clamp
149 return (Image)_texture.Clone();
153 public Matrix Transform {
155 return BrushTransform;
158 BrushTransform = value;
162 public WrapMode WrapMode {
173 #region public methods
175 public override object Clone () {
176 TextureBrush copy = (TextureBrush)InternalClone();
178 if (_texture != null)
179 copy._texture = (Image)_texture.Clone();
184 public void MultiplyTransform (Matrix matrix) {
185 base.BrushMultiplyTransform( matrix );
188 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
189 base.BrushMultiplyTransform( matrix, order );
192 public void ResetTransform () {
193 base.BrushResetTransform();
196 public void RotateTransform (float angle) {
197 base.BrushRotateTransform( angle );
200 public void RotateTransform (float angle, MatrixOrder order) {
201 base.BrushRotateTransform( angle, order );
204 public void ScaleTransform (float sx, float sy) {
205 base.BrushScaleTransform( sx, sy );
208 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
209 base.BrushScaleTransform( sx, sy, order );
212 public void TranslateTransform (float dx, float dy) {
213 base.BrushTranslateTransform( dx, dy );
216 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
217 base.BrushTranslateTransform( dx, dy, order );