I am using OnPaint method in my class Class1 : Panel.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
}
to rotate and draw rectangle I am using
Matrix m = new Matrix();
m.RotateAt(90, rotationPoint);
g.Transform = m;
g.FillRectangle(Brushes.Black, rectangle)
the problem is, that rotation isn't working as I want it to.
Red square is rotation point and it's located in the middle-top of rectangle. How to set x, y and rotation point so rotation would work properly?
After rotating at 90 degress it should look like this
red pixel is still at the same location.
Rotation point is not the point, which you want to rotate. It is point, around which graphics is rotated. So if you draw a rectangle on the top of the graphics and want to rotate it (rectangle) - then you should set rotation point as center of graphics and rotate image to 90 degrees.
Here is example, that does almost what you want:
base.OnPaint(e);
var g = e.Graphics;
var width = g.VisibleClipBounds.Width;
var height = g.VisibleClipBounds.Height;
var rotationPoint = new PointF(width / 2, height / 2); ;
// draw center point
g.FillRectangle(Brushes.Red, new RectangleF(rotationPoint.X - 5, rotationPoint.Y - 5, 10, 10));
using (var path = new GraphicsPath())
{
var rectangle = new RectangleF((width - 10) / 2, 0, 10, 10);
var m = new Matrix();
m.RotateAt(90, rotationPoint);
path.AddRectangle(rectangle);
path.Transform(m);
// draw rotated point
g.FillPath(Brushes.Black, path);
}
Related
I have the following code which i wrote to try and rotate a bitmap(this is a test) the idea is to take a bitmap and rotate it by some amount of degrees and then draw it on the screen using win forms
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
Graphics g = this.CreateGraphics();
Bitmap b = new Bitmap(path);
g.Clear(Color.White);
imagePosition = Cursor.Position;
b = RotateImage(b, 45);
g.DrawImage(b, new Point(100, 100));
}
public Bitmap RotateImage(Bitmap b, float angle)
{
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
returnBitmap.SetResolution(b.HorizontalResolution, b.VerticalResolution);
Graphics g = Graphics.FromImage(returnBitmap);
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
g.RotateTransform(angle);
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}
this is the image before rotation
and this is the image after rotating 45 degrees like was shown in the code
The reason it's getting clipped is that there isn't enough space to display it rotated. The diagonal is longer than the sides (Pythagoras).
You need to make more space for the image, then it should display OK. How you do that will depend on what the image is contained in.
I have a shape, for example a Rectangle which has the following bounds:
X = 100
Y = 100
Width = 100
Height = 100
I apply the following rotation to this rectangle using a new Matrix:
X = 100
Y = 100
Angle = 45
var transform = new Matrix();
transform.RotateAt(angle, point);
So the new Matrix has the following value:
0.7071068, 0.7071067, -0.7071067, 0.7071068, 150, -62.13202
I use this Matrix when I draw the Rectangle with Graphic:
protected override void OnPaint(PaintEventArgs e)
{
...
e.Graphic.Transform = transform;
g.DrawRectangle(Pen, bounds.X, bounds.Y, bounds.Width, bounds.Height);
}
The problem is the following: at a certain point I need to draw the same Rectangle but shifted by a certain offset, for example (50, 50). I have stored the Matrix transform and the Rectangle bounds. If I change only the bounds (adding the offset) the new Rectangle will be drawn in a wrong position, probably due to the previous rotation point.
How I have to change the Matrix in order to draw my Rectangle in the "right" position? that is, how can I retreive the right rotation point and the old rotation angle?
Try to add translation to the matrix using MatrixOrder.Prepend.
For offset (50, 50) it'll be:
transform.Translate(50, 50, MatrixOrder.Prepend);
Or create a separate matrix for this case:
var transformWithOffset = new Matrix();
transformWithOffset.Translate(50, 50);
transformWithOffset.RotateAt(angle, point);
I need to draw the most perfect circle possible. EmguCV seems to lack an anti-aliasing option.
I'm trying to use SmoothGaussian, but the circle still does not look good/smooth enough.
Also, the circle line intensity should have Gaussian shape (i.e.: brighter in the center).
How can I achieve that?
Here is what I'm doing now:
using (Image<Gray, Byte> img = new Image<Gray, byte>(800, 800, new Gray(0)))
{
PointF center = new PointF(img.Width / 2, img.Height / 2);
//Center line
float r = 200.0f;
CircleF circle = new CircleF(center, r);
img.Draw(circle, new Gray(255), 1);
img._SmoothGaussian(7, 7, 3, 3);
}
If you use Brg, Brga or Gray color you can use that hack:
using(var graph = Graphics.FromImage(image.Bitmap))
{
graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graph.DrawEllipse(new Pen(color.BackColor), x, y, d`i`ameter, diameter);
}
`
Edit:
You can also use cvInvoke
var center = new Point(x, y);
var color = new Bgr(this.color.BackColor);
CvInvoke.cvCircle(image.Ptr, center, radius, color.MCvScalar, thickness, Emgu.CV.CvEnum.LINE_TYPE.CV_AA, 0);
`
I am writing a library that will create a graphihc path (In the shape of a polygon or rectangle) and rotate it but I want the rotated shape to be centered within its bounding rectangle. I have tried almost everything to get the shape to rotate on its center but it cutting off at the edges.
Below is some smaple code I have written to try to accomplish this
GraphicsPath path = new GraphicsPath();
path.AddPolygon(new Point[] { new Point(0, 0), new Point(100, 0), new Point(100, 100), new Point(0, 100) });
Matrix m = new Matrix();
Matrix m2 = new Matrix();
m2.Rotate(30);
m.Translate(-((path.GetBounds(m2).Width) / 2), -((path.GetBounds(m2).Height) / 2));
m.Rotate(30, MatrixOrder.Append);
m.Translate(((path.GetBounds(m2).Width)/2), ((path.GetBounds(m2).Height)/2), MatrixOrder.Append);
path.Transform(m);
Bitmap b = new Bitmap((int)path.GetBounds().Width, (int)path.GetBounds().Height);
Graphics g = Graphics.FromImage(b);
g.FillPath(Brushes.Blue, path);
b.Save(#"c:\aSSETS\sample3.png");
Please help. Thanks
I have one problem relating to rotate ellipse by given Center,
Suppose I have one ellipse and what should be is to rotate that ellipse by point given by user and ellipse should be rotate around that given point.
I have tried
g.RotateTransform(…)
g.TranslateTransform(…)
Code:
Graphics g = this.GetGraphics();
g.RotateTransform((float)degreeArg); //degree to rotate object
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);
this works fine but how can we give our out center to rotate ellipse....
How could it be possible please any buddy can suggest……
Thanks…….
RotateTransform always rotates about the origin. So you need to first translate your centre of rotation to the origin, then rotate, then translate it back.
Something like this:
Graphics g = this.GetGraphics();
g.TranslateTransform(300,300);
g.RotateTransform((float)degreeArg); //degree to rotate object
g.TranslateTransform(-300,-300);
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);
//center of the rotation
PointF center = new PointF(...);
//angle in degrees
float angle = 45.0f;
//use a rotation matrix
using (Matrix rotate = new Matrix())
{
//used to restore g.Transform previous state
GraphicsContainer container = g.BeginContainer();
//create the rotation matrix
rotate.RotateAt(angle, center);
//add it to g.Transform
g.Transform = rotate;
//draw what you want
...
//restore g.Transform state
g.EndContainer(container);
}