正在使用的Picturebox对象

时间:2016-04-05 08:18:25

标签: c# object picturebox

我试图制作一个旋转图片框的程序......问题是当我为while(true)代码启动一个新线程时,我得到“当前正在其他地方使用的对象”。你能帮我解决这个问题吗?

代码

private Bitmap MoveImageByDesiredAngle(int original_width, int original_height, int desiredAngle)
{

    Bitmap resultPicture = new Bitmap(original_width, original_height);
    Graphics g = Graphics.FromImage(resultPicture);
    g.TranslateTransform((float)original_width / 2, (float)original_height / 2);
    g.RotateTransform(desiredAngle);
    g.TranslateTransform(-(float)original_width / 2, -(float)original_height / 2);
    g.DrawImage(pictureBox1.Image, new Point(0, 0));
    return resultPicture;

} private void ButtonL_Click(object sender, EventArgs e)
    {
        thr = new Thread(new ThreadStart(handler));
        if(!thr.IsAlive)
        {
            itemsWrite = gWrite.AddItems(itemsWrite);
            itemsValue[0] = new Opc.Da.ItemValue(itemsWrite[0]) { Value = 0 };
            itemsValue[1] = new Opc.Da.ItemValue(itemsWrite[1]) { Value = 1 };
            gWrite.Write(itemsValue);
            ButtonR.BorderStyle = BorderStyle.None;
            ButtonL.BorderStyle = BorderStyle.Fixed3D;
            smer = "L";
            //thr = new Thread(new ThreadStart(handler));
            thr.Start();
        }
        else
        {
            thr.Abort(thr);
        }

    }

    private void ButtonR_Click(object sender, EventArgs e)
    {
        thr = new Thread(new ThreadStart(handler));

        if (!thr.IsAlive)
        {
            itemsWrite = gWrite.AddItems(itemsWrite);
            itemsValue[0] = new Opc.Da.ItemValue(itemsWrite[0]) { Value = 1 };
            itemsValue[1] = new Opc.Da.ItemValue(itemsWrite[1]) { Value = 0 };
            gWrite.Write(itemsValue);
            ButtonL.BorderStyle = BorderStyle.None;
            ButtonR.BorderStyle = BorderStyle.Fixed3D;
            smer = "R";
            //thr = new Thread(new ThreadStart(handler));
            thr.Start();
        }
        else 
        {
            thr.Abort();
        }

    }
void handler()
{
    if (smer == "R")
    {
        while (ButtonR.BorderStyle == BorderStyle.Fixed3D)
        {
            Bitmap resultPicture = MoveImageByDesiredAngle(pictureBox1.Image.Width, pictureBox1.Image.Height, 5);
            pictureBox1.Image = resultPicture;
            Thread.Sleep(50);
        }
    }
    if (smer == "L")
    {
        while (ButtonL.BorderStyle == BorderStyle.Fixed3D)
        {
            Bitmap resultPicture = MoveImageByDesiredAngle(pictureBox1.Image.Width, pictureBox1.Image.Height, -5);
            pictureBox1.Image = resultPicture;
            Thread.Sleep(50);
        }
    }
    else
    {
        label1.Text = "No direction";
    }

}

其中“smer”是描述旋转方向的字符串。一开始是smer =“0”;

0 个答案:

没有答案