好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

winform开发中使用Panel容器组合图片和标题成为一个整体

winform开发中使用Panel容器组合图片和标题成为一个整体

private PictureBox CreatePictureBox(byte[] imageData, string title)

{

    // 转换二进制为Image对象

    using (MemoryStream ms = new MemoryStream((byte[])imageData))

    {

        Image image = Image.FromStream(ms);

        // 创建PictureBox

        PictureBox pb = new PictureBox();

        pb.Image = image;

        pb.SizeMode = PictureBoxSizeMode.Zoom;

        pb.Width = 200;  // 固定宽度

        pb.Height = 200; // 固定高度

        pb.Margin = new Padding(10);

        pb.Cursor = Cursors.Hand;

        // 添加标题Label

        Label lblTitle = new Label();

        lblTitle.Text = title;

        lblTitle.Dock = DockStyle.Bottom;

        lblTitle.TextAlign = ContentAlignment.MiddleCenter;

        lblTitle.BackColor = Color.LightGray;

        // 使用Panel容器组合图片和标题

        Panel container = new Panel();

        container.Controls.Add(pb);

        container.Controls.Add(lblTitle);

        container.Width = pb.Width;

        container.Height = pb.Height + lblTitle.Height;

        // 点击事件

        pb.Click += (sender, e) =>

        {

            MessageBox.Show($"查看图片: {title}", "图片详情");

        };

        return pb;

    }

}


查看更多关于winform开发中使用Panel容器组合图片和标题成为一个整体的详细内容...

  阅读:20次