NPOI获取图片详细信息
2016-10-11 21:30
281 查看
参考韩兆新的博客园http://www.cnblogs.com/hanzhaoxin/p/4442369.html
NPOI提供了非常方便的方法IWorkbook.GetAllPictures(),这个方法返回的是一个IList,里面的数据是HSSFPictureData类型的。
IWorkbook hssfworkbook;
...
IList pictures = hssfworkbook.GetAllPictures();
int i = 0;
foreach (HSSFPictureData pic in pictures)
{
string ext = pic.SuggestFileExtension();
if(ext.Equals("jpeg"))
{
Image jpg = Image.FromStream(new MemoryStream(pic.Data));
jpg.Save(string.Format("pic{0}.jpg",i++));
}
if (ext.Equals("png"))
{
Image png = Image.FromStream(new MemoryStream(pic.Data));
png.Save(string.Format("pic{0}.png",i++));
}
}
但只能获取图片扩展名信息,但实际需求是导入数据需要获得图片的行列
参考韩兆新的博客园http://www.cnblogs.com/hanzhaoxin/p/4442369.html
,可获取行列数据
public class PicturesInfo
{
public string ext { get; set; }
public int MinRow { get; set; }
public int MaxRow { get; set; }
public int MinCol { get; set; }
public int MaxCol { get; set; }
public Byte[] PictureData { get; private set; }
public PicturesInfo(int minRow, int maxRow, int minCol, int maxCol, Byte[] pictureData, string ext)
{
this.MinRow = minRow;
this.MaxRow = maxRow;
this.MinCol = minCol;
this.MaxCol = maxCol;
this.PictureData = pictureData;
this.ext = ext;
}
}
public static class NpoiExtend
{
public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet)
{
return sheet.GetAllPictureInfos(null, null, null, null);
}
public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal = true)
{
if (sheet is HSSFSheet)
{
return GetAllPictureInfos((HSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
}
else if (sheet is XSSFSheet)
{
return GetAllPictureInfos((XSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
}
else
{
throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!");
}
}
private static List<PicturesInfo> GetAllPictureInfos(HSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
{
List<PicturesInfo> picturesInfoList = new List<PicturesInfo>();
var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer;
if (null != shapeContainer)
{
var shapeList = shapeContainer.Children;
foreach (var shape in shapeList)
{
if (shape is HSSFPicture && shape.Anchor is HSSFClientAnchor)
{
var picture = (HSSFPicture)shape;
var anchor = (HSSFClientAnchor)shape.Anchor;
if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
{
picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data,picture.PictureData.MimeType));
}
}
}
}
return picturesInfoList;
}
private static List<PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
{
List<PicturesInfo> picturesInfoList = new List<PicturesInfo>();
var documentPartList = sheet.GetRelations();
foreach (var documentPart in documentPartList)
{
if (documentPart is XSSFDrawing)
{
var drawing = (XSSFDrawing)documentPart;
var shapeList = drawing.GetShapes();
foreach (var shape in shapeList)
{
if (shape is XSSFPicture)
{
var picture = (XSSFPicture)shape;
var anchor = picture.GetPreferredSize();
if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
{
picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data,picture.PictureData.MimeType));
}
}
}
}
}
return picturesInfoList;
}
private static bool IsInternalOrIntersect(int? rangeMinRow, int? rangeMaxRow, int? rangeMinCol, int? rangeMaxCol,
int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol, bool onlyInternal)
{
int _rangeMinRow = rangeMinRow ?? pictureMinRow;
int _rangeMaxRow = rangeMaxRow ?? pictureMaxRow;
int _rangeMinCol = rangeMinCol ?? pictureMinCol;
int _rangeMaxCol = rangeMaxCol ?? pictureMaxCol;
if (onlyInternal)
{
return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow &&
_rangeMinCol <= pictureMinCol && _rangeMaxCol >= pictureMaxCol);
}
else
{
return ((Math.Abs(_rangeMaxRow - _rangeMinRow) + Math.Abs(pictureMaxRow - pictureMinRow) >= Math.Abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow)) &&
(Math.Abs(_rangeMaxCol - _rangeMinCol) + Math.Abs(pictureMaxCol - pictureMinCol) >= Math.Abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol)));
}
}
}
NPOI提供了非常方便的方法IWorkbook.GetAllPictures(),这个方法返回的是一个IList,里面的数据是HSSFPictureData类型的。
IWorkbook hssfworkbook;
...
IList pictures = hssfworkbook.GetAllPictures();
int i = 0;
foreach (HSSFPictureData pic in pictures)
{
string ext = pic.SuggestFileExtension();
if(ext.Equals("jpeg"))
{
Image jpg = Image.FromStream(new MemoryStream(pic.Data));
jpg.Save(string.Format("pic{0}.jpg",i++));
}
if (ext.Equals("png"))
{
Image png = Image.FromStream(new MemoryStream(pic.Data));
png.Save(string.Format("pic{0}.png",i++));
}
}
但只能获取图片扩展名信息,但实际需求是导入数据需要获得图片的行列
参考韩兆新的博客园http://www.cnblogs.com/hanzhaoxin/p/4442369.html
,可获取行列数据
public class PicturesInfo
{
public string ext { get; set; }
public int MinRow { get; set; }
public int MaxRow { get; set; }
public int MinCol { get; set; }
public int MaxCol { get; set; }
public Byte[] PictureData { get; private set; }
public PicturesInfo(int minRow, int maxRow, int minCol, int maxCol, Byte[] pictureData, string ext)
{
this.MinRow = minRow;
this.MaxRow = maxRow;
this.MinCol = minCol;
this.MaxCol = maxCol;
this.PictureData = pictureData;
this.ext = ext;
}
}
public static class NpoiExtend
{
public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet)
{
return sheet.GetAllPictureInfos(null, null, null, null);
}
public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal = true)
{
if (sheet is HSSFSheet)
{
return GetAllPictureInfos((HSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
}
else if (sheet is XSSFSheet)
{
return GetAllPictureInfos((XSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
}
else
{
throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!");
}
}
private static List<PicturesInfo> GetAllPictureInfos(HSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
{
List<PicturesInfo> picturesInfoList = new List<PicturesInfo>();
var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer;
if (null != shapeContainer)
{
var shapeList = shapeContainer.Children;
foreach (var shape in shapeList)
{
if (shape is HSSFPicture && shape.Anchor is HSSFClientAnchor)
{
var picture = (HSSFPicture)shape;
var anchor = (HSSFClientAnchor)shape.Anchor;
if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
{
picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data,picture.PictureData.MimeType));
}
}
}
}
return picturesInfoList;
}
private static List<PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
{
List<PicturesInfo> picturesInfoList = new List<PicturesInfo>();
var documentPartList = sheet.GetRelations();
foreach (var documentPart in documentPartList)
{
if (documentPart is XSSFDrawing)
{
var drawing = (XSSFDrawing)documentPart;
var shapeList = drawing.GetShapes();
foreach (var shape in shapeList)
{
if (shape is XSSFPicture)
{
var picture = (XSSFPicture)shape;
var anchor = picture.GetPreferredSize();
if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
{
picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data,picture.PictureData.MimeType));
}
}
}
}
}
return picturesInfoList;
}
private static bool IsInternalOrIntersect(int? rangeMinRow, int? rangeMaxRow, int? rangeMinCol, int? rangeMaxCol,
int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol, bool onlyInternal)
{
int _rangeMinRow = rangeMinRow ?? pictureMinRow;
int _rangeMaxRow = rangeMaxRow ?? pictureMaxRow;
int _rangeMinCol = rangeMinCol ?? pictureMinCol;
int _rangeMaxCol = rangeMaxCol ?? pictureMaxCol;
if (onlyInternal)
{
return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow &&
_rangeMinCol <= pictureMinCol && _rangeMaxCol >= pictureMaxCol);
}
else
{
return ((Math.Abs(_rangeMaxRow - _rangeMinRow) + Math.Abs(pictureMaxRow - pictureMinRow) >= Math.Abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow)) &&
(Math.Abs(_rangeMaxCol - _rangeMinCol) + Math.Abs(pictureMaxCol - pictureMinCol) >= Math.Abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol)));
}
}
}
相关文章推荐
- android actionbar中share使用、图片详细信息获取
- 利用php获取图片完整Exif信息类 获取图片详细完整信息类
- 数据库获取图片 加入幻灯效果 点击查看详细信息
- UIImagePickerController获取图片详细信息
- 获取照片库中 某张图片的详细信息(拍照时间,地点)
- 获取图片属性详细信息
- 利用php获取图片完整Exif信息类 获取图片详细完整信息类
- flex加载本地图片怎么获取到图片的详细信息
- 用C#和WMI获取逻辑驱动器详细信息(转)
- sql获取表,字段,长度,类型,描述,等详细信息
- 获取句柄的详细信息:(原代码) http://blog.vckbase.com/bastet/archive/2005/03/31/4244.html
- ExtJs学习笔记(7)_获取GridPanel选中行的详细信息
- WinAPI: GetVersionEx - 获取系统版本详细信息
- outlook 处理请求的任务时出错,请复查下列错误表以获取详细信息。
- 获取硬盘的详细信息
- 再学 GDI+[102]: TGPImage(22) - 获取所有解码器的详细信息
- ExtJs学习笔记(7)_获取GridPanel选中行的详细信息
- C++ 通过WIN32 API 获取逻辑磁盘详细信息
- 实战DeviceIoControl之四:获取硬盘的详细信息
- 获取图片的EXIF信息如此困难?