C#旋转图形的算法

whyabc 2009-03-31 04:30:42
我想用算法实现图形的旋转,不是用系统的,因为系统的不符合条件!
旋转的效果同PPT上的图形旋转相似,当鼠标放到图形上拖动时可以任意角度的旋转!
要求用C#实现!如果有用过DrawTools的,也可以交流一下!
...全文
945 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chadguo 2009-06-02
  • 打赏
  • 举报
回复
在一个画图面板上面,我写到可以旋转,但是旋转之后句柄计算就不对了
whyabc 2009-04-01
  • 打赏
  • 举报
回复
顶一下
whyabc 2009-03-31
  • 打赏
  • 举报
回复
这也是一种方法,但这样实现起来很麻烦的,我需要重新设计与写代码!
dyshadow 2009-03-31
  • 打赏
  • 举报
回复
不同的图形画在不同的BITMAP上,旋转的话就旋转自己的BITMAP,这个方法如何?
whyabc 2009-03-31
  • 打赏
  • 举报
回复
顶一下
whyabc 2009-03-31
  • 打赏
  • 举报
回复
这样当然是可以旋转图形,但我这的情况是一个画板上有多个图形,这样会不会让画板上的所有图形都旋转呢?
zgke 2009-03-31
  • 打赏
  • 举报
回复
晕~~我说算坐标..你真计算去了..


Graphics _Graphics = this.CreateGraphics();
GraphicsPath _Path =new GraphicsPath();
_Path.AddRectangle(new Rectangle(0,0,100,100));
Region _Region = new Region(_Path);

Matrix _Mtrx = new Matrix();
_Mtrx.Rotate(30); //30度
_Region.Transform(_Mtrx);

_Graphics.FillRegion(Brushes.Yellow, _Region);

使用 Matrix 和 Region 做做看
yeniao5203 2009-03-31
  • 打赏
  • 举报
回复
帮定
whyabc 2009-03-31
  • 打赏
  • 举报
回复
是呀,是需要坐标点!

rect是图形的坐标和大小

int left = rect.Left;
int right = rect.Right;
int bottom = rect.Bottom;
int top = rect.Top;
int x = right;
int y = top;

int xcenter = (right - left + 1) / 2 + left;
int ycenter = (bottom - top + 1) / 2 + top;

double sinA = Math.Cos(angle);
double cosA = Math.Sin(angle);

double xNew = (x - xcenter) * cosA + (y - ycenter) * sinA + xcenter;
double yNew = -(x - xcenter) * sinA + (y - ycenter) * cosA + ycenter;


rect.X = (int)Math.Round(xNew);
rect.Y = (int)Math.Round(yNew);
this.Refresh();

这样得到的好像不对!有没有高手指教一下!
zgke 2009-03-31
  • 打赏
  • 举报
回复
哪个可能需要计算坐标点了.
whyabc 2009-03-31
  • 打赏
  • 举报
回复
我说的意思是图形,不是图片!
zgke 2009-03-31
  • 打赏
  • 举报
回复
/// <summary>
/// 任意角度旋转
/// </summary>
/// <param name="bmp">原始图Bitmap </param>
/// <param name="angle">旋转角度 </param>
/// <param name="bkColor">背景色 </param>
/// <returns>输出Bitmap </returns>
public static Bitmap ImageRotate(Bitmap p_Image, float p_Angle, Color p_BlackColor)
{
int _Width = p_Image.Width + 2;
int _Height = p_Image.Height + 2;

PixelFormat _BitmapPixelFormat = PixelFormat.Format32bppArgb;

if (p_BlackColor != Color.Transparent) _BitmapPixelFormat = p_Image.PixelFormat;


Bitmap _OldBitmap = new Bitmap(_Width, _Height, _BitmapPixelFormat);
Graphics _Graphics = Graphics.FromImage(_OldBitmap);
_Graphics.Clear(p_BlackColor);
_Graphics.DrawImageUnscaled(p_Image, 1, 1);
_Graphics.Dispose();

GraphicsPath _Path = new GraphicsPath();
_Path.AddRectangle(new RectangleF(0f, 0f, _Width, _Height));
Matrix _Mtrx = new Matrix();
_Mtrx.Rotate(p_Angle);
RectangleF _Rect = _Path.GetBounds(_Mtrx);

Bitmap _NewBitmap = new Bitmap((int)_Rect.Width, (int)_Rect.Height, _BitmapPixelFormat);
_Graphics = Graphics.FromImage(_NewBitmap);
_Graphics.Clear(p_BlackColor);
_Graphics.TranslateTransform(-_Rect.X, -_Rect.Y);
_Graphics.RotateTransform(p_Angle);
_Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
_Graphics.DrawImageUnscaled(_OldBitmap, 0, 0);
_Graphics.Dispose();

_OldBitmap.Dispose();

return _NewBitmap;
}

抄别人的.

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧