Texto e imagen en celda de DataGridView
Publicado por Preguntador VB.NET (22 intervenciones) el 19/05/2007 11:08:08
Buenas. Este codigo (que no se de quien es) contiene una clase para poder visualizar una imagen en una celda de texto de un DataGridView. He puesto como imagen una tipo BMP y otra GIF y funciona perfectamente, pero cuando es una imagen PNG o TIF no se visualiza correctamente por que la aumenta de tamaño cuando tienen el mismo. He intentado ver que pasa pero no hay manera. Si alguien tiene alguna solucion... pues gracias.
Os pongo un link a una imagen para que veais lo que sucede:
http://img176.imageshack.us/img176/6841/imageandtextdgvkr6.png
Os escribo el ejemplo:
En VB.Net 2005: Creais un formulario e incluis un DataGridView y pegais este codigo:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Col0 As New TextAndImageColumn
Col0.Name = "C0"
Col0.Image = Me.PictureBox1.Image
Col0.HeaderText = "Imagen .PNG"
Me.DataGridView1.Columns.Insert(0, Col0)
Dim Col1 As New TextAndImageColumn
Col1.Name = "C1"
Col1.Image = Me.PictureBox2.Image
Col1.HeaderText = "Imagen .GIF"
Me.DataGridView1.Columns.Insert(1, Col1)
Me.DataGridView1.Rows.Add()
End Sub
End Class
Public Class TextAndImageColumn
Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private imageSize As Size
Public Sub New()
Me.CellTemplate = New TextAndImageCell
End Sub
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageColumn = CType(TryCast(MyBase.Clone, TextAndImageColumn), TextAndImageColumn)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function
Public Property Image() As Image
Get
Return Me.imageValue
End Get
Set(ByVal value As Image)
If Not Me.Image Is value Then
Me.imageValue = value
Me.imageSize = value.Size
If Not (Me.InheritedStyle Is Nothing) Then
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.DefaultCellStyle.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End If
End Set
End Property
Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
Get
Return CType(TryCast(Me.CellTemplate, TextAndImageCell), TextAndImageCell)
End Get
End Property
Friend ReadOnly Property ImageSize_() As Size
Get
Return imageSize
End Get
End Property
End Class
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageCell = CType(TryCast(MyBase.Clone, TextAndImageCell), TextAndImageCell)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function
Public Property Image() As Image
Get
If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
Else
If Not (Me.imageValue Is Nothing) Then
Return Me.imageValue
Else
Return Me.OwningTextAndImageColumn.Image
End If
End If
End Get
Set(ByVal value As Image)
If Not Me.Image Is value Then
Me.imageValue = value
Me.imageSize = value.Size
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top + 5, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End Set
End Property
Protected Overloads Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Not (Me.Image Is Nothing) Then
Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer
graphics.SetClip(cellBounds)
graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
graphics.EndContainer(container)
End If
End Sub
Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
Get
Return CType(TryCast(Me.OwningColumn, TextAndImageColumn), TextAndImageColumn)
End Get
End Property
End Class
Os pongo un link a una imagen para que veais lo que sucede:
http://img176.imageshack.us/img176/6841/imageandtextdgvkr6.png
Os escribo el ejemplo:
En VB.Net 2005: Creais un formulario e incluis un DataGridView y pegais este codigo:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Col0 As New TextAndImageColumn
Col0.Name = "C0"
Col0.Image = Me.PictureBox1.Image
Col0.HeaderText = "Imagen .PNG"
Me.DataGridView1.Columns.Insert(0, Col0)
Dim Col1 As New TextAndImageColumn
Col1.Name = "C1"
Col1.Image = Me.PictureBox2.Image
Col1.HeaderText = "Imagen .GIF"
Me.DataGridView1.Columns.Insert(1, Col1)
Me.DataGridView1.Rows.Add()
End Sub
End Class
Public Class TextAndImageColumn
Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private imageSize As Size
Public Sub New()
Me.CellTemplate = New TextAndImageCell
End Sub
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageColumn = CType(TryCast(MyBase.Clone, TextAndImageColumn), TextAndImageColumn)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function
Public Property Image() As Image
Get
Return Me.imageValue
End Get
Set(ByVal value As Image)
If Not Me.Image Is value Then
Me.imageValue = value
Me.imageSize = value.Size
If Not (Me.InheritedStyle Is Nothing) Then
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.DefaultCellStyle.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End If
End Set
End Property
Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
Get
Return CType(TryCast(Me.CellTemplate, TextAndImageCell), TextAndImageCell)
End Get
End Property
Friend ReadOnly Property ImageSize_() As Size
Get
Return imageSize
End Get
End Property
End Class
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageCell = CType(TryCast(MyBase.Clone, TextAndImageCell), TextAndImageCell)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function
Public Property Image() As Image
Get
If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
Else
If Not (Me.imageValue Is Nothing) Then
Return Me.imageValue
Else
Return Me.OwningTextAndImageColumn.Image
End If
End If
End Get
Set(ByVal value As Image)
If Not Me.Image Is value Then
Me.imageValue = value
Me.imageSize = value.Size
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top + 5, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End Set
End Property
Protected Overloads Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Not (Me.Image Is Nothing) Then
Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer
graphics.SetClip(cellBounds)
graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
graphics.EndContainer(container)
End If
End Sub
Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
Get
Return CType(TryCast(Me.OwningColumn, TextAndImageColumn), TextAndImageColumn)
End Get
End Property
End Class
Valora esta pregunta


0