Пример #1
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)this.renderInfo.FormatInfo;

            if (formatInfo.Failure == ImageFailure.None)
            {
                XImage xImage = null;
                try
                {
                    xImage = XImage.FromImageSource(formatInfo.ImageSource);
                }
                catch (InvalidOperationException ex)
                {
                    Debug.WriteLine(string.Format(AppResources.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)
                    {
                        if (usrWidthSet && usrHeightSet)
                        {
                            if (inherentHeight / usrHeight > inherentWidth / usrWidth)
                            {
                                usrWidthSet = false;
                            }
                            else
                            {
                                usrHeightSet = false;
                            }
                        }
                        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 || scaleHeightSet && scaleWidthSet && scaleHeight < scaleWidth)
                        {
                            resultHeight = resultHeight * scaleHeight;
                            resultWidth  = resultWidth * scaleHeight;
                        }
                        else if (scaleWidthSet || scaleHeightSet && scaleWidthSet && scaleHeight > scaleWidth)
                        {
                            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);
                        Debug.WriteLine(AppResources.EmptyImageSize);
                        this.failure = ImageFailure.EmptySize;
                    }
                    else
                    {
                        formatInfo.Width  = resultWidth;
                        formatInfo.Height = resultHeight;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format(AppResources.ImageNotReadable, image.Source.ToString(), 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;
            }
        }