CSharp - Hướng dẫn hiển thị hình ảnh bằng PictureBox. (How to load image with PictureBox in CSharp.)


Bài viết này tôi sẽ hướng dẫn cách hiển thị hình ảnh qua đường dẫn(url) trên Win Form thông qua Common Control PictureBox trong ngôn ngữ lập trình C#.





Tạo dự án - Create new project:
  • Trên thanh menu Vào File → New Project.
  • Trong cửa sổ New Project, mục Project Types chọn Visual C# → Windows, tiếp theo tại mục Templates chọn Windows Forms Application.
  • Name project: HowToLoadImage



Thiết kế giao diện - Design GUI:


Main Form Properties
  • (name): Form1
  • Text: VNLIVES.NET - DISPLAY IMAGE

Text Box Properties
  • (name): tb_image
  • Text: D:\Z-Test\ABC.jpg
  • Anchor: Top, Left, Right

Button Properties
  • (name): bt_load
  • Text: Load
  • Anchor: Top, Right

Panel Properties
  • (name): panel1
  • AutoScroll: True
  • Anchor: Top, Bottom, Left, Right

PictureBox Properties

  • (name): pb_image
  • SizeMode: AutoSize


Mã nguồn - Source code:

Click double vào button Load để chuyển sang cửa sổ soạn thảo code và khởi tạo bt_load_Click event cho button.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void bt_load_Click(object sender, EventArgs e)
        {

            Image image = Image.FromFile(tb_image.Text);
            pb_image.Image = image;

        }
    }
}


Kết quả - Result
















No comments:

Post a Comment