winforms 如何在C#中自动调整图像到DataGridView Image列中的固定大小

izkcnapc  于 2023-08-07  发布在  C#
关注(0)|答案(3)|浏览(217)

我有一个DataGridView,它显示员工数据,包括由两个48 x48图像(Status_Active和Status_Inactive)表示的员工状态。我想在DataGridView Image列中以固定大小(例如,16 x16或32 x32)显示这些图像。但是,图像总是显示放大。我已经尝试将ImageLayout设置为Center,但它没有按预期工作。
目前,我正在从数据库中检索雇员列表,并将每个雇员及其相应的状态图标添加到DataGridView中。但是,状态图标看起来比所需的固定大小大。
我的方法:

var employees = db.Employees.ToList();
        // Add a row for each employee
        foreach (var employee in employees)
        {
            using (Image statusIcon = employee.IsActive ? Resources.Status_Active : Resources.Status_Inactive)
            {
                string fullName = employee.LastName + ", " + employee.FirstName;
                dgvStatustableau.Rows.Add(fullName, statusIcon);
            }
        }`

字符串
编辑:
感谢@IVSoftware提出了正确的解决方案。我能够通过创建一个新的位图与16 x16矩形的图标变通。

Bitmap resizedIcon = new Bitmap(16, 16);
                using (Graphics g = Graphics.FromImage(resizedIcon))
                {
                    g.DrawImage(statusIcon, new Rectangle(0, 0, 16, 16));
                }

f45qwnt8

f45qwnt81#

您提到将ImageMode设置为Center,您可能希望使用Stretch


的数据
我复制了你的帖子,其中两个嵌入的48 x 48图像是表示绑定数据集中的一行的类的一部分,并被强制转换到32 x 32的显示区域:

class Employee : INotifyPropertyChanged
{
    public Employee()
    {
        ensureImages();
        Image = _imageInactive;
    }

    public string FullName { get; set; }
    public Image Image
    {
        get => _image;
        set
        {
            if (!Equals(_image, value))
            {
                _image = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Image)));
            }
        }
    }
    Image _image = null;

    [Browsable(false)]
    public bool IsActive
    {
        get => _isActive;
        set
        {
            if (!Equals(_isActive, value))
            {
                _isActive = value;
                Image = _isActive ? _imageActive : _imageInactive;
            }
        }
    }
    bool _isActive = false;

    private void ensureImages()
    {
        if(_imageActive == null) 
        {
            var names = typeof(Employee).Assembly.GetManifestResourceNames();
            _imageActive = localImageFromResourceName(names.First(_=>_.Contains("active.png")));
            _imageInactive = localImageFromResourceName(names.First(_ => _.Contains("inactive.png")));

            Image localImageFromResourceName(string resource)
            {
                using (var stream = GetType().Assembly.GetManifestResourceStream(resource)!)
                {
                    return new Bitmap(stream);
                }
            }
        }
    }
    private static Image _imageActive = null;
    private static Image _imageInactive = null;
    public event PropertyChangedEventHandler PropertyChanged;
}

字符串

主窗体

主表单格式化DataGridView,并模拟数据库将在何处读取数据(因为数据库不是问题的一部分)。

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        dgvStatustableau.DataSource = Employees;
        dgvStatustableau.AllowUserToAddRows= false;

        #region F O R M A T    C O L U M N S
        Employees.Add(new Employee());
        DataGridViewColumn column;
        column = dgvStatustableau.Columns[nameof(Employee.FullName)];
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

        column = dgvStatustableau.Columns[nameof(Employee.Image)];
        column.HeaderText= string.Empty;
        column.Width = 32;
        // Use Stretch here
        ((DataGridViewImageColumn)column)
        .ImageLayout = DataGridViewImageCellLayout.Stretch;
        Employees.Clear();
        #endregion F O R M A T    C O L U M N S

        mockAddEmployees();
    }
    private BindingList<Employee> Employees { get; } = new BindingList<Employee>();

    private void mockAddEmployees()
    {
        Employees.Add(new Employee
        {
            FullName= "Lisa Smith",
            IsActive= true,
        });
        Employees.Add(new Employee
        {
            FullName= "Bob Jones",
            IsActive= false,
        });
        Employees.Add(new Employee
        {
            FullName= "Rene Montoya",
            IsActive= true,
        });
    }
}

disbfnqx

disbfnqx2#

我能够通过调整图标大小来解决这个问题,绘制一个新的位图。

Bitmap resizedIcon = new Bitmap(16, 16);
                    using (Graphics g = Graphics.FromImage(resizedIcon))
                    {
                        g.DrawImage(statusIcon, new Rectangle(0, 0, 16, 16));
                    }

字符串

ruarlubt

ruarlubt3#

DataGridViewImageColumn有一个ImageLayout属性,支持Normal、Stretch和Zoom:

  • 正常:图像居中并以其原始大小显示。
  • 拉伸:图像拉伸或收缩以适应单元格的宽度和高度。
  • 缩放:图像均匀地放大或缩小,直到填满单元格的宽度或高度。

根据你的问题,看起来你对Zoom感兴趣。因此,只需分配所需的任何图像,然后将列的ImageLayout属性设置为Zoom。

相关问题