Beispiel #1
0
        private static void ApplySize(PrintElementImage image, dynamic size)
        {
            if (size != null)
            {
                PrintElementSize imageSize = null;

                double width;

                if (BuildHelper.TryToSizeInPixels(size.Width, size.SizeUnit, out width))
                {
                    imageSize = imageSize ?? new PrintElementSize();
                    imageSize.Width = width;
                }

                double height;

                if (BuildHelper.TryToSizeInPixels(size.Height, size.SizeUnit, out height))
                {
                    imageSize = imageSize ?? new PrintElementSize();
                    imageSize.Height = height;
                }

                image.Size = imageSize;
            }
        }
        private static IEnumerable <byte> ImageToPixels(PrintElementImage image)
        {
            //var bitmap = image.Source as BitmapSource;

            //if (bitmap != null)
            //{
            //    var stride = bitmap.PixelWidth * 4;
            //    var size = bitmap.PixelHeight * stride;
            //    var pixels = new byte[size];
            //    bitmap.CopyPixels(pixels, stride, 0);

            //    return pixels;
            //}

            //return null;

            var stream = image.Source;

            if (stream == null)
            {
                return(null);
            }

            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                return(ms.ToArray());
            }
        }
Beispiel #3
0
        public object Create(PrintElementBuildContext buildContext, dynamic elementMetadata)
        {
            PrintElementImage element = CreateBarcodeImage(buildContext, elementMetadata);

            BuildHelper.ApplyTextProperties(element, buildContext.ElementStyle);
            BuildHelper.ApplyTextProperties(element, elementMetadata);

            BuildHelper.ApplyInlineProperties(element, buildContext.ElementStyle);
            BuildHelper.ApplyInlineProperties(element, elementMetadata);

            return(element);
        }
        public void ShouldBuildImage()
        {
            // Given
            var context = HtmlBuilderTestHelper.CreateHtmlBuilderContext();
            var element = new PrintElementImage(_getStream(Resources.BarcodeQrRotate0));
            var result  = new TextWriterWrapper();

            // When
            context.Build(element, result.Writer);

            // Then
            Assert.AreEqual(Resources.ResultTestShouldBuildImage, result.GetText());
        }
        public static void AssertImagesAreEqual(Bitmap expected, PrintElementImage image)
        {
            var actual = new Bitmap(image.Source);

            Assert.AreEqual(expected.Width, actual.Width);
            Assert.AreEqual(expected.Height, actual.Height);

            for (var x = 0; x < expected.Width; x++)
            {
                for (var y = 0; y < expected.Height; y++)
                {
                    Assert.AreEqual(expected.GetPixel(x, y), actual.GetPixel(x, y), "X={0}, Y={1}", x, y);
                }
            }
        }
Beispiel #6
0
 private static PrintElementImage ApplyImageData(Stream imageStream, dynamic elementMetadata)
 {
     try
     {
         imageStream = ApplyRotation(imageStream, elementMetadata.Rotation);
         var image = new PrintElementImage(imageStream);
         ApplySize(image, elementMetadata.Size);
         ApplyStretch(image, elementMetadata.Stretch);
         return image;
     }
     catch
     {
     }
     return null;
 }
        public void ShouldApplyImageData()
        {
            // Given
            var image = Resources.ImageRotate0;
            dynamic elementMetadata = new DynamicWrapper();
            elementMetadata.Data = ImageTestHelper.BitmapToBase64(image);

            // When
            PrintElementImage element = BuildTestHelper.BuildImage(elementMetadata);

            // Then
            Assert.IsNotNull(element);
            Assert.IsInstanceOf<PrintElementImage>(element);
            Assert.IsNull(element.Size);
            ImageTestHelper.AssertImagesAreEqual(image, element);
        }
        public void ShouldApplyRotation180()
        {
            // Given
            var original = Resources.ImageRotate0;
            var expected = Resources.ImageRotate180;
            dynamic elementMetadata = new DynamicWrapper();
            elementMetadata.Data = ImageTestHelper.BitmapToBase64(original);
            elementMetadata.Rotation = "Rotate180";

            // When
            PrintElementImage element = BuildTestHelper.BuildImage(elementMetadata);

            // Then
            Assert.IsNotNull(element);
            Assert.IsNull(element.Size);
            ImageTestHelper.AssertImagesAreEqual(expected, element);
        }
        public void ShouldApplyText()
        {
            // Given
            var     expectedImage   = Resources.BarcodeEan13Rotate0;
            dynamic elementMetadata = new DynamicWrapper();

            elementMetadata.Text     = BarcodeEan13Text;
            elementMetadata.ShowText = false;

            // When
            PrintElementImage element = BuildTestHelper.BuildBarcodeEan13(elementMetadata);

            // Then
            Assert.IsNotNull(element);
            Assert.AreEqual(expectedImage.Width, element.Size.Width, 0.1);
            Assert.AreEqual(expectedImage.Height, element.Size.Height, 0.1);
            ImageTestHelper.AssertImagesAreEqual(expectedImage, element);
        }
Beispiel #10
0
        public void ShouldApplyErrorCorrectionHigh()
        {
            // Given
            var     expectedImage   = Resources.BarcodeQrErrorCorrectionHigh;
            dynamic elementMetadata = new DynamicWrapper();

            elementMetadata.Text            = BarcodeQrText;
            elementMetadata.ShowText        = false;
            elementMetadata.ErrorCorrection = "High";

            // When
            PrintElementImage element = BuildTestHelper.BuildBarcodeQr(elementMetadata);

            // Then
            Assert.IsNotNull(element);
            Assert.AreEqual(expectedImage.Width, element.Size.Width, 0.1);
            Assert.AreEqual(expectedImage.Height, element.Size.Height, 0.1);
            ImageTestHelper.AssertImagesAreEqual(expectedImage, element);
        }
        public void ShouldApplySize()
        {
            // Given
            var image = Resources.ImageRotate0;
            dynamic elementMetadata = new DynamicWrapper();
            elementMetadata.Size = new DynamicWrapper();
            elementMetadata.Size.Width = 2*image.Width;
            elementMetadata.Size.Height = 2*image.Height;
            elementMetadata.Size.SizeUnit = "Px";
            elementMetadata.Data = ImageTestHelper.BitmapToBase64(image);

            // When
            PrintElementImage element = BuildTestHelper.BuildImage(elementMetadata);

            // Then
            Assert.IsNotNull(element);
            Assert.AreEqual(2*image.Width, element.Size.Width, 0.1);
            Assert.AreEqual(2*image.Height, element.Size.Height, 0.1);
        }
        public static void ApplyImageStyles(this TextWriter result, PrintElementImage element)
        {
            if (element.Size != null)
            {
                if (element.Size.Width != null)
                {
                    result.Write("width:");
                    result.WriteInvariant(element.Size.Width);
                    result.Write("px;");
                }

                if (element.Size.Height != null)
                {
                    result.Write("height:");
                    result.WriteInvariant(element.Size.Height);
                    result.Write("px;");
                }
            }
        }
Beispiel #13
0
        private static void ApplyStretch(PrintElementImage image, dynamic stretch)
        {
            string stretchString;

            if (ConvertHelper.TryToNormString(stretch, out stretchString))
            {
                switch (stretchString)
                {
                    case "none":
                        image.Stretch = PrintElementStretch.None;
                        break;
                    case "fill":
                        image.Stretch = PrintElementStretch.Fill;
                        break;
                    case "uniform":
                        image.Stretch = PrintElementStretch.Uniform;
                        break;
                }
            }
        }
 public static void AssertImagesAreEqual(Bitmap expected, PrintElementImage image)
 {
     CollectionAssert.AreEqual(BitmapToPixels(expected), ImageToPixels(image));
 }
        public void ShouldBuildDocumentWithContents()
        {
            //Given
            var context = HtmlBuilderTestHelper.CreateHtmlBuilderContext();
            var element = new PrintViewDocument();
            var result  = new TextWriterWrapper();

            var image0   = new PrintElementImage(_getStream(Resources.ImageRotate0));
            var image90  = new PrintElementImage(_getStream(Resources.ImageRotate90));
            var image180 = new PrintElementImage(_getStream(Resources.ImageRotate180));
            var image270 = new PrintElementImage(_getStream(Resources.ImageRotate270));

            image0.Size = new PrintElementSize {
                Height = 50, Width = 150
            };
            image90.Size = new PrintElementSize {
                Height = 150, Width = 50
            };
            image180.Size = new PrintElementSize {
                Height = 50, Width = 150
            };
            image270.Size = new PrintElementSize {
                Height = 150, Width = 50
            };

            var par1 = new PrintElementParagraph();

            par1.Inlines.Add(image0);
            par1.Inlines.Add(image90);
            par1.Inlines.Add(image180);
            par1.Inlines.Add(image270);

            var runNormal = new PrintElementRun {
                Text = "Normal"
            };
            var runSubscript = new PrintElementRun
            {
                Text = "Subscript",
                Font = new PrintElementFont {
                    Variant = PrintElementFontVariant.Subscript
                }
            };
            var runSuperscript = new PrintElementRun
            {
                Text = "Superscript",
                Font = new PrintElementFont {
                    Variant = PrintElementFontVariant.Superscript
                }
            };

            var par2 = new PrintElementParagraph();

            par2.Inlines.Add(runNormal);
            par2.Inlines.Add(runSubscript);
            par2.Inlines.Add(runSuperscript);

            var run = new PrintElementRun {
                Text = "White Foreground & Black Background"
            };

            var par3 = new PrintElementParagraph();

            par3.Foreground = "white";
            par3.Background = "black";

            par3.Inlines.Add(run);

            element.Blocks.Add(par1);
            element.Blocks.Add(par2);
            element.Blocks.Add(new PrintElementPageBreak());
            element.Blocks.Add(par3);

            element.PagePadding = new PrintElementThickness(100);

            //When
            context.Build(element, result.Writer);

            //Then
            Assert.AreEqual(Resources.ResultTestShouldBuildDocumentWithContents, result.GetText());
        }