This is my rectangle
protected void DrawRectangle(DrawingContext dc, Point point)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawContext = drawingVisual.RenderOpen())
{
Pen drawingPen = new Pen(ErrorBarBrush, ErrorBarThickness);
dc.DrawRectangle(Brushes.Red,
new Pen(Brushes.Black, 5),
new Rect(new Point(point.X - 50, point.Y + 50),
new Point(point.X + 50, point.Y - 50)));
dc.PushOpacity(2);
}
}
So my question is how do i set my opacity, is this right way to do it?
(This is changing the opacity of the Rectangle)
Instead of passing Brushes.Red into the Rectangle make a new SolidColorBrush and set the opacity of the SolidColorBrush you pass into the Rectangle
SolidColorBrush rectBrush = new SolidColorBrush(Colors.Red);
rectBrush.Opacity = 0.5; // or whatever
dc.DrawRectangle(rectBrush, ...
You'll need to do a similar thing for the Pen
Simply
drawingVisual.Opacity = 0.5;
Related
I would like to know how can I draw an arc in C# , I'm trying using DrawEllipse but it doesn't work and it give wrong drawing.
However, I have searched for a method to draw an arc in the Class DrawingContext but I didn't find it.
DrawingVisual d = new DrawingVisual();
System.Windows.Media.Pen pen = new System.Windows.Media.Pen();
DrawingContext drawingContext = d.RenderOpen();
pen.Brush = System.Windows.Media.Brushes.Black;
System.Windows.Point center = new System.Windows.Point();
center.X = 0.4;
center.Y = 0.5;
drawingContext.DrawEllipse(System.Windows.Media.Brushes.White, pen, center, 4,4);
drawingContext.Close();
canvas.Children.Add(new VisualHost { Visual = d });
You would have to draw a PathGeometry or a StreamGeometry that contains an arc segment, like the following circular arc of radius 100 from (100,100) to (200,200):
var visual = new DrawingVisual();
var pen = new Pen(Brushes.Black, 1);
using (var dc = visual.RenderOpen())
{
var figure = new PathFigure
{
StartPoint = new Point(100, 100) // start point
};
figure.Segments.Add(new ArcSegment
{
Point = new Point(200, 200), // end point
Size = new Size(100, 100), // radii
SweepDirection = SweepDirection.Clockwise
});
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
dc.DrawGeometry(null, pen, geometry);
}
Is there a way to create non-rectangular pictureBoxes. I have round shapes that should overlap and if possible should be in different pictureboxes..
I tried the this here, but you could not see both pictureboxes that are overlapping at a time, but just one..
Here the picture that resulted from my tests:
You can make the PictureBoxes circular by adding an Ellipse to a GraphicsPath and then building a new Region from it.
Here's a quick example:
public class Target : PictureBox
{
public Target()
{
this.Size = new Size(100, 100);
this.Paint += Target_Paint;
Rectangle rc = this.ClientRectangle;
rc.Inflate(-10, -10);
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(rc);
this.Region = new Region(gp);
}
void Target_Paint(object sender, PaintEventArgs e)
{
Rectangle rc = this.ClientRectangle;
rc.Inflate(-10, -10);
using (Pen pen = new Pen(Color.Blue, 5))
{
e.Graphics.DrawEllipse(pen, rc);
}
rc = new Rectangle(new Point(this.Size.Width / 2, this.Size.Height / 2), new Size(1, 1));
rc.Inflate(9, 9);
e.Graphics.FillEllipse(Brushes.Red, rc);
}
}
After compiling, the new control appears at the top of your ToolBox.
Here's a screenshot with three of them overlapping each other:
I have got a System.Drawing.Bitmap in my code.
The width is fix, the height varies.
What I want to do, is to add a white border around the bitmap, with about 20 pixel, to all 4 edges.
How would this work?
You could draw a rectangle behind the bitmap. The width of the rectangle would be (Bitmap.Width + BorderWidth * 2), and the position would be (Bitmap.Position - new Point(BorderWidth, BorderWidth)). Or at least that's the way I'd go about it.
EDIT:
Here is some actual source code explaining how to implement it (if you were to have a dedicated method to draw an image):
private void DrawBitmapWithBorder(Bitmap bmp, Point pos, Graphics g) {
const int borderSize = 20;
using (Brush border = new SolidBrush(Color.White /* Change it to whichever color you want. */)) {
g.FillRectangle(border, pos.X - borderSize, pos.Y - borderSize,
bmp.Width + borderSize, bmp.Height + borderSize);
}
g.DrawImage(bmp, pos);
}
You can use 'SetPixel' method of a Bitmap class, to set nesessary pixels with the color. But more convenient is to use 'Graphics' class, as shown below:
bmp = new Bitmap(FileName);
//bmp = new Bitmap(bmp, new System.Drawing.Size(40, 40));
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 0), new Point(0, 40));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 0), new Point(40, 0));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 40), new Point(40, 40));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(40, 0), new Point(40, 40));
Below function will add border around the bitmap image. Original image will increase in size by the width of border.
private static Bitmap DrawBitmapWithBorder(Bitmap bmp, int borderSize = 10)
{
int newWidth = bmp.Width + (borderSize * 2);
int newHeight = bmp.Height + (borderSize * 2);
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics gfx = Graphics.FromImage(newImage))
{
using (Brush border = new SolidBrush(Color.White))
{
gfx.FillRectangle(border, 0, 0,
newWidth, newHeight);
}
gfx.DrawImage(bmp, new Rectangle(borderSize, borderSize, bmp.Width, bmp.Height));
}
return (Bitmap)newImage;
}
The following code will draw an ellipse on an image and fill that ellipse with the Tomato colour
string imageWithTransEllipsePathToSaveTo = "~/Images/imageTest.png";
Graphics g = Graphics.FromImage(sourceImage);
g.FillEllipse(Brushes.Tomato, 50, 50, 200, 200);
sourceImage.Save(Server.MapPath(imageWithTransEllipsePathToSaveTo), ImageFormat.Png);
If I change the brush to Transparent it obviously will not show because the ellipse will be transparent and the image underneath will show.
How do I set the 'background' of the ellipse to be transparent so that the image contains a transparent spot?
EDIT:
Sorry for the confusion but like this...
This is my second answer and works with an Image instead of a color brush. Unfortunately there is no RadialImageBrush (known to me). I've included code to save the image to the disk, and included usings to ensure you import the correct components. This does use WPF but it should work as part of a library or console app.
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Shapes;
namespace WpfApplication30
{
class ImageEditor
{
public static void processImage(string loc)
{
ImageSource ic = new BitmapImage(new Uri(loc, UriKind.Relative));
ImageBrush brush = new ImageBrush(ic);
Path p = new Path();
p.Fill = brush;
CombinedGeometry cb = new CombinedGeometry();
cb.GeometryCombineMode = GeometryCombineMode.Exclude;
EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 5, 5);
RectangleGeometry rect = new RectangleGeometry(new Rect(new Size(100, 100)));
cb.Geometry1 = rect;
cb.Geometry2 = ellipse;
p.Data = cb;
Canvas inkCanvas1 = new Canvas();
inkCanvas1.Children.Add(p);
inkCanvas1.Height = 96;
inkCanvas1.Width = 96;
inkCanvas1.Measure(new Size(96, 96));
inkCanvas1.Arrange(new Rect(new Size(96, 96)));
RenderTargetBitmap targetBitmap =
new RenderTargetBitmap((int)inkCanvas1.ActualWidth,
(int)inkCanvas1.ActualHeight,
96d, 96d,
PixelFormats.Default);
targetBitmap.Render(inkCanvas1);
using (System.IO.FileStream outStream = new System.IO.FileStream( loc.Replace(".png","Copy.png"), System.IO.FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
encoder.Save(outStream);
}
}
}
}
Here is the result:
You need to create a brush using a semi-transparent color.
You do that with Color.FromArgb(alpha, r, g, b), where alpha sets the opacity.
Example copied from MSDN:
public void FromArgb1(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Transparent red, green, and blue brushes.
SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(120, 0, 255, 0));
SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(120, 0, 0, 255));
// Base and height of the triangle that is used to position the
// circles. Each vertex of the triangle is at the center of one of the
// 3 circles. The base is equal to the diameter of the circles.
float triBase = 100;
float triHeight = (float)Math.Sqrt(3*(triBase*triBase)/4);
// Coordinates of first circle's bounding rectangle.
float x1 = 40;
float y1 = 40;
// Fill 3 over-lapping circles. Each circle is a different color.
g.FillEllipse(trnsRedBrush, x1, y1, 2*triHeight, 2*triHeight);
g.FillEllipse(trnsGreenBrush, x1 + triBase/2, y1 + triHeight,
2*triHeight, 2*triHeight);
g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2*triHeight, 2*triHeight);
}
You need to use a RadialGradientBrush:
RadialGradientBrush b = new RadialGradientBrush();
b.GradientOrigin = new Point(0.5, 0.5);
b.Center = new Point(0.5, 0.5);
b.RadiusX = 0.5;
b.RadiusY = 0.5;
b.GradientStops.Add(new GradientStop(Colors.Transparent,0));
b.GradientStops.Add(new GradientStop(Colors.Transparent,0.25));
b.GradientStops.Add(new GradientStop(Colors.Tomato, 0.25));
g.FillEllipse(b, 50, 50, 200, 200);
Am using the below code to create polygon. i just want to fill this polygon surface with black dots, how i can do that, then i want to convert this polygon to bitmap or in memory stream, how to do this??
// Create a blue and a black Brush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Transparent;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
// Create a Polygon
Polygon yellowPolygon = new Polygon();
yellowPolygon.Stroke = blackBrush;
yellowPolygon.Fill = yellowBrush;
yellowPolygon.StrokeThickness = 4;
// Create a collection of points for a polygon
System.Windows.Point Point1 = new System.Windows.Point(50, 100);
System.Windows.Point Point2 = new System.Windows.Point(200, 100);
System.Windows.Point Point3 = new System.Windows.Point(200, 200);
System.Windows.Point Point4 = new System.Windows.Point(300, 30);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
polygonPoints.Add(Point4);
// Set Polygon.Points properties
yellowPolygon.Points = polygonPoints;
// Add Polygon to the page
mygrid.Children.Add(yellowPolygon);
Do the dots have to be positioned in a particular order or do you just want to have a dotted pattern in your polygon without specific order?
If you don't need a special order you could use a Brush, a DrawingBrush for instance. Check out this link: http://msdn.microsoft.com/en-us/library/aa970904.aspx
You can then set this Brush as the Fill-Property of your Polygon instead of the SolidColorBrush.
This is the DrawingBrush example from the msdn link, but modified to display dots:
// Create a DrawingBrush and use it to
// paint the rectangle.
DrawingBrush myBrush = new DrawingBrush();
GeometryDrawing backgroundSquare =
new GeometryDrawing(
Brushes.Yellow,
null,
new RectangleGeometry(new Rect(0, 0, 100, 100)));
GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new EllipseGeometry(new Rect(0, 0, 20, 20)));
SolidColorBrush checkerBrush = new SolidColorBrush(Colors.Black);
GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);
DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);
myBrush.Drawing = checkersDrawingGroup;
myBrush.Viewport = new Rect(0, 0, 0.05, 0.05);
myBrush.TileMode = TileMode.Tile;
yellowPolygon.Fill = myBrush;