-1

我需要一些帮助来解决一个真正让我头疼的问题。这是一个允许用户加载图像文件,然后将图像顺时针或逆时针旋转 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;
}

对于逆时针,只需将 else 的“90”更改为“-90” cwRotateTransform.Angle

4

1 回答 1

0

使用图片框和使用旋转翻转的位图对我来说效果很好:

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;
}
于 2013-02-13T18:17:52.920 回答