public static Size SizeImage(Size image, Size box, Size.Strategy strategy) { Size result = default(Size); switch (strategy) { case Size.Strategy.AutoSize: result = image; return(result); case Size.Strategy.Fit: result = box; return(result); case Size.Strategy.FitProportional: { float num = (float)box.Width / (float)image.Width; float num2 = (float)box.Height / (float)image.Height; if (num2 < num) { num = num2; } result.Height = (int)((float)image.Height * num); result.Width = (int)((float)image.Width * num); break; } case Size.Strategy.Clip: result = image; return(result); } return(result); }
public void AddImage(byte[] imgBuf, float height, float width, RPLFormat.Sizings sizing) { bool flag = imgBuf == null || imgBuf.Length == 0; Size image = default(Size); string extension = null; if (!flag) { try { using (Image image2 = Image.FromStream(new MemoryStream(imgBuf))) { image.Height = WordOpenXmlUtils.PixelsToEmus(image2.Height, image2.VerticalResolution, 0, 20116800); image.Width = WordOpenXmlUtils.PixelsToEmus(image2.Width, image2.HorizontalResolution, 0, 20116800); extension = ((image2.RawFormat.Guid == ImageFormat.Png.Guid) ? "png" : ((image2.RawFormat.Guid == ImageFormat.Jpeg.Guid) ? "jpg" : ((!(image2.RawFormat.Guid == ImageFormat.Gif.Guid)) ? "bmp" : "gif"))); } } catch (ArgumentException) { flag = true; } } if (flag) { AddImage(PictureDescriptor.INVALIDIMAGEDATA, height, width, RPLFormat.Sizings.Clip); return; } Size size = default(Size); size.Height = WordOpenXmlUtils.ToEmus(height, 0, 20116800); size.Width = WordOpenXmlUtils.ToEmus(width, 0, 20116800); Size size2 = size; ImageHash hash = new SizingIndependentImageHash(new OfficeImageHasher(imgBuf).Hash); Relationship relationship = _document.WriteImageData(imgBuf, hash, extension); Size.Strategy strategy = Size.Strategy.AutoSize; switch (sizing) { case RPLFormat.Sizings.AutoSize: strategy = Size.Strategy.AutoSize; break; case RPLFormat.Sizings.Fit: strategy = Size.Strategy.Fit; break; case RPLFormat.Sizings.FitProportional: strategy = Size.Strategy.FitProportional; break; case RPLFormat.Sizings.Clip: strategy = Size.Strategy.Clip; break; } Size size3 = WordOpenXmlUtils.SizeImage(image, size2, strategy); Size desiredSize = (strategy == Size.Strategy.FitProportional || strategy == Size.Strategy.AutoSize) ? size3 : size2; GetCurrentParagraph().AddImage(new OpenXmlPictureModel(size3, desiredSize, sizing == RPLFormat.Sizings.Clip, NextPictureId(), NextPictureId(), relationship.RelationshipId, Path.GetFileName(relationship.RelatedPart))); }