Ejemplo n.º 1
0
        private string GenerateThumbnailId(string thumbnailId = null)
        {
            var ext = Common.GetImageExtension(FileFormat);

            if (ext == null)
            {
                throw new MediaUnsupportedException(string.Format("FileFormat: {0}", FileFormat));
            }

            string id;

            if (!string.IsNullOrEmpty(thumbnailId))
            {
                id = thumbnailId;
            }
            else
            {
                var fileName = new StringBuilder(Path.GetFileNameWithoutExtension(_source.Name));
                var ci       = CultureInfo.InvariantCulture;
                fileName.Append("[");
                fileName.Append(Size.Width.ToString(ci));
                fileName.Append("x");
                fileName.Append(Size.Height.ToString(ci));

                if (ColorManagement != null)
                {
                    fileName.Append(",cm(");
                    var sourceProfile = ColorManagement.GetProfile(ColorManagement, _source.Params.PixelFormat.ColorSpace);
                    if (sourceProfile != null)
                    {
                        fileName.Append(AjaxControls.Common.CalculateMD5(Encoding.UTF8.GetBytes(sourceProfile.Description)));
                    }

                    var targetProfile = ColorManagement.GetTargetProfile(ColorManagement);
                    if (targetProfile != null)
                    {
                        fileName.Append(",");
                        fileName.Append(AjaxControls.Common.CalculateMD5(Encoding.UTF8.GetBytes(targetProfile.Description)));
                    }

                    fileName.Append(")");
                }

                if (ResizeMode != Transforms.ResizeMode.Resize)
                {
                    fileName.Append(",");
                    fileName.Append(ResizeMode);
                }

                fileName.Append("]");

                id = fileName.ToString().ToLowerInvariant();
            }

            return(string.Format("{0}.{1}", id, ext));
        }
Ejemplo n.º 2
0
        public void RenderLayers(PipelineElement writer,
                                 IEnumerable <Layer> layers,
                                 SizeF workspace,
                                 float dpi                       = 300f,
                                 ColorSpace colorSpace           = ColorSpace.Rgb,
                                 Color backgroundColor           = null,
                                 bool isPreview                  = false,
                                 SizeF document                  = new SizeF(),
                                 ColorManagement colorManagement = null,
                                 float?targetDpi                 = null
                                 )
        {
            if (workspace.Width <= 0 || workspace.Height <= 0)
            {
                throw new ArgumentException(null, "workspace");
            }

            if (dpi <= 0)
            {
                throw new ArgumentOutOfRangeException("dpi", dpi, Exceptions.DpiOutOfRange);
            }

            colorSpace = colorSpace == ColorSpace.Unknown ? ColorSpace.Rgb : colorSpace;

            if (colorSpace != ColorSpace.Cmyk && colorSpace != ColorSpace.Grayscale && colorSpace != ColorSpace.Rgb)
            {
                throw new ArgumentException(null, "colorSpace");
            }

            // Turn on proofing if (colorManagement != null && isPreview)
            var initialTargetColorSpace = colorManagement != null ? colorManagement.TargetColorSpace : null;

            if (colorManagement != null)
            {
                colorManagement.TargetColorSpace = isPreview ? new ColorSpace?(colorSpace) : null;
                colorSpace = isPreview ? ColorSpace.Rgb : colorSpace;
            }

            backgroundColor = backgroundColor ?? ColorManagement.GetWhiteColor(colorSpace);

            var format  = ColorManagement.GetPixelFormat(colorSpace);
            var profile = ColorManagement.GetProfile(colorManagement, colorSpace, isPreview);

            var pixelSize = new Size(
                Common.ConvertPointsToPixels(dpi, workspace.Width),
                Common.ConvertPointsToPixels(dpi, workspace.Height));

            var pdfWriter = writer as PdfWriter;

            if (pdfWriter != null)
            {
                var docWidth  = pixelSize.Width;
                var docHeight = pixelSize.Height;
                var scale     = 1f;
                var offset    = PointF.Empty;
                if (!document.IsEmpty)
                {
                    docWidth  = Common.ConvertPointsToPixels(dpi, document.Width);
                    docHeight = Common.ConvertPointsToPixels(dpi, document.Height);

                    var scaleX = docWidth / (float)pixelSize.Width;
                    var scaleY = docHeight / (float)pixelSize.Height;

                    scale = System.Math.Min(scaleX, scaleY);
                    if (Utils.EqualsOfFloatNumbers(scaleX, scaleY, 0.001f))
                    {
                        scale     = (float)System.Math.Round(scale, 2);
                        docWidth  = Common.ConvertPointsToPixels(dpi, workspace.Width * scale);
                        docHeight = Common.ConvertPointsToPixels(dpi, workspace.Height * scale);
                    }
                    else
                    {
                        offset.X = (docWidth - pixelSize.Width * scale) / 2f;
                        offset.Y = (docHeight - pixelSize.Height * scale) / 2f;
                    }
                }

                pdfWriter.AddPage(docWidth, docHeight, dpi, dpi, backgroundColor, profile);
                using (var graphics = pdfWriter.GetGraphics())
                {
                    var path = new Path();
                    path.DrawRectangle(0, 0, pixelSize.Width, pixelSize.Height);

                    graphics.Transform.Scale(scale, scale, MatrixOrder.Append);
                    graphics.Transform.Translate(offset.X, offset.Y, MatrixOrder.Append);
                    graphics.ClippingPaths.Add(path);

                    var vObjects = GetVObjects(layers, workspace).ToArray();

                    using (var imageParams = new ImageGenerator(pixelSize, format, backgroundColor)
                    {
                        DpiX = dpi, DpiY = dpi, ColorProfile = profile
                    })
                    {
                        if (colorManagement != null)
                        {
                            colorManagement.InitColorMap(from v in vObjects from c in v.GetColors() select c, imageParams);
                        }

                        DrawVObjects(vObjects, graphics, imageParams, colorManagement);
                    }

                    graphics.ClippingPaths.Remove(path);
                    graphics.Transform.Translate(-offset.X, -offset.Y, MatrixOrder.Append);
                    graphics.Transform.Scale(1 / scale, 1 / scale, MatrixOrder.Append);
                }
            }
            else
            {
                var            vObjectGroups            = new List <List <VObject> >();
                List <VObject> currentVObjectGroup      = null;
                List <VObject> currentImageVObjectGroup = null;

                var vObjects = GetVObjects(layers, workspace).ToArray();

                foreach (var vObject in vObjects)
                {
                    if (vObject is IPipelineExtender)
                    {
                        if (currentVObjectGroup != null)
                        {
                            vObjectGroups.Add(currentVObjectGroup);
                            currentVObjectGroup = null;
                        }

                        if (currentImageVObjectGroup == null)
                        {
                            currentImageVObjectGroup = new List <VObject>();
                        }

                        currentImageVObjectGroup.Add(vObject);
                    }
                    else
                    {
                        if (currentImageVObjectGroup != null)
                        {
                            vObjectGroups.Add(currentImageVObjectGroup);
                            currentImageVObjectGroup = null;
                        }

                        if (currentVObjectGroup == null)
                        {
                            currentVObjectGroup = new List <VObject>();
                        }

                        currentVObjectGroup.Add(vObject);
                    }
                }

                if (currentVObjectGroup != null)
                {
                    vObjectGroups.Add(currentVObjectGroup);
                }

                if (currentImageVObjectGroup != null)
                {
                    vObjectGroups.Add(currentImageVObjectGroup);
                }

                var pipeline = new Pipeline();
                var deps     = new List <IDisposable>();

                var scale = dpi / targetDpi ?? 1;

                try
                {
                    var bitmapGenerator = new ImageGenerator(pixelSize.Width, pixelSize.Height, format, backgroundColor)
                    {
                        DpiX         = dpi / scale,
                        DpiY         = dpi / scale,
                        ColorProfile = profile
                    };

                    if (colorManagement != null)
                    {
                        colorManagement.InitColorMap(from v in vObjects from c in v.GetColors() select c, bitmapGenerator);
                    }

                    pipeline.Add(bitmapGenerator);

                    foreach (var group in vObjectGroups)
                    {
                        if (group.Count == 0)
                        {
                            break;
                        }

                        if (group.FirstOrDefault() is IPipelineExtender)
                        {
                            foreach (var vObject in group)
                            {
                                IEnumerable <IDisposable> vObjectDeps;
                                ((IPipelineExtender)vObject).ExtendPipeline(pipeline, bitmapGenerator, colorManagement, scale, out vObjectDeps);
                                deps.AddRange(vObjectDeps);
                            }
                        }
                        else
                        {
                            var groupVObjects = group.ToArray();
                            var drawer        = new Drawer();
                            drawer.Draw += (sender, e) =>
                            {
                                if (!Utils.EqualsOfFloatNumbers(scale, 1))
                                {
                                    e.Graphics.Transform.Scale(scale, scale);
                                }

                                DrawVObjects(groupVObjects, e.Graphics, bitmapGenerator, colorManagement);

                                if (!Utils.EqualsOfFloatNumbers(scale, 1))
                                {
                                    e.Graphics.Transform.Scale(1 / scale, 1 / scale);
                                }
                            };
                            pipeline.Add(drawer);
                        }
                    }

                    // Check if writer support pixel format with or without alpha.
                    if (!writer.IsPixelFormatSupported(bitmapGenerator.PixelFormat) &&
                        writer.IsPixelFormatSupported(PixelFormat.DiscardAlpha(bitmapGenerator.PixelFormat)))
                    {
                        pipeline.Add(new RemoveAlpha(backgroundColor));
                    }

                    if (!Utils.EqualsOfFloatNumbers(scale, 1))
                    {
                        pipeline.Add(new ResolutionModifier(dpi, dpi));
                    }

                    pipeline.Add(writer);
                    pipeline.Run();
                }
                finally
                {
                    pipeline.Remove(writer);
                    pipeline.DisposeAllElements();

                    if (deps != null)
                    {
                        foreach (var dep in deps)
                        {
                            dep.Dispose();
                        }
                    }

                    if (colorManagement != null)
                    {
                        colorManagement.TargetColorSpace = initialTargetColorSpace;
                    }
                }
            }

            if (colorManagement != null)
            {
                colorManagement.TargetColorSpace = initialTargetColorSpace;
            }
        }
Ejemplo n.º 3
0
        private static Bitmap GetTextBitmap(BaseTextVObject vObject, float dpi, Point location, bool extendBitmap, out string id)
        {
            if (vObject == null)
            {
                throw new ArgumentNullException("vObject");
            }

            if (dpi <= 0)
            {
                throw new ArgumentOutOfRangeException("dpi");
            }

            var rect   = vObject.MeasureText(dpi);
            var width  = (int)System.Math.Ceiling(rect.Width + (extendBitmap ? location.X * 2 : 0));
            var height = (int)System.Math.Ceiling(rect.Height + (extendBitmap ? location.Y * 2 : 0));

            if (rect.IsEmpty)
            {
                throw new InvalidOperationException("TextRenderer cannot write a text bitmap because it is empty");
            }

            var scale           = dpi / vObject.TargetDpi ?? 1;
            var colorManagement = vObject.GetColorManagement(true);
            var textBitmap      = new Bitmap(width, height, ColorManagement.GetPixelFormat(_colorSpace), RgbColor.Transparent)
            {
                DpiX         = dpi / scale,
                DpiY         = dpi / scale,
                ColorProfile = ColorManagement.GetProfile(colorManagement, _colorSpace, true)
            };

            using (var graphics = textBitmap.GetAdvancedGraphics())
            {
                graphics.Transform.Translate(-rect.X + location.X, -rect.Y + location.Y);

                if (!Utils.EqualsOfFloatNumbers(scale, 1))
                {
                    graphics.Transform.Scale(scale, scale);
                }

                var angle   = vObject.Angle;
                var opacity = vObject.Opacity;

                var curvedText = vObject as CurvedTextVObject;
                if (curvedText != null)
                {
                    curvedText.TextPath.RotateAt(-angle, curvedText.TextPath.GetFirstPoint());
                }

                vObject.Transform.Update(null, null, null, null, 0);
                vObject.Opacity = 1;

                vObject.DrawText(graphics, textBitmap, colorManagement);

                if (curvedText != null)
                {
                    curvedText.TextPath.RotateAt(angle, curvedText.TextPath.GetFirstPoint());
                }

                vObject.Transform.Update(null, null, null, null, angle);
                vObject.Opacity = opacity;
            }

            textBitmap.DpiX = dpi;
            textBitmap.DpiY = dpi;

            id = CreateFileId(vObject.GetMD5(dpi));

            return(textBitmap);
        }