0

我只想移动图片框的某个部分,但我只能移动那个部分。目前我可以从左到右移动它,但我只想移动它的尖端。

    private float angle = 0.0f;
    Image image;

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right) angle += 1;
        else if (e.KeyCode == Keys.Left) angle -= 1;

        int a = pictureBox1.Location.X;
        int b = pictureBox1.Location.Y;

        if (e.KeyCode == Keys.Right) a += 5;
        else if (e.KeyCode == Keys.Left) a -= 5;

        pictureBox2.Location = new Point(a, b);

        RotateImage(pictureBox1, image, angle);
    }

    private void RotateImage(PictureBox pb, Image img, float angle)
    {
        //Store our old image so we can delete it
        Image oldImage = pb.Image;
        pb.Image = RotateImage(img, angle);

        if (oldImage != null)
            oldImage.Dispose();
    }

    public static Bitmap RotateImage(Image image, float angle)
    {
        return RotateImageFinal(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
    }

    public static Bitmap RotateImageFinal(Image image, PointF offset, float angle)
    {
        //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);

        //Put the rotation point in the center of the image
        g.TranslateTransform(offset.X, offset.Y);

        //rotate the image
        g.RotateTransform(angle);

        //move the image back
        g.TranslateTransform(-offset.X, -offset.Y);

        //draw passed in image onto graphics object
        g.DrawImage(image, new PointF(0, 0));

        return rotatedBmp;
    }

    private void Form1_Load(object sender, EventArgs e)
    {   //Just to store the address of the 'arrow' image.
        string path = Directory.GetCurrentDirectory();
        image = new Bitmap(path + "/img/arrow.bmp");
    } 

我想倾斜的对象的 PS,我有兴趣固定底部同时只移动顶部 http://imgur.com/H2ic2g7

4

1 回答 1

0

我真的对你的问题很感兴趣,所以我决定花一些时间来搜索更多关于 C# 中图像旋转的信息。所以结果出来了。

    private float angle = 0.0f;
    Image image;

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right) angle+=1;
        else if (e.KeyCode == Keys.Left) angle-=1;

        int a = ballPB.Location.X;
        int b = ballPB.Location.Y;

        if (e.KeyCode == Keys.Right) a += 5;
        else if (e.KeyCode == Keys.Left) a -= 5;

        ballPB.Location = new Point(a, b);

        RotateImage(arrowPB, image, angle);
    }

    private void RotateImage(PictureBox pb, Image img, float angle)
    {
        //Store our old image so we can delete it
        Image oldImage = pb.Image;
        pb.Image = RotateImage(img, angle);

        if (oldImage != null)
            oldImage.Dispose();
    }

    public static Bitmap RotateImage(Image image, float angle)
    {
        return RotateImageFinal(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
    }

    public static Bitmap RotateImageFinal(Image image, PointF offset, float angle)
    {
        //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);

        //Put the rotation point in the center of the image
        g.TranslateTransform(offset.X, offset.Y);

        //rotate the image
        g.RotateTransform(angle);

        //move the image back
        g.TranslateTransform(-offset.X, -offset.Y);

        //draw passed in image onto graphics object
        g.DrawImage(image, new PointF(0, 0));

        return rotatedBmp;
    }

    private void Form1_Load(object sender, EventArgs e)
    {   //Just to store the address of the 'arrow' image.
        string path = Directory.GetCurrentDirectory();
        image = new Bitmap(path + "\\img\\arrow.png");
    }

该逻辑的功劳归于 MusicMonkey5555 ( http://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations )。如果上面的代码不是很清楚,您甚至可以从链接下载源代码。祝你好运!

于 2013-05-21T10:56:36.257 回答