简体   繁体   中英

How to move a certain part of a picture box using Visual Studio c#

I would like to only move a certain part of my picture box however I could move only that certain part. Currently I can move it from left to right but I only want to move the tip of it only.

    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");
    } 

A PS of the object that I want to tilt, I'm interested in fixating the bottom part meanwhile only moving the top http://imgur.com/H2ic2g7

I was really interested in your question and so I decided to spend some time to search more about image rotation in C#. So here goes the result.

    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");
    }

The credits for the logic goes to MusicMonkey5555 ( http://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations ). You can even download the source code from the link in case the above code isn't really clear. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM