private PDFSize CalculateAppropriateImageSize(PDFImageData imgdata, PDFRect bounds) { //Find the dimension that makes sure the bounds are fully covered double scale = (bounds.Width.PointsValue / imgdata.DisplayWidth.PointsValue); double resultHeight = imgdata.DisplayHeight.PointsValue * scale; if (resultHeight < bounds.Height) { scale = bounds.Height.PointsValue / imgdata.DisplayHeight.PointsValue; } PDFUnit imgw = imgdata.DisplayWidth.PointsValue * scale; PDFUnit imgh = imgdata.DisplayHeight.PointsValue * scale; if (imgw > bounds.Width) { this.XPostion = -((imgw.PointsValue - bounds.Width.PointsValue) / 2.0); } if (imgh > bounds.Height) { this.YPostion = -((imgh.PointsValue - bounds.Height.PointsValue) / 2.0); } this.XStep = imgw; this.YStep = imgh; return(new PDFSize(imgw, imgh)); }
public static PDFImageData LoadImageFromUriData(string src, IPDFDocument document, IPDFComponent owner) { if (null == document) { throw new ArgumentNullException("document"); } if (null == owner) { throw new ArgumentNullException("owner"); } var dataUri = ParseDataURI(src); if (dataUri.encoding != "base64") { throw new ArgumentException("src", $"unsupported encoding {dataUri.encoding}; expected base64"); } if (dataUri.mediaType != "image/png") { throw new ArgumentException("src", $"unsupported encoding {dataUri.mediaType}; expected image/png"); } var binary = Convert.FromBase64String(dataUri.data); using (var ms = new System.IO.MemoryStream(binary)) { return(PDFImageData.LoadImageFromStream(document.GetIncrementID(owner.Type) + "data_png", ms, owner)); } }
private PDFSize CalculateAppropriateImageSize(PDFImageData imgdata) { if (this.XSize > 0 && this.YSize > 0) { //We have both explicit widths return(new PDFSize(this.XSize, this.YSize)); } PDFUnit imgw = imgdata.DisplayWidth; PDFUnit imgh = imgdata.DisplayHeight; //If we have one dimension, then calculate the other proportionally. if (this.XSize > 0) { imgh = this.XSize * (imgh.PointsValue / imgw.PointsValue); imgw = this.XSize; } else if (this.XSize > 0) { imgw = this.XSize * (imgw.PointsValue / imgh.PointsValue); imgh = this.XSize; } return(new PDFSize(imgw, imgh)); }
/// <summary> /// Creates a new PDFImageData instance from the specified bitmap. /// The sourceKey is a unique reference to this image data in the document /// </summary> /// <param name="sourcekey"></param> /// <param name="bitmap"></param> /// <returns></returns> public static PDFImageData LoadImageFromBitmap(string sourcekey, System.Drawing.Bitmap bitmap, bool compress = false) { if (null == bitmap) { throw new ArgumentNullException("bitmap"); } PDFImageData data = InitImageData(sourcekey, bitmap, compress); return(data); }
public static PDFImageData Parse(string data, bool compress) { if (string.IsNullOrEmpty(data) == false) { try { byte[] binary = System.Convert.FromBase64String(data); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(binary)) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms); return(PDFImageData.LoadImageFromBitmap(Guid.NewGuid().ToString(), bmp, compress)); } } catch (Exception ex) { throw new PDFException("Cannot convert the BASE64 string to image data", ex); } } else { return(null); } }