简体   繁体   English

DataGridViewImageColumn图像布局不起作用

[英]DataGridViewImageColumn Image layout doesn't work

I'm trying to display an image on datagridview with an image column; 我正在尝试在带有图像列的datagridview上显示图像; however, it appears stretched vertically and squished horizontally. 但是,它似乎垂直拉伸而水平挤压。 I'm not sure what's limiting it to be displayed this way. 我不确定是什么限制了这种方式显示它。

Grid Generation 网格生成

            gvPackage.Columns.Clear();

            gvPackage.AutoGenerateColumns = false;
            DataGridViewColumn[] cols = new DataGridViewColumn[5];
            GridColumn gc = null;

            cols[0] = new DataGridViewTextBoxColumn();
            cols[0].DataPropertyName = "ID";
            cols[0].Name = "ID";
            cols[0].Visible = false;

            cols[1] = new DataGridViewTextBoxColumn();
            cols[1].DataPropertyName = "iLive";
            cols[1].Name = "iLive";
            cols[1].Visible = false;

            cols[2] = new DataGridViewTextBoxColumn();
            cols[2].DataPropertyName = "iExplicit";
            cols[2].Name = "iExplicit";
            cols[2].Visible = false;

            cols[3] = new DataGridViewImageColumn();
            cols[3].Name = "Icon";
            cols[3].Width = 70;
            cols[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            cols[3].FillWeight = 1F;

            cols[4] = new DataGridViewTextBoxColumn();
            cols[4].DataPropertyName = "sName";
            cols[4].Name = "sName";
            cols[4].Width = 216;
            cols[4].FillWeight = 50.5166F;

            gvPackage.Columns.AddRange(cols);

            List<Package> _pl = _______Manager.SHARED_RES;

            foreach (Package p in _pl)
            {
                gvPackage.Rows.Add(
                    p.ID,
                    p.iLive,
                    p.iExplicit,
                    gImageList.Images["imgNoExplicitNoLive"],
                    p.sName
                    );
            }

Cell Formatting: 单元格格式:

 private void gvPackage_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gvPackage.Columns[e.ColumnIndex].Name == "Icon")
            {
                bool isExp = this.gvPackage.Rows[e.RowIndex].Cells[2].Value.ToString() == "1";
                bool isLive = this.gvPackage.Rows[e.RowIndex].Cells[1].Value.ToString() == "1";
                if (isExp && isLive)
                {
                    e.Value = gImageList.Images["imgExplicitLive"];

                }
                else
                    if (!isExp && isLive)
                    {
                        e.Value = gImageList.Images["imgNoExplicitLive"];
                    }
                    else
                        if (isExp && !isLive)
                        {
                            e.Value = gImageList.Images["imgExplicitNoLive"];
                        }
                        else
                            if (!isExp && !isLive)
                            {
                                e.Value = gImageList.Images["imgNoExplicitNoLive"];
                            }
            }
        }

Bad image: 图片不良:

图片不正确

The source image: 源图像: 源图像

Any help would be appreciated. 任何帮助,将不胜感激。

Start by checking the gImageList.Imagesize ! 首先检查gImageList.Imagesize

It's default will use square image sizes of 16x16! 默认情况下将使用16x16的正方形图像尺寸!

Change that to the size of your Images before you load them: 加载Images 之前 ,将其更改为Images的大小:

gImageList.ImageSize = new Size(61,12);

Note that all Images need to have the same size. 请注意, 所有 Images必须具有相同的大小。 If they haven't you should change them; 如果没有,则应进行更改; much better than the stretching the ImageList will apply! 比拉伸ImageList更好!

Also note that the Images in an ImageList are limited to 256x256 pixels. 另请注意, ImageList中的Images 限制256x256像素。

You may also want to check if you are happy with its ColorDepth = Depth8Bit 您可能还想检查一下它的ColorDepth = Depth8Bit是否满意

Obviously your Column/Cell needs to provide enough space as well! 显然,您的Column/Cell需要提供足够的空间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM