public DocumentPictureContext(IPictureData data)
 {
     Location = data;
 }
Пример #2
0
        /**
         * Calculate and Set the preferred size (anchor) for this picture.
         *
         * @param scaleX the amount by which image width is multiplied relative to the original width.
         * @param scaleY the amount by which image height is multiplied relative to the original height.
         * @return the new Dimensions of the scaled picture in EMUs
         */
        public static Size SetPreferredSize(IPicture picture, double scaleX, double scaleY)
        {
            IClientAnchor anchor = picture.ClientAnchor;
            bool          isHSSF = (anchor is HSSFClientAnchor);
            IPictureData  data   = picture.PictureData;
            ISheet        sheet  = picture.Sheet;

            // in pixel
            Size imgSize = GetImageDimension(new MemoryStream(data.Data), data.PictureType);
            // in emus
            Size   anchorSize  = ImageUtils.GetDimensionFromAnchor(picture);
            double scaledWidth = (scaleX == Double.MaxValue)
                ? imgSize.Width : anchorSize.Width / Units.EMU_PER_PIXEL * scaleX;
            double scaledHeight = (scaleY == Double.MaxValue)
                ? imgSize.Height : anchorSize.Height / Units.EMU_PER_PIXEL * scaleY;

            double w    = 0;
            int    col2 = anchor.Col1;
            int    dx2  = 0;

            //space in the leftmost cell
            w = sheet.GetColumnWidthInPixels(col2++);
            if (isHSSF)
            {
                w *= 1d - anchor.Dx1 / 1024d;
            }
            else
            {
                w -= anchor.Dx1 / (double)Units.EMU_PER_PIXEL;
            }

            while (w < scaledWidth)
            {
                w += sheet.GetColumnWidthInPixels(col2++);
            }

            if (w > scaledWidth)
            {
                //calculate dx2, offset in the rightmost cell
                double cw    = sheet.GetColumnWidthInPixels(--col2);
                double delta = w - scaledWidth;
                if (isHSSF)
                {
                    dx2 = (int)((cw - delta) / cw * 1024);
                }
                else
                {
                    dx2 = (int)((cw - delta) * Units.EMU_PER_PIXEL);
                }
                if (dx2 < 0)
                {
                    dx2 = 0;
                }
            }
            anchor.Col2 = (/*setter*/ col2);
            anchor.Dx2  = (/*setter*/ dx2);

            double h    = 0;
            int    row2 = anchor.Row1;
            int    dy2  = 0;

            h = GetRowHeightInPixels(sheet, row2++);
            if (isHSSF)
            {
                h *= 1 - anchor.Dy1 / 256d;
            }
            else
            {
                h -= anchor.Dy1 / (double)Units.EMU_PER_PIXEL;
            }

            while (h < scaledHeight)
            {
                h += GetRowHeightInPixels(sheet, row2++);
            }

            if (h > scaledHeight)
            {
                double ch    = GetRowHeightInPixels(sheet, --row2);
                double delta = h - scaledHeight;
                if (isHSSF)
                {
                    dy2 = (int)((ch - delta) / ch * 256);
                }
                else
                {
                    dy2 = (int)((ch - delta) * Units.EMU_PER_PIXEL);
                }
                if (dy2 < 0)
                {
                    dy2 = 0;
                }
            }

            anchor.Row2 = (/*setter*/ row2);
            anchor.Dy2  = (/*setter*/ dy2);

            Size dim = new Size(
                (int)Math.Round(scaledWidth * Units.EMU_PER_PIXEL),
                (int)Math.Round(scaledHeight * Units.EMU_PER_PIXEL)
                );

            return(dim);
        }
 /// <summary>
 /// 尝试添加图片到工作表当中
 /// </summary>
 /// <param name="sheet">工作表</param>
 /// <param name="row">行索引</param>
 /// <param name="col">列索引</param>
 /// <param name="pictureData">图片数据</param>
 /// <returns>添加成功则返回true</returns>
 public static bool TryAddPicture(this ISheet sheet, int row, int col, IPictureData pictureData) => TryAddPicture(sheet, row, col, pictureData.Data, pictureData.PictureType);