gpt4 book ai didi

c# winforms 如何旋转图像(但不围绕其中心旋转)

转载 作者:行者123 更新时间:2023-11-30 15:38:58 24 4
gpt4 key购买 nike

我正在尝试开发我几乎已经完成的节拍器应用程序。

我唯一剩下的部分是辅助视觉,它要求我及时前后旋转 ARM 的图像(想想时钟上的分针)。

如何旋转图像而不是围绕其中心点旋转图像。在我正在编写的场景中,我需要能够围绕图像底部边框中间的一个点旋转图像。

此外,我希望能够在 n 毫秒内将图像从 X 旋转到 Y,并让它在动画中缓入缓出。对于 C# 来说,这是一座太远的桥梁吗?是否有任何库可以帮助我实现这种先进的物理动画类型。

非常感谢

最佳答案

扩展回复

没有任何闪烁。 ctor 中的 SetStyle 负责处理这个问题。您所看到的“闪烁”是由三个因素造成的假象:

  1. 更新速率仅为 10 次/秒。尝试将其增加到 20 或 30。
  2. 方向值为航向。它应该基于时间/加速度。这是留给你的练习。
  3. 蹩脚的“手”图像具有坚硬的锯齿边缘。无论您更新多快或制作动画多流畅,它都会看起来很紧张。同样,进行抗锯齿、混合图形处理留作练习。

仔细看代码。这不是“画在窗体上”,它是一个自定义控件。看看 MetronomeControl 是如何派生自 Control 的?看看我们是如何通过向其添加 MetronomeControl 来创建表单的?

图片框是用来显示静态图片的,不是用来自定义控件的!

它更新的方式是创建一个计时器。当计时器的 Tick 事件触发时,我们更新角度和方向,更一般地说,我们更新控件的状态。对 Invalidate 的调用告诉操作系统,“嘿,我需要重新绘制,方便时给我发送 WM_PAINT 消息!”。我们的 OnPaint 覆盖只是绘制控件的当前状态。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

class MetronomeControl : Control
{
private Bitmap hand;
private float angle = 0;
private float direction = 2;
private Timer timer = new Timer { Enabled = true, Interval = 30 };

public MetronomeControl()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.Opaque | ControlStyles.AllPaintingInWmPaint, true);
hand = CreateCrappyHandBitmap();
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{
if (angle < -45 || angle > 45)
direction = -direction;
angle += direction;
Invalidate();
}

private static Bitmap CreateCrappyHandBitmap()
{
Bitmap bitmap = new Bitmap(100, 300, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.Transparent);
graphics.FillRectangle(Brushes.LightGray, 50 - 5, 0, 10, 300);
graphics.FillPolygon(Brushes.LightSlateGray, new Point[] { new Point(50 - 30, 40), new Point(50 + 30, 40), new Point(50 + 20, 80), new Point(50 - 20, 80) });
graphics.FillEllipse(Brushes.LightSlateGray, 0, 200, 100, 100);
}
return bitmap;
}

protected override void OnPaint(PaintEventArgs e)
{
// Erase background since we specified AllPaintingInWmPaint
e.Graphics.Clear(Color.AliceBlue);

e.Graphics.DrawString(Text, Font, Brushes.Black, new RectangleF(0, 0, ClientSize.Width, ClientSize.Height), new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

// Move the 0,0 point to the just below the bottom-center of our client area
e.Graphics.TranslateTransform(ClientSize.Width / 2, ClientSize.Height + 40);
// Rotate around this new 0,0
e.Graphics.RotateTransform(angle);
// Turn on AA to make it a bit less jagged looking
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Draw the image so that the center of the ellipse is at 0,0
e.Graphics.DrawImage(hand, 0 - hand.Width / 2, 0 - hand.Height + 50);

// Reset the transform for other drawing
e.Graphics.ResetTransform();

base.OnPaint(e);
}
}

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form
{
Text = "Metronome Control Demo",
ClientSize = new Size(640, 480),
Controls =
{
new MetronomeControl
{
Location = new Point(10, 10),
Size = new Size (340, 300),
Font = new Font(FontFamily.GenericSansSerif, 24),
Text = "Metronome Control Demo",
}
}
});
}
}

关于c# winforms 如何旋转图像(但不围绕其中心旋转),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com