C# 是否可以将图形旋转到其定义的一半?
我画了一些重复的画,每一幅画都做了很多工作。 我需要做的是将图形旋转到其定义的一半,如下所示:C# 是否可以将图形旋转到其定义的一半?,c#,drawing,rotatetransform,C#,Drawing,Rotatetransform,我画了一些重复的画,每一幅画都做了很多工作。 我需要做的是将图形旋转到其定义的一半,如下所示: using (Graphics g = Graphics.FromImage(bmp)) { //define area do pictureBox e preenche a branco Brush brush = new SolidBrush(Color.White); Rectangle area = ne
using (Graphics g = Graphics.FromImage(bmp))
{
//define area do pictureBox e preenche a branco
Brush brush = new SolidBrush(Color.White);
Rectangle area = new Rectangle(0, 0, 520, 520);
g.FillRectangle(brush, area);
//rotate
g.RotateTransform(some angle, some reference point)
//draw some more lines on the top of the rotated previous ones.
}
我尝试使用g.RotateTransform(90)
,因为Winforms中有这个函数,但它没有改变任何东西。为什么
有小费吗
编辑:如果有帮助,我只需要旋转固定角度,180º旋转变换肯定会更改后续的图形 请注意,在设置旋转点之前,通常需要一个
TranslateTransform
。但它“没有改变任何事情”肯定是错误的。再试一次!是的,您可以在任意点旋转(或缩放或移动)并将其向后移动/翻转,或完全重置图形
对象
是的,学习矩阵和多重变换也很有帮助
但是:您需要理解图形
对象不包含任何图形,这是一个常见的误解!它是在位图
上进行绘图的工具,通常是控件的表面
。因此旋转将发生,但仅限于您在之后绘制的东西:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(25, 25, 25, 25);
e.Graphics.TranslateTransform(25, 25);
e.Graphics.FillRectangle(Brushes.Red, rect);
for (int i = 0; i < 15; i++)
{
rect.Inflate(2, 2);
e.Graphics.TranslateTransform(5, 2);
e.Graphics.RotateTransform(2.5f);
e.Graphics.DrawRectangle(Pens.Blue, rect);
}
}
private void panel 1_Paint(对象发送器,PaintEventArgs e)
{
矩形rect=新矩形(25,25,25,25);
e、 图形学.翻译(25,25);
e、 图形。填充矩形(画笔。红色,矩形);
对于(int i=0;i<15;i++)
{
直接充气(2,2);
e、 图形学.译文(5,2);
e、 图形.旋转变换(2.5f);
e、 图形。DrawRectangle(Pens.Blue,rect);
}
}
试试这个:
使用以下参考资料:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
我制作了windows应用程序并放入form1 picturebox,这是form_load中的代码:
//Load an image in from a file
Bitmap pImage = new Bitmap(Environment.CurrentDirectory + @"\Image.bmp", true);
//Set our picture box to that image
pictureBox1.Image = (Bitmap)pImage.Clone();
//Store our old image so we can delete it
Image oldImage = pictureBox1.Image;
//Pass in our original image and return a new image rotated 35 degrees right
pictureBox1.Image = RotateImage(pImage, 270);
if (oldImage != null)
{
oldImage.Dispose();
}
然后使用image和rotation angle参数进行static函数,返回旋转后的图像,并从form_load调用,如前所述:
if (image == null)
{
throw new ArgumentNullException("image");
}
else
{
//create a new empty bitmap to hold rotated image
Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(rotatedBmp);
//move rotation point to center of image
g.TranslateTransform((float)image.Width / 2, (float)image.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)image.Width / 2, -(float)image.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(image, new PointF(0, 0));
return rotatedBmp;
}
您也可以通过使用ready RotateFlipType(枚举类型)直接使用表单_load中的代码,但这需要固定角度,如90270,。。。。但在前面的方法中,您可以使用任意整数值旋转图像:
private void Form1_Load(object sender, EventArgs e)
{
//Load an image in from a file
Bitmap pImage = new Bitmap(Environment.CurrentDirectory + @"\Image.bmp", true);
pImage.RotateFlip(RotateFlipType.Rotate90FlipXY);
pictureBox1.Image = pImage;
}
看见