[英]Display an image into windows forms
I wanted to display an image to the windows forms, but i already did this and the image did not come out.我想向窗体显示图像,但我已经这样做了,但图像没有出来。
Where did I go wrong?我哪里做错了?
Here is the code:这是代码:
private void Images(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("../SamuderaJayaMotor.png");
pb1.Location = new Point(100, 100);
pb1.Size = new Size(500, 500);
this.Controls.Add(pb1);
}
Here ( http://www.dotnetperls.com/picturebox ) there 3 ways to do this:在这里( http://www.dotnetperls.com/picturebox )有 3 种方法可以做到这一点:
Using ImageLocation property of the PictureBox like:使用 PictureBox 的 ImageLocation 属性,例如:
private void Form1_Load(object sender, EventArgs e) { PictureBox pb1 = new PictureBox(); pb1.ImageLocation = "../SamuderaJayaMotor.png"; pb1.SizeMode = PictureBoxSizeMode.AutoSize; }
Using an image from the web like:使用来自网络的图像,例如:
private void Form1_Load(object sender, EventArgs e) { PictureBox pb1 = new PictureBox(); pb1.ImageLocation = "http://www.dotnetperls.com/favicon.ico"; pb1.SizeMode = PictureBoxSizeMode.AutoSize; }
And please, be sure that "../SamuderaJayaMotor.png" is the correct path of the image that you are using.请确保“../SamuderaJayaMotor.png”是您正在使用的图像的正确路径。
There could be many reasons for this.这可能有很多原因。 A few that come up quickly to my mind:
一些我很快想到的:
InitializeComponent()
?InitializeComponent()
之后调用了此例程?I display images in windows forms when I put it in Load event like this:当我把它放在 Load 事件中时,我以 Windows 窗体显示图像,如下所示:
private void Form1_Load( object sender , EventArgs e )
{
pictureBox1.ImageLocation = "./image.png"; //path to image
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Location = new Point(0, 0);
pb.Size = new Size(150, 150);
pb.Image = Image.FromFile("E:\\Wallpaper (204).jpg");
pb.Visible = true;
this.Controls.Add(pb);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.