我需要一些真正让我头痛的问题的帮助。这是一个应用程序,允许用户加载图像文件,然后顺时针或逆时针旋转图像90度。代码工作正常,但是如果用户顺时针旋转图像然后决定通过逆时针将其旋转回初始位置,则图像不是应该的位置。因为我对c#很新,所以我不确定我错过了什么。下面是我用于顺时针旋转的代码。
private int cW = 0;
private void button6_Click(object sender, RoutedEventArgs e)
{
RotateTransform rotateTransform = new RotateTransform();
rotateTransform.CenterX = image1.ActualWidth / 2.0;
rotateTransform.CenterY = image1.ActualHeight / 2.0;
cW += 90;
rotateTransform.Angle = cW;
TransformGroup transformGroup = new TransformGroup();
transformGroup.Children.Add(rotateTransform);
image1.RenderTransform = transformGroup;
}
我使用if / else语句完全更改了代码。以下是我正在寻找的结果。
RotateTransform cwRotateTransform;
private void button6_Click(object sender, RoutedEventArgs e)
{
if (cwRotateTransform == null)
{
cwRotateTransform = new RotateTransform();
}
image1.LayoutTransform = cwRotateTransform;
if (cwRotateTransform.Angle == 360)
cwRotateTransform.Angle = 0;
else cwRotateTransform.Angle += 90;
}
对于逆时针方向,只需将“90”更改为“-90”,否则为cwRotateTransform.Angle
。
答案 0 :(得分:0)
使用带有rotateflip的图片框和位图,这对我来说很好用:
Bitmap bitmapPicturebox1;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.ImageLocation = @"C:\Wallpaper.jpg";
}
private void btnLeft_Click(object sender, EventArgs e)
{
bitmapPicturebox1 = new Bitmap(pictureBox1.Image);
bitmapPicturebox1.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox1.Image = bitmapPicturebox1;
}
private void btnRight_Click(object sender, EventArgs e)
{
bitmapPicturebox1 = new Bitmap(pictureBox1.Image);
bitmapPicturebox1.RotateFlip(RotateFlipType.Rotate270FlipNone);
pictureBox1.Image = bitmapPicturebox1;
}