Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Fedor  
#1 Posted : Friday, September 6, 2013 12:28:14 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
I have created a sample how to create 3D-preview of business card using Graphics Mill. You can find a sample rendering bellow.

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            CreateBusinessCardPreview(@"C:\template.psd", @"C:\result.png";
        }


        private void CreateBusinessCardPreview(string sourcePath, string destPath)
        {
            //Inititally we draw on image of large size for better quality
            var size = 1000f;
            var pileHeight = size * 0.08f;
            var card1Left = size * 0.05f;
            var card2Top = size * 0.2f;
            var resultBgColor = new Aurigma.GraphicsMill.RgbColor(55, 55, 55, 255);
            var pileFoldColor = System.Drawing.Color.FromArgb(255, 210, 210, 210);

            System.Drawing.PointF[] card1Shape = {
                new System.Drawing.PointF(size * 0.222f, 0f),
                new System.Drawing.PointF(0f, size * 0.31f),
                new System.Drawing.PointF(size * 0.871f, size * 0.53f),
                new System.Drawing.PointF(size, size * 0.166f)
            };

            System.Drawing.PointF[] card2Shape = {
                new System.Drawing.PointF(size * 0.163f, 0f),
                new System.Drawing.PointF(0f, size * 0.444f),
                new System.Drawing.PointF(size * 0.9f, size * 0.652f),
                new System.Drawing.PointF(size, size * 0.16f)
            };

            System.Drawing.PointF[] pileShape = {
                new System.Drawing.PointF(card1Shape[1].X + card1Left, card1Shape[1].Y),
                new System.Drawing.PointF(card1Shape[2].X + card1Left, card1Shape[2].Y),
                new System.Drawing.PointF(card1Shape[3].X + card1Left, card1Shape[3].Y),
                new System.Drawing.PointF(card1Shape[3].X + card1Left, card1Shape[3].Y + pileHeight),
                new System.Drawing.PointF(card1Shape[2].X + card1Left, card1Shape[2].Y + pileHeight),
                new System.Drawing.PointF(card1Shape[1].X + card1Left, card1Shape[1].Y + pileHeight)
            };

            float[] pileOffsets = { 0.12f, 0.24f, 0.3f, 0.41f, 0.56f, 0.6f, 0.72f, 0.88f, 0.92f};

            int resultWidth = (int)(Math.Ceiling(card1Shape[3].X + card1Left));
            int resultHeight = (int)(Math.Ceiling(card2Shape[2].Y + card2Top));

            using (var psdReader = new Aurigma.GraphicsMill.Codecs.Psd.PsdReader(sourcePath))
            {
                using (var source = psdReader.MergedImageFrame.GetBitmap())
                {
                    //Add alpha channel to source image
                    source.ColorManagement.Convert(Aurigma.GraphicsMill.PixelFormat.Format32bppArgb);

                    using (var result = new Aurigma.GraphicsMill.Bitmap(resultWidth, resultHeight, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb, resultBgColor))
                    using (var card1 = GetProjectedBitmap(source, 150, card1Shape))
                    using (var card2 = GetProjectedBitmap(source, 200, card2Shape))
                    {
                        //Draw card pile
                        var pileBgColor = GetPileColor(source);

                        using (var graphics = result.GetGdiPlusGraphics())
                        using (var brush = new System.Drawing.SolidBrush(pileBgColor))
                        using (var pen = new System.Drawing.Pen(pileFoldColor, 1))
                        {
                            graphics.FillPolygon(brush, pileShape);

                            foreach (float k in pileOffsets)
                            {
                                graphics.DrawLines(pen, new System.Drawing.PointF[] {
                                    new System.Drawing.PointF(card1Shape[1].X + card1Left, card1Shape[1].Y + pileHeight * k),
                                    new System.Drawing.PointF(card1Shape[2].X + card1Left, card1Shape[2].Y + pileHeight * k),
                                    new System.Drawing.PointF(card1Shape[3].X + card1Left, card1Shape[3].Y + pileHeight * k)});
                            }
                        }

                        //Draw cards
                        using (var combiner = new Aurigma.GraphicsMill.Transforms.Combiner())
                        {
                            combiner.Mode = Aurigma.GraphicsMill.Transforms.CombineMode.Alpha;

                            //Combine first card
                            combiner.TopImage = card1;
                            combiner.X = (int)(card1Left);
                            combiner.Y = 0;
                            result.ApplyTransform(combiner);

                            //Combine second card
                            combiner.TopImage = card2;
                            combiner.X = 0;
                            combiner.Y = (int)(card2Top);
                            result.ApplyTransform(combiner);
                        }

                        //Do final transforms (resize, crop) here
                        result.Transforms.Resize(result.Width / 3, result.Height / 3, Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode.High);

                        result.Save(destPath);
                    }
                }
            }
        }


        private Aurigma.GraphicsMill.Bitmap GetProjectedBitmap(Aurigma.GraphicsMill.Bitmap source, int lightness, System.Drawing.PointF[] targetShape)
        {
            using (var bitmap = new Aurigma.GraphicsMill.Bitmap(source))
            {
                //Reduce light with radial gradient for more realistic rendering
                using (var lighGradient = new Aurigma.GraphicsMill.Bitmap(source.Width, source.Height, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb))
                {
                    using (var graphics = lighGradient.GetGdiPlusGraphics())
                    {
                        var c = new System.Drawing.Rectangle(source.Width / 4 - source.Width, source.Height / 2 - source.Width
                            , source.Width * 2, source.Width * 2);
                        var path = new System.Drawing.Drawing2D.GraphicsPath();
                        path.AddEllipse(c);

                        using (var pgb = new System.Drawing.Drawing2D.PathGradientBrush(path))
                        {
                            pgb.CenterColor = System.Drawing.Color.FromArgb(255, 255, 255, 255);
                            pgb.SurroundColors = new System.Drawing.Color[] { 
                                System.Drawing.Color.FromArgb(255, lightness, lightness, lightness) };

                            graphics.FillEllipse(pgb, c);
                        }
                    }

                    using (var combiner = new Aurigma.GraphicsMill.Transforms.Combiner())
                    {
                        combiner.Mode = Aurigma.GraphicsMill.Transforms.CombineMode.Subtract;
                        combiner.TopImage = lighGradient;
                        combiner.X = 0;
                        combiner.Y = 0;
                        bitmap.ApplyTransform(combiner);
                    }
                }

                //Make projective transformation
                System.Drawing.PointF[] sourceShape = {
                    new System.Drawing.PointF(0f, 0f),
                    new System.Drawing.PointF(0f, bitmap.Height),
                    new System.Drawing.PointF(bitmap.Width, bitmap.Height),
                    new System.Drawing.PointF(bitmap.Width, 0f)
                };

                using (var matrix = Aurigma.GraphicsMill.Transforms.Matrix.CreateFromProjectivePoints(sourceShape, targetShape))
                {
                    using (var transform = new Aurigma.GraphicsMill.Transforms.MatrixTransform(matrix))
                    {
                        transform.InterpolationMode = Aurigma.GraphicsMill.Transforms.InterpolationMode.High;
                        transform.BackgroundColor = new Aurigma.GraphicsMill.RgbColor(255, 255, 255, 0);

                        return transform.Apply(bitmap);
                    }
                }
            }
        }


        private System.Drawing.Color GetPileColor(Aurigma.GraphicsMill.Bitmap source)
        {
            using (var bitmap = new Aurigma.GraphicsMill.Bitmap(source))
            {
                //Get last scan line
                bitmap.Transforms.Crop(0, 0, bitmap.Width, bitmap.Height);

                //Get average color
                while (bitmap.Width > 1)
                {
                    bitmap.Transforms.Resize(bitmap.Width / 2, 1, Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode.Lanczos);
                }

                bitmap.ColorAdjustment.Brightness(0.3f);

                return bitmap.GetPixel(0, 0);
            }
        }

Code:
blah

Edited by user Wednesday, February 12, 2014 5:48:45 AM(UTC)  | Reason: Not specified

File Attachment(s):
sample-template14.zip (5,367kb) downloaded 32 time(s).
Fedor attached the following image(s):
sample-preview1.png
sample-preview14.png
Best regards,

Fedor Skvortsov

Fedor  
#2 Posted : Monday, July 3, 2017 1:01:29 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Best regards,

Fedor Skvortsov

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.