Uso de imágenes en libros de trabajo para .NET

Ahora que sabemos lo básico para hacer y editar nuestro Libro de Trabajo, incluyendo el uso de Tablas, Estilos y SheetViews, veamos cómo añadir un nuevo elemento: Cuadros

Más información Cuadernos Xeed para .NET

Ahora que sabemos lo básico para hacer y editar nuestro Libro de Trabajo, incluyendo el uso de TablasEstilos y SheetViewsvamos a añadir un nuevo elemento: Imágenes.

¿Qué es una imagen?

Fotografía representa un elemento de imagen en el Libro de Trabajo, también se denomina elemento de dibujo.

La clase Picture tiene las siguientes propiedades:

  • Description: the description of the picture.
  • DrawingClientData: the information of the ClientData, which is how the picture should behave when the worksheet is protected or printed.
  • PictureLocks: the information of the PictureLocks, which are the manipulations allowed on the picture.

Acceso a las imágenes

Se accede a las imágenes de un Libro de Trabajo a través de la opción ColecciónDeImágenes en un determinado Hoja de trabajo. La clase PictureCollection contendrá todas las imágenes de la hoja de cálculo, y también permitirá al usuario añadir nuevas imágenes y manipularlas.

La clase PictureCollection tiene las siguientes propiedades:

  • Count: returns the number of Pictures in the current Worksheet.
  • Item: returns the Picture located at the specified index.
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var worksheet = document.Worksheets[ 0 ];
	var pictureQty = worksheet.Pictures.Count;
	var firstPicture = worksheet.Pictures[ 0 ];
}

Añadir imágenes a una hoja de cálculo

Para añadir imágenes a una hoja de cálculo, utilizamos la función Añadir disponible en la clase PictureCollection.

El método Añadir ofrece varias sobrecargas, permitiendo flexibilidad en el tipo de Imagen a añadir y donde se posiciona.

Note:

  • For the overloads that have a scale parameter, the value must be 1 or greater (100 by default).

Añadir imágenes a una hoja de cálculo:

using( var document = Workbook.Load( "testDoc.xlsx" ));
{
	var srcFile = PictureSampleResourcesDirectory + @"balloon.jpg";
	var srcStream = new FileStream( PictureSampleResourcesDirectory + @"balloon.jpg", FileMode.Open, FileAccess.Read );

	var worksheet = document.Worksheets[ 0 ];

	// Overload 1: filename and scale
	var pic1 = worksheet.Pictures.Add( srcFile, 50 );

	// Overload 2: stream and scale
	var pic2 = worksheet.Pictures.Add( srcStream, 50 );

	// Overload 3: filename, top left position (coordinates), scale
	var pic3 = worksheet.Pictures.Add( srcFile, 14, 0, 50 );

	// Overload 4: stream, top left position (coordinates), scale
	var pic4 = worksheet.Pictures.Add( srcStream, 14, 0, 50 );

	// Overload 5: filename, top left position (address), scale
	var pic5 = worksheet.Pictures.Add( srcFile, "A4", 50 );

	// Overload 6: stream, top left position (address), scale
	var pic6 = worksheet.Pictures.Add( srcStream, "A4", 50 );

	// Overload 7: filename, top left and bottom right positions (coordinates)
	var pic7 = worksheet.Pictures.Add( srcFile, 14, 0, 30, 15 );

	// Overload 8: stream, top left and bottom right positions (coordinates)
	var pic8 = worksheet.Pictures.Add( srcStream, 14, 0, 30, 15 );

	// Overload 9: filename, top left and bottom right positions (addresses)
	var pic9 = worksheet.Pictures.Add( srcFile, "A4", "E12");

	// Overload 10: stream, top left and bottom right positions (addresses)
	var pic10 = worksheet.Pictures.Add( srcStream, "A4", "E12" );

	document.Save();
}

Restringir una imagen

En PictureLocks permite eliminar la autorización para modificar ciertas propiedades de la imagen en Microsoft Excel. Por defecto, todos los valores se establecen en false.

Ajustes disponibles (establézcalos en true para aplicar su comportamiento correspondiente):

  • NoAdjustHandles: removes the authorization to use the adjust handles.
  • NoChangeArrowheads: removes the authorization to change Arrowheads.
  • NoChangeAspect: removes the authorization to change the ratio.
  • NoChangeShapeType: removes the authorization to change the connection shape.
  • NoCrop: removes the authorization to crop the picture.
  • NoEditPoints: removes the authorization to edit the connection shape point.
  • NoGrp: removes the authorization to group shapes.
  • NoMove: removes the authorization to move the picture.
  • NoRot: removes the authorization to rotate the picture.
  • NoSelect: removes the authorization to select the picture.

Para más información, consulte el documentación.