public void MultiplyTransform_Matrix_SetsTransformToExpected(Matrix originalTransform, Matrix matrix, MatrixOrder matrixOrder) { try { using (var image = new Bitmap(10, 10)) using (var brush = new TextureBrush(image)) using (var expected = (Matrix)originalTransform.Clone()) { expected.Multiply(matrix, matrixOrder); brush.Transform = originalTransform; if (matrixOrder == MatrixOrder.Prepend) { TextureBrush clone = (TextureBrush)brush.Clone(); clone.MultiplyTransform(matrix); Assert.Equal(expected, clone.Transform); } brush.MultiplyTransform(matrix, matrixOrder); Assert.Equal(expected, brush.Transform); } } finally { originalTransform.Dispose(); matrix.Dispose(); } }
public void TranslateTransform_Invoke_SetsTransformToExpected(Matrix originalTransform, float dX, float dY, MatrixOrder matrixOrder) { try { using (var image = new Bitmap(10, 10)) using (var brush = new TextureBrush(image)) using (Matrix expected = originalTransform.Clone()) { expected.Translate(dX, dY, matrixOrder); brush.Transform = originalTransform; if (matrixOrder == MatrixOrder.Prepend) { TextureBrush clone = (TextureBrush)brush.Clone(); clone.TranslateTransform(dX, dY); Assert.Equal(expected, clone.Transform); } brush.TranslateTransform(dX, dY, matrixOrder); Assert.Equal(expected, brush.Transform); } } finally { originalTransform.Dispose(); } }
public void Clone() { TextureBrush t = new TextureBrush(image); TextureBrush clone = (TextureBrush)t.Clone(); Common(clone, t.WrapMode); }
public void Dispose_Clone() { TextureBrush t = new TextureBrush(image); t.Dispose(); Assert.Throws <ArgumentException> (() => t.Clone()); }
private void GenerateBackground(Graphics g, Size size) { TextureBrush bottom = (TextureBrush)bottomBorderBrush.Clone(); TextureBrush right = (TextureBrush)rightBorderBrush.Clone(); bottom.TranslateTransform(0, size.Height - bottom.Image.Height); right.TranslateTransform(size.Width - right.Image.Width, 0); g.FillRegion(mainBrush, new Region(new Rectangle(topLeft.Width, topLeft.Height, size.Width - topLeft.Width - bottomRight.Width, size.Height - topLeft.Height - bottomRight.Height))); //e.Graphics.DrawLine(texturePenMain, this.Width / 2, 0, this.Width / 2, this.Height); //top border g.FillRectangle(topBorderBrush, new Rectangle(topLeft.Width, 0, size.Width - topLeft.Width - topRight.Width, topRight.Height)); //bottom border g.FillRectangle(bottom, new Rectangle(bottomLeft.Width, size.Height - bottom.Image.Height, size.Width - bottomLeft.Width - bottomRight.Width, size.Height - bottom.Image.Height)); //left border g.FillRectangle(leftBorderBrush, new Rectangle(0, topLeft.Height, leftBorderBrush.Image.Width, size.Height - topLeft.Height - bottomLeft.Height)); //right border g.FillRectangle(right, new Rectangle(size.Width - right.Image.Width, topRight.Height, size.Width - right.Image.Width, size.Height - topRight.Height - bottomRight.Height)); //upper left corner g.DrawImage(topLeft, 0, 0); //upper right corner g.DrawImage(topRight, size.Width - topRight.Width, 0); //lower left corner g.DrawImage(bottomLeft, 0, size.Height - bottomLeft.Height); //lower right corner g.DrawImage(bottomRight, size.Width - topRight.Width, size.Height - bottomLeft.Height); }
public void Dispose_Clone() { TextureBrush t = new TextureBrush(image); t.Dispose(); t.Clone(); }
public void Clone_Disposed_ThrowsArgumentException() { using (var image = new Bitmap(10, 10)) { var brush = new TextureBrush(image); brush.Dispose(); AssertExtensions.Throws <ArgumentException>(null, () => brush.Clone()); } }
// Snippet for: M:System.Drawing.TextureBrush.Clone // <snippet1> public void Clone_Example(PaintEventArgs e) { // Create a TextureBrush object. TextureBrush tBrush = new TextureBrush(new Bitmap("texture.jpg")); // Create an exact copy of tBrush. TextureBrush cloneBrush = (TextureBrush)tBrush.Clone(); // Fill a rectangle with cloneBrush. e.Graphics.FillRectangle(cloneBrush, 0, 0, 100, 100); }
public void Clone_Invoke_Success() { using (var image = new Bitmap(10, 10)) using (var brush = new TextureBrush(image, WrapMode.Clamp)) { TextureBrush clone = Assert.IsType <TextureBrush>(brush.Clone()); Assert.NotSame(brush, clone); Assert.Equal(new Size(10, 10), brush.Image.Size); Assert.Equal(WrapMode.Clamp, clone.WrapMode); } }
/// <summary> /// Returns a <see cref="Brush"/> to draw from a reference image specifying the orientation, quality, style and effect. /// </summary> /// <param name="image">An <see cref="Image"/> base object to create the drawing brush.</param> /// <param name="rect">A <see cref="RectangleF"/> structure that represents the rectangle in which to paint.</param> /// <param name="style">One of the values in the enumeration <see cref="ImageStyle"/> that represents image style.</param> /// <param name="effect">One of the values in the enumeration <see cref="EffectType"/> that represents the type of effect to be applied.</param> /// <param name="orientation">One of the values in the enumeration <see cref="Orientation"/> that represents the orientation of the brush.</param> /// <param name="quality">One of the values in the enumeration <see cref="SmoothingModeEx"/> that represents the quality of presentation</param> /// <returns> /// Returns a <see cref="Brush"/> object reference that represents the drawing brush. /// </returns> public static Brush ToBrush(this Image image, RectangleF rect, ImageStyle style, EffectType effect, Orientation orientation, SmoothingModeEx quality) { if (image == null) { return(new SolidBrush(Color.Empty)); } Brush brush; TextureBrush tempTextureBrush = null; Image texture = image.Rotate(orientation).ApplyEffect(effect); using (var bitmap = new Bitmap(texture.Width, texture.Height)) using (var graphics = Graphics.FromImage(bitmap)) using (new Smoothing(graphics, quality)) { Rectangle dstRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); Rectangle srcRect = new Rectangle(Point.Empty, texture.Size); graphics.DrawImage(texture, dstRect, srcRect, GraphicsUnit.Pixel); try { tempTextureBrush = new TextureBrush(bitmap) { WrapMode = style.ToWrapMode() }; switch (style) { case ImageStyle.TopLeft: tempTextureBrush.TranslateTransform(rect.Left, rect.Top); break; case ImageStyle.TopMiddle: tempTextureBrush.TranslateTransform(rect.Left + (rect.Width - texture.Width) / 2, rect.Top); break; case ImageStyle.TopRight: tempTextureBrush.TranslateTransform(rect.Right - texture.Width, rect.Top); break; case ImageStyle.CenterLeft: tempTextureBrush.TranslateTransform(rect.Left, rect.Top + (rect.Height - texture.Height) / 2); break; case ImageStyle.CenterMiddle: tempTextureBrush.TranslateTransform(rect.Left + (rect.Width - texture.Width) / 2, rect.Top + (rect.Height - texture.Height) / 2); break; case ImageStyle.CenterRight: tempTextureBrush.TranslateTransform(rect.Right - texture.Width, rect.Top + (rect.Height - texture.Height) / 2); break; case ImageStyle.BottomLeft: tempTextureBrush.TranslateTransform(rect.Left, rect.Bottom - texture.Height); break; case ImageStyle.BottomMiddle: tempTextureBrush.TranslateTransform(rect.Left + (rect.Width - texture.Width) / 2, rect.Bottom - texture.Height); break; case ImageStyle.BottomRight: tempTextureBrush.TranslateTransform(rect.Right - texture.Width, rect.Bottom - texture.Height); break; case ImageStyle.Stretch: tempTextureBrush.TranslateTransform(rect.Left, rect.Top); tempTextureBrush.ScaleTransform(rect.Width / texture.Width, rect.Height / texture.Height); break; case ImageStyle.Tile: case ImageStyle.TileFlipX: case ImageStyle.TileFlipY: case ImageStyle.TileFlipXY: tempTextureBrush.TranslateTransform(rect.Left, rect.Top); break; } brush = (Brush)tempTextureBrush.Clone(); } finally { tempTextureBrush?.Dispose(); } } return(brush); }
/// <summary> /// 马路 /// </summary> /// <param name="g"></param> /// <param name="time"></param> private void Draw2(Graphics g, int time) { if (base.DrawVisible) { Matrix matrix1 = base.Transform.Matrix.Clone(); GraphicsContainer container1 = g.BeginContainer(); g.SmoothingMode = base.OwnerDocument.SmoothingMode; Matrix matrix2 = base.GraphTransform.Matrix.Clone(); base.GraphTransform.Matrix.Multiply(matrix1, MatrixOrder.Prepend); ClipAndMask.ClipPath.Clip(g, time, this); bool flag1 = base.Visible; if (!base.Visible) { g.SetClip(Rectangle.Empty); } float single1 = this.StrokeOpacity; if (this.svgAnimAttributes.ContainsKey("fill-opacity")) { single1 = Math.Min(single1, (float)this.svgAnimAttributes["fill-opacity"]); } ISvgBrush brush1 = this.GraphBrush; using (GraphicsPath path1 = (GraphicsPath)this.GPath.Clone()) { path1.Transform(base.GraphTransform.Matrix); if (!base.ShowBound) { float width1 = Width * GraphTransform.Matrix.Elements[0]; Stroke stroke = Stroke.GetStroke(this); Color color1 = Color.FromArgb(75, 75, 75); if (stroke.StrokeColor.ToArgb() != Color.Black.ToArgb()) { color1 = stroke.StrokeColor; } using (Pen p = new Pen(Color.FromArgb((int)(single1 * 255), color1))) { p.Width = width1; g.DrawPath(p, path1); } if (LineType == "3") { using (Pen p = new Pen(Color.Yellow)) { if (width1 > 30) { p.Width = 2; } else { p.Width = 1; } //p.DashPattern = new float[] { 10, 10 }; g.DrawPath(p, path1); } } else { //using (Pen p = new Pen(Color.Yellow)) { // if (width1 > 30) // p.Width = 2; // else // p.Width = 1; // g.DrawPath(p, path1); //} } if (LineType == "4") { using (Pen p = new Pen(Color.FromArgb((int)(single1 * 255), color1))) { p.Width = width1; float f22 = width1 / 4f; ImageAttributes imageAttributes = new ImageAttributes(); ColorMatrix cmatrix1 = new ColorMatrix(); cmatrix1.Matrix00 = 1f; cmatrix1.Matrix11 = 1f; cmatrix1.Matrix22 = 1f; cmatrix1.Matrix33 = single1;//透明度 cmatrix1.Matrix44 = 1f; //设置透明度 imageAttributes.SetColorMatrix(cmatrix1, ColorMatrixFlag.Default, ColorAdjustType.Default); if (BackgroundImage == null) { BackgroundImageFile = "road.png"; } TextureBrush tbush = new TextureBrush(BackgroundImage, new Rectangle(0, 0, BackgroundImage.Width, BackgroundImage.Height), imageAttributes); tbush.WrapMode = WrapMode.Tile; for (int i = 0; i < path1.PointCount - 1; i++) { float k = (path1.PathPoints[i + 1].Y - path1.PathPoints[i].Y) / (path1.PathPoints[i + 1].X - path1.PathPoints[i].X); float y1 = path1.PathPoints[i].Y - path1.PathPoints[i + 1].Y; float y2 = path1.PathPoints[i].X - path1.PathPoints[i + 1].X; float k2 = (float)Math.Abs(k); float angle = (float)Math.Atan(k2) * 180 / (float)Math.PI; if (k < 0) { angle = 360 - angle; } PointF[] pts = new PointF[] { new PointF(path1.PathPoints[i].X, path1.PathPoints[i].Y - 26) }; Matrix matrix11 = new Matrix(); matrix11.RotateAt(angle, path1.PathPoints[i]); matrix11.Translate(path1.PathPoints[i].X, path1.PathPoints[i].Y); matrix11.Scale(width1 / 50, width1 / 50); //tbush.ScaleTransform(width1 / 50, width1 / 50, MatrixOrder.Append); //tbush.RotateTransform(angle, MatrixOrder.Append); //tbush.TranslateTransform(path1.PathPoints[i].X, path1.PathPoints[i].Y , MatrixOrder.Append); tbush.Transform = matrix11; p.Brush = tbush.Clone() as TextureBrush; p.Alignment = PenAlignment.Center; g.DrawLine(p, path1.PathPoints[i], path1.PathPoints[i + 1]); tbush.ResetTransform(); } } if (BackgroundImageFile == "road.png") { using (Pen p = new Pen(Color.Yellow)) { if (width1 > 30) { p.Width = 2; } else { p.Width = 1; } g.DrawPath(p, path1); } } } } else { g.DrawPath(new Pen(base.BoundColor), path1); } this.DrawConnect(g); } matrix1.Dispose(); ClipAndMask.ClipPath.DrawClip(g, time, this); g.EndContainer(container1); this.pretime = time; } }