Visual Basic.NET - Texto e imagen en celda de DataGridView

 
Vista:

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
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Texto e imagen en celda de DataGridView

Publicado por Harold V (411 intervenciones) el 21/05/2007 00:35:03
Este ejemplo hace: Inserta imagenes PNG y TIF, funciona con cualquier formato

El codigo agrega 2 Columnas de tipo Imagen al datagridView, inserta una fila y luego le añade las imagenes que hay en el picturebox1 y en el picturebox2 a su respectiva columna. y le da un tamaño uniforme a tus imagenes

Solo necesitas que tus Picturebox tengan imagenes


Imports System.Drawing.Image

Dim col1 As New DataGridViewImageColumn
Dim col2 As New DataGridViewImageColumn
Dim CallBack As New GetThumbnailImageAbort(AddressOf MycallBack)

Private Sub cmdCargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCargar.Click

col1.HeaderText = "PNG"
col2.HeaderText = "TIF"

Me.DataGridView1.Columns.Add(col1)
Me.DataGridView1.Columns.Add(col2)

Me.DataGridView1.Rows.Add(1)
Me.DataGridView1.Rows(0).Height = 50

Me.DataGridView1.Rows(0).Cells(0).Value = PictureBox1.Image.GetThumbnailImage(50, 50, CallBack, IntPtr.Zero)

Me.DataGridView1.Rows(0).Cells(1).Value = PictureBox2.Image.GetThumbnailImage(50, 50, CallBack, IntPtr.Zero)

End Sub

Function MycallBack() As Boolean
Return False
End Function
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Imagen Autosize

Publicado por Harold V (411 intervenciones) el 21/05/2007 00:45:38
Olvidaba que querias que sea del tamaño de la fila....

entonces.....

Quita esta linea
Me.DataGridView1.Rows(0).Height = 50

declara
dim msize as int32=DataGridView1.Rows(0).Height

y remplaza cada 50 por msize

si agrandas la fila 0 la imagen nueva automaticamente cogera su tamaño
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Creo que no has entendido bien

Publicado por Preguntador VB.NET (22 intervenciones) el 21/05/2007 17:30:04
Antetodo, gracias por contestar.

Creo que no has entendido lo que hace la clase que yo he puesto.

Lo que hace la clase TextAndImageColumn es que puedas crear columnas en las que puedas insertar texto e imagen en una misma celda (cosa que por si solo el DataGridView no hace), pero lo que tu dices es como insertar una imagen en la celda de una columna del tipo imagen.

No se si me he explicado bien, asi que si tienes dudas, dimelo.

Gracias de nuevo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Creo que no has entendido bien

Publicado por Harold V (411 intervenciones) el 21/05/2007 22:30:33
Hola:

con esto tendras todas las imagenes del mismo tamaño por mas grandes que sean. en cualquier formato pruebalo.

remplaza la linea:

graphics.DrawImage(Me.Image, cellBounds.Location)


por esta:

graphics.DrawImage(Me.Image, cellBounds.Location.X, cellBounds.Location.Y, cellBounds.Height, cellBounds.Height)

normalmente deberia ser:
'cellBounds.Width, cellBounds.Height
pero te ocupara todo el ancho de tu celda.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

¡¡¡¡¡GRACIAAAAAAAAASSSSSSS!!!!!!

Publicado por Preguntador VB.NET (22 intervenciones) el 22/05/2007 11:49:09
Muchas gracias amigo, eso es lo que buscaba. Me estaba volviendo loco ya.
De verdad, muchisimas gracias.

Nos vemos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Y añado...

Publicado por Preguntador VB.NET (22 intervenciones) el 22/05/2007 17:26:28
La linea del codigo original:

graphics.DrawImage(Me.Image, cellBounds.Location)

Que Harold sustituye por esta (se ajusta el tamaño al alto de la celda):

graphics.DrawImage(Me.Image, cellBounds.Location.X, cellBounds.Location.Y, cellBounds.Height, cellBounds.Height)

Si la sustituimos por esta otra, se dibuja la imagen a su tamaño real:

graphics.DrawImage(Me.Image, cellBounds.Location.X, cellBounds.Location.Y, Me.Image.Width, Me.Image.Height)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Y añado...

Publicado por Diego (1 intervención) el 17/02/2017 16:53:47
Buenas tardes gracias por todo el aporte, otra pregunta, para ubicar la imagen a lado derecho de la celda, es posible?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar