public PDFImageData LoadImageData(IPDFDocument document, IPDFComponent owner, string path) { try { var uri = new Uri(path); var param = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped); var name = System.IO.Path.GetFileNameWithoutExtension(param); // Standard System.Drawing routines to draw a bitmap // could load an image from SQL, use parameters, whatever is needed Bitmap bmp = new Bitmap(300, 100); using (Graphics graphics = Graphics.FromImage(bmp)) { graphics.FillRectangle(new SolidBrush(Color.LightBlue), new Rectangle(0, 0, 300, 100)); graphics.DrawString(name, new Font("Times", 12), new SolidBrush(Color.Blue), PointF.Empty); graphics.Flush(); } var dir = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal); var png = System.IO.Path.Combine(dir, "Temp.png"); bmp.Save(png); PDFImageData data = PDFImageData.LoadImageFromBitmap(path, bmp, false); return(data); } catch (Exception ex) { throw new ArgumentException("The image creation failed", ex); } }
public void BytesPerLine_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); int expected = ImageBytesPerLine; int actual = (int)target.BytesPerLine; Assert.AreEqual(expected, actual); }
public void PixelWidth_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); int expected = ImagePixelWidth; int actual = target.PixelWidth; Assert.AreEqual(expected, actual); }
public void ColorsPerSample_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); int expected = ImageColorsPerSample; int actual = target.ColorsPerSample; Assert.AreEqual(expected, actual); }
public void HorizontalResolution_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); int expected = ImageResolution; int actual = target.HorizontalResolution; Assert.AreEqual(expected, actual); }
public void LoadImageFromURI_Test() { string uri = ImageUrlPath; PDFImageData actual; actual = PDFImageData.LoadImageFromURI(uri); Assert.IsNotNull(actual); Assert.IsNotNull(actual.Data); }
// // Load Image methods // /// <summary> /// Creates a new image resource from the specified image data with the resource name. /// The imagedata.SourcePath will act as the resource key. /// </summary> /// <param name="imgdata">The image data this resource holds</param> /// <param name="name">The name of the image resource</param> /// <returns>An initialized imageXObject</returns> public static PDFImageXObject Load(PDFImageData imgdata, string name) { PDFImageXObject x = new PDFImageXObject(); x._src = imgdata.SourcePath; x._data = imgdata; x.Name = (Native.PDFName)name; return(x); }
public void LoadImageFromBitmap_Test() { string sourcekey = ImageFilePath; Bitmap bitmap = CreateImageBitmap(); PDFImageData actual = PDFImageData.LoadImageFromBitmap(sourcekey, bitmap, false); Assert.IsNotNull(actual); Assert.IsNotNull(actual.Data); }
public void DisplayWidth_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); double w = ((double)ImagePixelWidth) / ImageResolution; PDFUnit expected = new PDFUnit(w, PageUnits.Inches); PDFUnit actual = target.DisplayWidth; Assert.AreEqual(expected, actual); }
public void SourcePath_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); string expected = ImageFilePath; string actual = target.SourcePath; Assert.AreEqual(expected, actual); }
/// <summary> /// Loads an image from a base 64 data image /// </summary> /// <param name="document"></param> /// <param name="owner"></param> /// <param name="path"></param> /// <returns></returns> public Scryber.Drawing.PDFImageData LoadImageData(IPDFDocument document, IPDFComponent owner, string path) { path = GetBase64FromPath(path); var binary = Convert.FromBase64String(path); using (var ms = new System.IO.MemoryStream(binary)) { return(PDFImageData.LoadImageFromStream(document.GetIncrementID(owner.Type) + "data_png", ms, owner)); } }
public void GetSize_Test() { Bitmap bmp = CreateImageBitmap(); PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false); //GetSize() returns actual size based on pixels and resolution double w = ((double)ImagePixelWidth) / ImageResolution; double h = ((double)ImagePixelHeight) / ImageResolution; PDFSize expected = new PDFSize(new PDFUnit(w, PageUnits.Inches), new PDFUnit(h, PageUnits.Inches)); int accuracy = 0; PDFSize actual; actual = target.GetSize(); Assert.AreEqual(Math.Round(expected.Width.PointsValue, accuracy), Math.Round(actual.Width.PointsValue, accuracy)); Assert.AreEqual(Math.Round(expected.Height.PointsValue, accuracy), Math.Round(actual.Height.PointsValue, accuracy)); }
protected override Resources.PDFImageXObject InitImageXObject(PDFContextBase context, Style style) { Document doc = this.Document; if (null == doc) { throw new NullReferenceException(Errors.ParentDocumentCannotBeNull); } if (null != this.Data) { _xobj = null; if (!string.IsNullOrEmpty(this.ImageKey)) { _xobj = this.Document.GetImageResource(this.ImageKey, this, false); } if (null == _xobj) { string name; if (string.IsNullOrEmpty(this.ImageKey)) { name = "DataImage_" + this.Document.GetIncrementID(PDFObjectTypes.ImageXObject); } else { name = this.ImageKey; } System.ComponentModel.TypeConverter BitmapConverter = TypeDescriptor.GetConverter(typeof(Bitmap)); Bitmap img = (Bitmap)BitmapConverter.ConvertFrom(this.Data.Raw); PDFImageData data = PDFImageData.LoadImageFromBitmap(name, img, this.Compress); _xobj = PDFImageXObject.Load(data, name); this.Document.SharedResources.Add(_xobj); } } return(_xobj); }
public void LoadImageFromLocalFile_Test() { string tmp = System.IO.Path.GetTempFileName(); Bitmap bmp = CreateImageBitmap(); bmp.Save(tmp); try { string path = tmp; PDFImageData actual; actual = PDFImageData.LoadImageFromLocalFile(path); Assert.IsNotNull(actual); Assert.IsNotNull(actual.Data); } finally { System.IO.File.Delete(tmp); } }
private void RegisterBadgeData(PDFLayoutContext context) { _outputbadge = this.PageOwner.ShowBadge; if (!_outputbadge) { return; } ScryberBadgeStyle style; if (!this.FullStyle.TryGetItem(StyleKeys.BadgeItemKey, out style)) { style = new ScryberBadgeStyle(); } //get any existing badge resource registered in the document Document doc = context.DocumentLayout.DocumentComponent; string rsrcName = BadgeResourceStem; if (style.DisplayOption == BadgeType.BlackOnWhite) { rsrcName = rsrcName + BlackOnWhiteResourceName; } else if (style.DisplayOption == BadgeType.WhiteOnBlack) { rsrcName = rsrcName + WhiteOnBlackResourceName; } else if (style.DisplayOption == BadgeType.Environment) { rsrcName = rsrcName + EnvironmentBadgeName; } _badgexobj = doc.GetImageResource(rsrcName, this.PageOwner, false); if (null == _badgexobj) { //Document does not have a previous badge registed, so need to register it now. System.Resources.ResourceManager mgr = new System.Resources.ResourceManager("Scryber.Components.Properties.Resources", typeof(Page).Assembly); System.Drawing.Bitmap bmp; if (style.DisplayOption == BadgeType.BlackOnWhite) { bmp = mgr.GetObject(BlackOnWhiteResourceName) as System.Drawing.Bitmap; } else if (style.DisplayOption == BadgeType.WhiteOnBlack) { bmp = mgr.GetObject(WhiteOnBlackResourceName) as System.Drawing.Bitmap; } else { bmp = mgr.GetObject(EnvironmentBadgeName) as System.Drawing.Bitmap; } PDFImageData data = PDFImageData.LoadImageFromBitmap(rsrcName, bmp, false); string name = doc.GetIncrementID(PDFObjectTypes.ImageXObject); _badgexobj = PDFImageXObject.Load(data, name); doc.SharedResources.Add(_badgexobj); } //Make sure the badge is registered with the page too _badgexobj.RegisterUse(this.Resources, this.PageOwner); // calculate the position based on the X and Y Offset plus the corners. _badgePosition = new PDFPoint(style.XOffset, style.YOffset); _badgeSize = new PDFSize(_badgexobj.ImageData.DisplayWidth, _badgexobj.ImageData.DisplayHeight); PDFSize pageSize = this.Size; switch (style.Corner) { default: case Corner.TopLeft: //Do Nothing break; case Corner.TopRight: _badgePosition.X = (pageSize.Width - _badgePosition.X) - _badgeSize.Width; break; case Corner.BottomRight: _badgePosition.X = (pageSize.Width - _badgePosition.X) - _badgeSize.Width; _badgePosition.Y = (pageSize.Height - _badgePosition.Y) - _badgeSize.Height; break; case Corner.BottomLeft: _badgePosition.Y = (pageSize.Height - _badgePosition.Y) - _badgeSize.Height; break; } }
public virtual bool IsSupportedImageType(PDFImageData data) { return(true); }
protected override Resources.PDFImageXObject InitImageXObject(PDFContextBase context, Style style) { Document doc = this.Document; if (null == doc) { throw RecordAndRaise.NullReference(Errors.ParentDocumentCannotBeNull); } PDFImageXObject xobj = null; if (null != this.Data) { xobj = this.Document.GetImageResource(this.Data.SourcePath, this, false); if (null == xobj) { string name = this.Document.GetIncrementID(PDFObjectTypes.ImageXObject); xobj = PDFImageXObject.Load(this.Data, name); this.Document.SharedResources.Add(xobj); } } else if (String.IsNullOrEmpty(this.Source) == false) { string fullpath = this.Source; try { //Commented so the document checks the factories, and if not found calls back to this component to map the path after. //if (System.Uri.IsWellFormedUriString(fullpath, UriKind.Relative)) // fullpath = this.MapPath(fullpath); xobj = this.Document.GetImageResource(fullpath, this, true); } catch (Exception ex) { throw new PDFMissingImageException(string.Format(Errors.CouldNotLoadImageFromPath, this.Source), ex); } if (null == xobj) { PDFImageData data = null; try { data = this.Document.LoadImageData(this, fullpath); } catch (Exception ex) { throw new PDFMissingImageException(string.Format(Errors.CouldNotLoadImageFromPath, this.Source), ex); } if (null != data) { string name = this.Document.GetIncrementID(PDFObjectTypes.ImageXObject); xobj = PDFImageXObject.Load(data, name); this.Document.SharedResources.Add(xobj); } else { throw new PDFMissingImageException(string.Format(Errors.CouldNotLoadImageFromPath, this.Source)); } } } return(xobj); }