Show image in datagridview tooltip

Background: I work with winforms in C #. I do not want the images displayed in the datagridview cells, I only saved the path in the database and showing them in the datagridview from the database.

Problem: When a user enters a cell, a tooltip pops up. I need so that when the column index of the current cell is 2, then the tooltip should display an image from the path specified in the current cell.

I found this article is very good. But failed to succeed. I have the following code

    void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
    {
        DataGridView parent = e.AssociatedControl as DataGridView;
        if (parent.CurrentCell != null)
        {
            if (parent.CurrentCell.ColumnIndex == 2)
            {
                Bitmap bmpIn = new Bitmap(parent.CurrentCell.Value + "");
                using (Graphics g = Graphics.FromImage(bmpIn))
                {
                    Rectangle mr = new Rectangle(5, 5, 50, 50);
                    mr.Location = new Point(5, 5);
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.DrawImage(bmpIn, mr);
                }
            }
        }
    }

, , , , , , , tootip. . datagridview.

enter image description here

+5
3

"StretchImage" visible = false, datagridview

       private void metroGrid1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv_sender = sender as DataGridView;
        DataGridViewCell dgv_MouseOverCell=null;
        if (e.RowIndex > 0 && e.ColumnIndex > 0 && e.RowIndex <dgv_sender.RowCount && e.ColumnIndex<dgv_sender.ColumnCount)
        {
          dgv_MouseOverCell = dgv_sender.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }
        if(dgv_MouseOverCell !=null)
        if (e.ColumnIndex == 4) {
            if (dgv_MouseOverCell.Value != null)
            {
                if (File.Exists(dgv_MouseOverCell.Value.ToString()))
                {
                    Image img = Image.FromFile(dgv_MouseOverCell.Value.ToString());
                    pictureBox1.ImageLocation = dgv_MouseOverCell.Value.ToString();
                    pictureBox1.Location = new System.Drawing.Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);
                    pictureBox1.Visible = true;
                }
            }
        }
    }

    private void metroGrid1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
        pictureBox1.Visible = false;
    }

,

+1

- .

, CellMouseOver, CellMouseLeave

    frm_MouseOverPicture HoverZoom = new frm_MouseOverPicture();

    private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
       DataGridView dgv_sender = sender as DataGridView;
       DataGridViewCell dgv_MouseOverCell = dgv_sender.Rows[e.RowIndex].Cells[e.ColumnIndex];

       //Get FilePath from dgv_MouseOverCell content

       //Get x, y based on position relative to edge of screen
       //x, y = top left point of HoverZoom form

       HoverZoom.LoadPicture(FilePath);
       HoverZoom.Location = new System.Drawing.Point(x, y);
       HoverZoom.Show();

    }

    private void dgv_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
       HoverZoom.Hide();
       HoverZoom.ClearPicture();
    }

, , . .

+4
    void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
    {
        DataGridView parent = e.AssociatedControl as DataGridView;
        if (parent.CurrentCell != null)
        {
            if (parent.CurrentCell.ColumnIndex == 2)
            {
                string path = parent.CurrentCell.Value.ToString();
                using (System.Drawing.Imaging.Metafile emf = new System.Drawing.Imaging.Metafile(path))
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(emf.Width, emf.Height))
                {
                    bmp.SetResolution(emf.HorizontalResolution, emf.VerticalResolution);

                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
                    {
                        g.DrawImage(emf,
                            new Rectangle(0, 0, emf.Width, emf.Height),
                            new Rectangle(0, 0, emf.Width, emf.Height),
                            GraphicsUnit.Pixel
                        );

                        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    }
                }
            }
        }
    }

System.Windows.Interop

" .NET 3.0 runtime , ..."

" .NET 3.0 Add References " WindowsBase ". , " " " ". (C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 )"

+1

All Articles