コード例 #1
0
        internal override void Render()
        {
            RenderFilling();

            ImageFormatInfo formatInfo  = (ImageFormatInfo)_renderInfo.FormatInfo;
            Area            contentArea = _renderInfo.LayoutInfo.ContentArea;
            XRect           destRect    = new XRect(contentArea.X, contentArea.Y, formatInfo.Width, formatInfo.Height);

            if (formatInfo.Failure == ImageFailure.None)
            {
                XImage xImage = null;
                try
                {
                    XRect srcRect = new XRect(formatInfo.CropX, formatInfo.CropY, formatInfo.CropWidth, formatInfo.CropHeight);
                    //xImage = XImage.FromFile(formatInfo.ImagePath);
                    xImage = CreateXImage(formatInfo.ImagePath);
                    _gfx.DrawImage(xImage, destRect, srcRect, XGraphicsUnit.Point); //Pixel.
                }
                catch (Exception)
                {
                    RenderFailureImage(destRect);
                }
                finally
                {
                    if (xImage != null)
                    {
                        xImage.Dispose();
                    }
                }
            }
            else
            {
                RenderFailureImage(destRect);
            }

            RenderLine();
        }
コード例 #2
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)this.renderInfo.FormatInfo;

            if (formatInfo.failure == ImageFailure.None)
            {
                XImage xImage = null;
                try
                {
                    xImage = XImage.FromFile(this.imageFilePath);
                }
                catch (InvalidOperationException ex)
                {
                    Trace.WriteLine(Messages.InvalidImageType(ex.Message));
                    formatInfo.failure = ImageFailure.InvalidType;
                }

                try
                {
                    XUnit usrWidth     = image.Width.Point;
                    XUnit usrHeight    = image.Height.Point;
                    bool  usrWidthSet  = !this.image.IsNull("Width");
                    bool  usrHeightSet = !this.image.IsNull("Height");

                    XUnit resultWidth  = usrWidth;
                    XUnit resultHeight = usrHeight;

                    double xPixels          = xImage.PixelWidth;
                    bool   usrResolutionSet = !image.IsNull("Resolution");

                    double horzRes        = usrResolutionSet ? (double)image.Resolution : xImage.HorizontalResolution;
                    XUnit  inherentWidth  = XUnit.FromInch(xPixels / horzRes);
                    double yPixels        = xImage.PixelHeight;
                    double vertRes        = usrResolutionSet ? (double)image.Resolution : xImage.VerticalResolution;
                    XUnit  inherentHeight = XUnit.FromInch(yPixels / vertRes);

                    bool lockRatio = this.image.IsNull("LockAspectRatio") ? true : image.LockAspectRatio;

                    double scaleHeight    = this.image.ScaleHeight;
                    double scaleWidth     = this.image.ScaleWidth;
                    bool   scaleHeightSet = !this.image.IsNull("ScaleHeight");
                    bool   scaleWidthSet  = !this.image.IsNull("ScaleWidth");

                    if (lockRatio && !(scaleHeightSet && scaleWidthSet))
                    {
                        if (usrWidthSet && !usrHeightSet)
                        {
                            resultHeight = inherentHeight / inherentWidth * usrWidth;
                        }
                        else if (usrHeightSet && !usrWidthSet)
                        {
                            resultWidth = inherentWidth / inherentHeight * usrHeight;
                        }
                        else if (!usrHeightSet && !usrWidthSet)
                        {
                            resultHeight = inherentHeight;
                            resultWidth  = inherentWidth;
                        }

                        if (scaleHeightSet)
                        {
                            resultHeight = resultHeight * scaleHeight;
                            resultWidth  = resultWidth * scaleHeight;
                        }
                        if (scaleWidthSet)
                        {
                            resultHeight = resultHeight * scaleWidth;
                            resultWidth  = resultWidth * scaleWidth;
                        }
                    }
                    else
                    {
                        if (!usrHeightSet)
                        {
                            resultHeight = inherentHeight;
                        }

                        if (!usrWidthSet)
                        {
                            resultWidth = inherentWidth;
                        }

                        if (scaleHeightSet)
                        {
                            resultHeight = resultHeight * scaleHeight;
                        }
                        if (scaleWidthSet)
                        {
                            resultWidth = resultWidth * scaleWidth;
                        }
                    }

                    formatInfo.CropWidth  = (int)xPixels;
                    formatInfo.CropHeight = (int)yPixels;
                    if (!this.image.IsNull("PictureFormat"))
                    {
                        PictureFormat picFormat = this.image.PictureFormat;
                        //Cropping in pixels.
                        XUnit cropLeft   = picFormat.CropLeft.Point;
                        XUnit cropRight  = picFormat.CropRight.Point;
                        XUnit cropTop    = picFormat.CropTop.Point;
                        XUnit cropBottom = picFormat.CropBottom.Point;
                        formatInfo.CropX       = (int)(horzRes * cropLeft.Inch);
                        formatInfo.CropY       = (int)(vertRes * cropTop.Inch);
                        formatInfo.CropWidth  -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch);
                        formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch);

                        //Scaled cropping of the height and width.
                        double xScale = resultWidth / inherentWidth;
                        double yScale = resultHeight / inherentHeight;

                        cropLeft   = xScale * cropLeft;
                        cropRight  = xScale * cropRight;
                        cropTop    = yScale * cropTop;
                        cropBottom = yScale * cropBottom;

                        resultHeight = resultHeight - cropTop - cropBottom;
                        resultWidth  = resultWidth - cropLeft - cropRight;
                    }
                    if (resultHeight <= 0 || resultWidth <= 0)
                    {
                        formatInfo.Width  = XUnit.FromCentimeter(2.5);
                        formatInfo.Height = XUnit.FromCentimeter(2.5);
                        Trace.WriteLine(Messages.EmptyImageSize);
                        this.failure = ImageFailure.EmptySize;
                    }
                    else
                    {
                        formatInfo.Width  = resultWidth;
                        formatInfo.Height = resultHeight;
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(Messages.ImageNotReadable(this.image.Name, ex.Message));
                    formatInfo.failure = ImageFailure.NotRead;
                }
                finally
                {
                    if (xImage != null)
                    {
                        xImage.Dispose();
                    }
                }
            }
            if (formatInfo.failure != ImageFailure.None)
            {
                if (!this.image.IsNull("Width"))
                {
                    formatInfo.Width = this.image.Width.Point;
                }
                else
                {
                    formatInfo.Width = XUnit.FromCentimeter(2.5);
                }

                if (!this.image.IsNull("Height"))
                {
                    formatInfo.Height = this.image.Height.Point;
                }
                else
                {
                    formatInfo.Height = XUnit.FromCentimeter(2.5);
                }
                return;
            }
        }
コード例 #3
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo;

            if (formatInfo.Failure == ImageFailure.None)
            {
                XImage xImage = null;
                try
                {
                    //xImage = XImage.FromFile(_imageFilePath);
                    xImage = CreateXImage(_imageFilePath);
                }
                catch (InvalidOperationException ex)
                {
                    Debug.WriteLine(Messages2.InvalidImageType(ex.Message));
                    formatInfo.Failure = ImageFailure.InvalidType;
                }

                if (formatInfo.Failure == ImageFailure.None)
                {
                    try
                    {
                        XUnit usrWidth     = _image.Width.Point;
                        XUnit usrHeight    = _image.Height.Point;
                        bool  usrWidthSet  = !_image._width.IsNull;
                        bool  usrHeightSet = !_image._height.IsNull;

                        XUnit resultWidth  = usrWidth;
                        XUnit resultHeight = usrHeight;

                        Debug.Assert(xImage != null);
                        double xPixels          = xImage.PixelWidth;
                        bool   usrResolutionSet = !_image._resolution.IsNull;

                        double horzRes = usrResolutionSet ? _image.Resolution : xImage.HorizontalResolution;
                        double vertRes = usrResolutionSet ? _image.Resolution : xImage.VerticalResolution;

// ReSharper disable CompareOfFloatsByEqualityOperator
                        if (horzRes == 0 && vertRes == 0)
                        {
                            horzRes = 72;
                            vertRes = 72;
                        }
                        else if (horzRes == 0)
                        {
                            Debug.Assert(false, "How can this be?");
                            horzRes = 72;
                        }
                        else if (vertRes == 0)
                        {
                            Debug.Assert(false, "How can this be?");
                            vertRes = 72;
                        }
                        // ReSharper restore CompareOfFloatsByEqualityOperator

                        XUnit  inherentWidth  = XUnit.FromInch(xPixels / horzRes);
                        double yPixels        = xImage.PixelHeight;
                        XUnit  inherentHeight = XUnit.FromInch(yPixels / vertRes);

                        //bool lockRatio = _image.IsNull("LockAspectRatio") ? true : _image.LockAspectRatio;
                        bool lockRatio = _image._lockAspectRatio.IsNull || _image.LockAspectRatio;

                        double scaleHeight = _image.ScaleHeight;
                        double scaleWidth  = _image.ScaleWidth;
                        //bool scaleHeightSet = !_image.IsNull("ScaleHeight");
                        //bool scaleWidthSet = !_image.IsNull("ScaleWidth");
                        bool scaleHeightSet = !_image._scaleHeight.IsNull;
                        bool scaleWidthSet  = !_image._scaleWidth.IsNull;

                        if (lockRatio && !(scaleHeightSet && scaleWidthSet))
                        {
                            if (usrWidthSet && !usrHeightSet)
                            {
                                resultHeight = inherentHeight / inherentWidth * usrWidth;
                            }
                            else if (usrHeightSet && !usrWidthSet)
                            {
                                resultWidth = inherentWidth / inherentHeight * usrHeight;
                            }
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
                            else if (!usrHeightSet && !usrWidthSet)
                            {
                                resultHeight = inherentHeight;
                                resultWidth  = inherentWidth;
                            }

                            if (scaleHeightSet)
                            {
                                resultHeight = resultHeight * scaleHeight;
                                resultWidth  = resultWidth * scaleHeight;
                            }
                            if (scaleWidthSet)
                            {
                                resultHeight = resultHeight * scaleWidth;
                                resultWidth  = resultWidth * scaleWidth;
                            }
                        }
                        else
                        {
                            if (!usrHeightSet)
                            {
                                resultHeight = inherentHeight;
                            }

                            if (!usrWidthSet)
                            {
                                resultWidth = inherentWidth;
                            }

                            if (scaleHeightSet)
                            {
                                resultHeight = resultHeight * scaleHeight;
                            }
                            if (scaleWidthSet)
                            {
                                resultWidth = resultWidth * scaleWidth;
                            }
                        }

                        formatInfo.CropWidth  = (int)xPixels;
                        formatInfo.CropHeight = (int)yPixels;
                        if (_image._pictureFormat != null && !_image._pictureFormat.IsNull())
                        {
                            PictureFormat picFormat = _image.PictureFormat;
                            //Cropping in pixels.
                            XUnit cropLeft   = picFormat.CropLeft.Point;
                            XUnit cropRight  = picFormat.CropRight.Point;
                            XUnit cropTop    = picFormat.CropTop.Point;
                            XUnit cropBottom = picFormat.CropBottom.Point;
                            formatInfo.CropX       = (int)(horzRes * cropLeft.Inch);
                            formatInfo.CropY       = (int)(vertRes * cropTop.Inch);
                            formatInfo.CropWidth  -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch);
                            formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch);

                            //Scaled cropping of the height and width.
                            double xScale = resultWidth / inherentWidth;
                            double yScale = resultHeight / inherentHeight;

                            cropLeft   = xScale * cropLeft;
                            cropRight  = xScale * cropRight;
                            cropTop    = yScale * cropTop;
                            cropBottom = yScale * cropBottom;

                            resultHeight = resultHeight - cropTop - cropBottom;
                            resultWidth  = resultWidth - cropLeft - cropRight;
                        }
                        if (resultHeight <= 0 || resultWidth <= 0)
                        {
                            formatInfo.Width  = XUnit.FromCentimeter(2.5);
                            formatInfo.Height = XUnit.FromCentimeter(2.5);
                            Debug.WriteLine(Messages2.EmptyImageSize);
                            _failure = ImageFailure.EmptySize;
                        }
                        else
                        {
                            formatInfo.Width  = resultWidth;
                            formatInfo.Height = resultHeight;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(Messages2.ImageNotReadable(_image.Name, ex.Message));
                        formatInfo.Failure = ImageFailure.NotRead;
                    }
                    finally
                    {
                        if (xImage != null)
                        {
                            xImage.Dispose();
                        }
                    }
                }
            }
            if (formatInfo.Failure != ImageFailure.None)
            {
                if (!_image._width.IsNull)
                {
                    formatInfo.Width = _image.Width.Point;
                }
                else
                {
                    formatInfo.Width = XUnit.FromCentimeter(2.5);
                }

                if (!_image._height.IsNull)
                {
                    formatInfo.Height = _image.Height.Point;
                }
                else
                {
                    formatInfo.Height = XUnit.FromCentimeter(2.5);
                }
            }
        }
コード例 #4
0
        XImage CreateBarcode()
        {
            XImage image = null;

            ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo;

#if CORE_WITH_GDI || GDI
            BarcodeFormat format = BarcodeFormat.All_1D;

            switch (_barcode.Type)
            {
            case BarcodeType.Barcode25i:
                format = BarcodeFormat.ITF;
                break;

            case BarcodeType.Barcode39:
                format = BarcodeFormat.CODE_39;
                break;

            case BarcodeType.Barcode128:
                format = BarcodeFormat.CODE_128;
                break;

            case BarcodeType.QRCode:
                format = BarcodeFormat.QR_CODE;
                break;

            default:
                break;
            }

            if (String.IsNullOrEmpty(_barcode.Code))
            {
                throw new ArgumentNullException("Code");
            }

            var options = new EncodingOptions();

            int horzPixels = (int)(formatInfo.Width.Inch * formatInfo.Resolution);
            int vertPixels = (int)(formatInfo.Height.Inch * formatInfo.Resolution);

            options.Width  = horzPixels;
            options.Height = vertPixels;

            var writer = new BarcodeWriter
            {
                Format  = format,
                Options = options
            };

            var bmp = writer.Write(_barcode.Code);

            string fileName = Path.GetTempFileName();

            bmp.SetResolution((float)formatInfo.Resolution, (float)formatInfo.Resolution);
            bmp.Save(fileName, ImageFormat.Png);

            image = XImage.FromFile(fileName);

            /*
             * if (File.Exists(fileName))
             *  File.Delete(fileName);
             */
#endif
            return(image);
        }