예제 #1
0
        internal override void Format(Area area, FormatInfo previousFormatInfo)
        {
            _imageFilePath = _image.GetFilePath(_documentRenderer.WorkingDirectory);
            // The Image is stored in the string if path starts with "base64:", otherwise we check whether the file exists.
            if (!_imageFilePath.StartsWith("base64:") &&
                !XImage.ExistsFile(_imageFilePath))
            {
                _failure = ImageFailure.FileNotFound;
                Debug.WriteLine(Messages2.ImageNotFound(_image.Name), "warning");
            }
            ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo;

            formatInfo.Failure   = _failure;
            formatInfo.ImagePath = _imageFilePath;
            CalculateImageDimensions();
            base.Format(area, previousFormatInfo);
        }
예제 #2
0
        private void CalculateImageDimensions()
        {
            XImage bip = null;

            try
            {
                _imageFile = File.OpenRead(_filePath);
                //System.Drawing.Bitmap bip2 = new System.Drawing.Bitmap(imageFile);
                bip = XImage.FromFile(_filePath);

                float  horzResolution;
                float  vertResolution;
                string ext         = Path.GetExtension(_filePath).ToLower();
                float  origHorzRes = (float)bip.HorizontalResolution;
                float  origVertRes = (float)bip.VerticalResolution;

                _originalHeight = bip.PixelHeight * 72 / origVertRes;
                _originalWidth  = bip.PixelWidth * 72 / origHorzRes;

                if (_image.IsNull("Resolution"))
                {
                    horzResolution = (ext == ".gif") ? 72 : (float)bip.HorizontalResolution;
                    vertResolution = (ext == ".gif") ? 72 : (float)bip.VerticalResolution;
                }
                else
                {
                    horzResolution = (float)GetValueAsIntended("Resolution");
                    vertResolution = horzResolution;
                }

                Unit origHeight = bip.Size.Height * 72 / vertResolution;
                Unit origWidth  = bip.Size.Width * 72 / horzResolution;

                _imageHeight = origHeight;
                _imageWidth  = origWidth;

                bool  scaleWidthIsNull  = _image.IsNull("ScaleWidth");
                bool  scaleHeightIsNull = _image.IsNull("ScaleHeight");
                float sclHeight         = scaleHeightIsNull ? 1 : (float)GetValueAsIntended("ScaleHeight");
                _scaleHeight = sclHeight;
                float sclWidth = scaleWidthIsNull ? 1 : (float)GetValueAsIntended("ScaleWidth");
                _scaleWidth = sclWidth;

                bool doLockAspectRatio = _image.IsNull("LockAspectRatio") || _image.LockAspectRatio;

                if (doLockAspectRatio && (scaleHeightIsNull || scaleWidthIsNull))
                {
                    if (!_image.IsNull("Width") && _image.IsNull("Height"))
                    {
                        _imageWidth  = _image.Width;
                        _imageHeight = origHeight * _imageWidth / origWidth;
                    }
                    else if (!_image.IsNull("Height") && _image.IsNull("Width"))
                    {
                        _imageHeight = _image.Height;
                        _imageWidth  = origWidth * _imageHeight / origHeight;
                    }
                    else if (!_image.IsNull("Height") && !_image.IsNull("Width"))
                    {
                        _imageWidth  = _image.Width;
                        _imageHeight = _image.Height;
                    }
                    if (scaleWidthIsNull && !scaleHeightIsNull)
                    {
                        _scaleWidth = _scaleHeight;
                    }
                    else if (scaleHeightIsNull && !scaleWidthIsNull)
                    {
                        _scaleHeight = _scaleWidth;
                    }
                }
                else
                {
                    if (!_image.IsNull("Width"))
                    {
                        _imageWidth = _image.Width;
                    }
                    if (!_image.IsNull("Height"))
                    {
                        _imageHeight = _image.Height;
                    }
                }
                return;
            }
            catch (FileNotFoundException)
            {
                Debug.WriteLine(Messages2.ImageNotFound(_image.Name), "warning");
            }
            catch (Exception exc)
            {
                Debug.WriteLine(Messages2.ImageNotReadable(_image.Name, exc.Message), "warning");
            }
            finally
            {
                if (bip != null)
                {
                    bip.Dispose();
                }
            }

            //Setting defaults in case an error occured.
            _imageFile   = null;
            _imageHeight = (Unit)GetValueOrDefault("Height", Unit.FromInch(1));
            _imageWidth  = (Unit)GetValueOrDefault("Width", Unit.FromInch(1));
            _scaleHeight = (double)GetValueOrDefault("ScaleHeight", 1.0);
            _scaleWidth  = (double)GetValueOrDefault("ScaleWidth", 1.0);
        }