没有错误,但无法在 Windows 窗体中加载图像
代码在这里,设计器窗口有一个按钮和一个图片框!
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();*/
}
}
窗口打开没有错误,当我按下按钮时,它打开目录,然后选择图像,但它无法在窗口中加载图像。我加载的图像为 49.6 MB,这会产生任何问题吗?
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 that create any problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经注释掉了将图片框添加到窗口的部分,我还建议设置图片框的一些基本宽度/高度,以便您可以确保它显示在屏幕上。
它适用于更小的测试图像吗?
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?
不要即时创建图片框,而是将其放置在设计器中您喜欢的任何位置,并将其 Visible 属性设置为 False。
假设您将其命名为
PictureBox1
,只需在按下按钮时分配其图像,而不创建任何新的图片框,并将其 Visible 更改为true
:使用当前代码,图片框的默认位置为 0,0,这意味着窗口的左上角。
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 totrue
:With your current code, the picture box gets default position of 0,0 which means top left corner of the window.
尝试
Try to