Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# picturebox

作者:烟寂断魂   发布日期:2025-12-19   浏览:79

using System;
using System.Drawing;
using System.Windows.Forms;

public class PictureBoxExample : Form
{
    private PictureBox pictureBox1;

    public PictureBoxExample()
    {
        // 创建一个 PictureBox 控件
        pictureBox1 = new PictureBox
        {
            // 设置 PictureBox 的大小模式为拉伸图像
            SizeMode = PictureBoxSizeMode.StretchImage,
            // 设置 PictureBox 的位置和大小
            Location = new Point(10, 10),
            Size = new Size(200, 200),
            // 加载图像
            Image = Image.FromFile("example.jpg") // 替换为你的图片路径
        };

        // 将 PictureBox 添加到窗体中
        this.Controls.Add(pictureBox1);

        // 设置窗体的属性
        this.Text = "PictureBox Example";
        this.Size = new Size(300, 300);
    }

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new PictureBoxExample());
    }
}

解释说明:

  1. 创建 PictureBox 控件:通过 new PictureBox() 创建一个新的 PictureBox 控件,并设置其属性,如 SizeModeLocationSize
  2. 加载图像:使用 Image.FromFile("example.jpg") 方法从文件加载图像。你需要将 "example.jpg" 替换为你自己的图像路径。
  3. 添加 PictureBox 到窗体:通过 this.Controls.Add(pictureBox1) 将 PictureBox 控件添加到窗体中。
  4. 设置窗体属性:设置窗体的标题和大小。
  5. 运行应用程序:在 Main 方法中调用 Application.Run(new PictureBoxExample()) 来启动应用程序。

希望这段代码和解释对你有帮助!

上一篇:c#switch

下一篇:c#winform

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站