在C#中编程添加picturebox

本文关键字:添加 picturebox 编程 | 更新日期: 2023-09-27 18:24:11

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

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);
    }
}
}

这就是整个代码,因为我只是想测试为我正在处理的其他东西添加一个picturebox。我想做的是,当我运行程序时,它会将picturebox放入表单中,但这并没有发生。表格是空白的。

-----编辑---------------

这是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());
    }
}
}

设计师是不是有什么东西挡住了它,或者是我没有添加的东西?

在C#中编程添加picturebox

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

试着把它带到前线:

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();
}

图片可能无法完全显示,只需设置SizeMode即可尝试:

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom