以C#形式添加图片框

时间:2014-09-24 17:52:27

标签: c# winforms picturebox

好的,我正在测试我的winform应用程序添加了一个图片框。我终于在这里问了,因为当我查看如何做到这一点时,我看不出与我正在做的事情有什么不同。这是代码:

namespace AddPanel
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PictureBox pictureBox1 = new PictureBox();
        pictureBox1.ImageLocation = @"C:\Users\xoswaldr\Desktop\OrangeLogo.jpg";
        pictureBox1.Location = new System.Drawing.Point(20, 40);
        pictureBox1.Name = "pictureBox1";
        pictureBox1.Size = new System.Drawing.Size(100, 50);
        pictureBox1.BackColor = Color.Black;
        this.Controls.Add(pictureBox1);

    }
}
}

这就是整个代码,因为我只是想测试为我正在做的其他事情添加一个图片框。我正在尝试做的是当我运行程序时它将图片框放在表单中,但这并没有发生。表格只是空白。

----- EDIT ------------

这是Form1.Designer.cs代码

namespace AddPanel
{
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(690, 381);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion

}
}

这是Program.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace AddPanel
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}

设计师中是否存在阻挡它的东西或者我还没有添加的东西?

2 个答案:

答案 0 :(得分:2)

由于您的代码看起来正确,是否可能有另一个控件覆盖图片框?

尝试将它带到前面:

private void Form1_Load(object sender, EventArgs e)
{
    var pictureBox1 = new PictureBox
    {
        BackColor = Color.Black,
        ImageLocation = @"C:\Users\xoswaldr\Desktop\OrangeLogo.jpg",
        Location = new Point(20, 40),
        Name = "pictureBox1",
        Size = new Size(100, 50)
    };

    this.Controls.Add(pictureBox1);

    pictureBox1.BringToFront();
}

答案 1 :(得分:0)

图片可能无法完全显示,只需将SizeMode设置为试用:

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom