无法在c#中将我的图片加载到图片框

时间:2013-11-29 15:49:00

标签: c# bitmap

我正在尝试将图片加载到c#中的图片框中,但有时它会让我异常,有时它可以正常工作但不会在图片框上显示图片。 这是我的代码

try
{
    Bitmap bmp = new Bitmap("C:\\Users\\digit\\Desktop\\C.png");
    picBoxMain.Image = bmp;
    picBoxMain.Visible = true;
    InitializeComponent();
}
catch(Exception ex)
{
    MessageBox.Show("Error "+ex.Message);
}

2 个答案:

答案 0 :(得分:1)

首先,我首先放置InitliazeComponent方法(我假设这是一个构造函数)

try
{
    InitializeComponent();
    Bitmap bmp = new Bitmap("C:\\Users\\digit\\Desktop\\C.png");
    picBoxMain.Image = bmp;
    picBoxMain.Visible = true;

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

第二次 - 检查图片是否确实存在该路径(您可以在代码中执行此操作 - 使用File.Exists path))。

第三次 - 检查图像是否有透明度:可能图片框的尺寸太小而且您正在显示透明部分。

答案 1 :(得分:0)

您的代码应该在InitializeComponent();

之后

试试这个..

    try
    {
        InitializeComponent();
        Bitmap bmp = new Bitmap("C:\\Users\\digit\\Desktop\\C.png");
        picBoxMain.Image = bmp;
        picBoxMain.Visible = true;

    }
    catch(Exception ex)
    {
        MessageBox.Show("Error "+ex.Message);
    }
相关问题