开发者

no error but cannot load image in windows form

the code is here,designer window has a button and a picture box!

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Title = "Open Image";
    dlg.Filter = "bmp files (*.bmp)|*.bmp";

    if (dlg.ShowDialog() == DialogResult.OK)
    {

        PictureBox PictureBox1 = new PictureBox();
        PictureBox1.Image = Image.FromFile(dlg.FileName);
        /* PictureBox1.Image = new Bitmap(dlg.FileName);

            // Add the new control to its parent's controls collection
            this.Controls.Add(PictureBox1);
            //dlg.Dispose();*/
    }
}

there is no error the window opens,when i press the button it opens directory ,then selected the image ,but it is not able to load the image in the window. the image im loading is 49.6 MB ,does t开发者_Go百科hat create any problem.


You've commented out the part where you added the picturebox to your window, id also suggest setting some elementary width/height of the picturebox so you can be sure it shows on screen.

Does it work with a much smaller test image?


Instead of creating the picture box on the fly, put it wherever you like in the Designer and set its Visible property to False.

Assuming you will name it PictureBox1 just assign its Image whenever the button is pressed, without creating any new picture box and in addition change its Visible to true:

PictureBox1.Image = Image.FromFile(dlg.FileName);
PictureBox1.Visible = true;

With your current code, the picture box gets default position of 0,0 which means top left corner of the window.


Try to

PictureBox.Image = new Bitmap(dlg.FileName);
PictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜