使用未分配的变量,如何从另一个线程返回位图?

时间:2013-12-06 15:29:08

标签: c# multithreading bitmap

我制作了这个下载图像的脚本,但为了提高GUI响应能力,我想创建一个新线程但是如何将位图返回给GUI线程呢?

我的代码:

        try
        {
            Bitmap bit;

            var t = new Thread((ThreadStart)(() =>
            {
                string picUri = "";

                if (ONLINE_MODE_CHKBOX.Checked)
                {
                    picUri = "http://minecraft.aggenkeech.com/body.php?u=" + GetSessionId(true) + "&s=128&r=255&g=250&b=238";
                }
                else
                    picUri = "http://minecraft.aggenkeech.com/body.php?u=%USERNAME%&s=128&r=255&g=250&b=238";


                picUri = picUri.Replace("%USERNAME%", USERNAME_TXT.Text);

                // Create the requests.
                WebRequest requestPic = WebRequest.Create(picUri);

                WebResponse responsePic = requestPic.GetResponse();

                Image webImage = Image.FromStream(responsePic.GetResponseStream());

                Color red = Color.FromArgb(255, 255, 250, 238);

                bit = new Bitmap(webImage);

                bit.MakeTransparent(red);
            }));
            t.Start();

            SKIN_PICTURE_BOX.Image = bit; //<<<< Here it returns an error : Error, Use of unasigned local variable 'bit'

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

2 个答案:

答案 0 :(得分:1)

您可以定义一个公共事件并调用该事件从该线程触发该事件。您可以阅读更多如何通过here添加/使用事件。

public delegate void GetBitMapDelegate(Bitmap bit);
public event GetBitMapDelegate GetBitMap;

//Your method
try
{
    Bitmap bit;

    var t = new Thread((ThreadStart)(() =>
    {
        string picUri = "";

        if (ONLINE_MODE_CHKBOX.Checked)
        {
            picUri = "http://minecraft.aggenkeech.com/body.php?u=" + GetSessionId(true) + "&s=128&r=255&g=250&b=238";
        }
        else
            picUri = "http://minecraft.aggenkeech.com/body.php?u=%USERNAME%&s=128&r=255&g=250&b=238";


        picUri = picUri.Replace("%USERNAME%", USERNAME_TXT.Text);

        // Create the requests.
        WebRequest requestPic = WebRequest.Create(picUri);

        WebResponse responsePic = requestPic.GetResponse();

        Image webImage = Image.FromStream(responsePic.GetResponseStream());

        Color red = Color.FromArgb(255, 255, 250, 238);

        bit = new Bitmap(webImage);

        bit.MakeTransparent(red);
        GetBitMap(bit);
    }));
    t.Start();

    SKIN_PICTURE_BOX.Image = bit; //<<<< Here it returns an error : Error, Use of unasigned local variable 'bit'

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

当您调用生成线程的方法时,您将在Form类中使用此事件,您将获得该事件。

答案 1 :(得分:1)

如果您使用的是.Net 4.5,则可以使用async/await,这可以让您的生活更轻松。 (不需要事件,代表,“调用”等。)

this.BackgroundImage = await GetImageAsync("http://......");

async Task<Image> GetImageAsync(string url)
{
    using (var client = new HttpClient())
    {
        var bmp = (Bitmap)Bitmap.FromStream(await client.GetStreamAsync(url));

        Color transparent = Color.FromArgb(255, 255, 250, 238);
        bmp.MakeTransparent(transparent);

        return bmp;
    }
}