Why Is File size SO Much Larger ?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
GlennIM
Posts: 23
Joined: 2018-07-22T22:22:12-07:00
Authentication code: 1152

Why Is File size SO Much Larger ?

Post by GlennIM »

When I use var image = new MagickImage(MagickColors.Red, 1000, 1000); the final output file size is 64K but when I use a real image .JPG file of 147K the resulting file is the size of 1.55 MB even after I compress it.

Why is the resulting .PNG file so much larger than the original using the code below?

Code: Select all

private void TextOnImage()
        {
            var image = new MagickImage(@"D:\Webs\mydomain.com\images\419608890268243889.jpg"); // 147K on size
            // var image = new MagickImage(MagickColors.Red, 1000, 1000);

            MagickImage imageresult;
            MagickImage modimage;

            try
            {
                using (var images = new MagickImageCollection())
                {
                    int widthOfImage = GetProperFontSize(image);

                    var topheaderSnippetreadSettings = new MagickReadSettings()
                    {
                        BackgroundColor = MagickColors.LightBlue,
                        FontFamily = "Showcard Gothic",
                        Width = image.Width - 190,
                        //FontPointsize = widthOfImage,
                        FillColor = MagickColors.Black,
                        FontWeight = FontWeight.Bold
                    };

                    var headerSnippetreadSettings = new MagickReadSettings()
                    {
                        BackgroundColor = MagickColors.LightBlue,
                        FontFamily = "Showcard Gothic",
                        Width = image.Width - 60, // was 60
                        //FontPointsize = widthOfImage,
                        FillColor = MagickColors.Black,
                        FontWeight = FontWeight.Bold
                    };

                    // This will make the background of the label Khaki.
                    var readSettings = new MagickReadSettings()
                    {
                        BackgroundColor = MagickColors.LightBlue,
                        FontFamily = "Helvetica-Condensed-Light", // -font Helvetica-Condensed-Light
                        FontPointsize = widthOfImage, // -pointsize 26
                        FillColor = MagickColors.Black,
                        FontWeight = FontWeight.Bold
                    };

                    // Create the label image.
                    var branding = new MagickImage("label:MyDomain.com", readSettings);

                    // Create the label image.
                    var topheading = new MagickImage("label:Great Kitchen Decor Ideas", topheaderSnippetreadSettings);
                    // var topheading = new MagickImage("label:Our Site Is Full of Great Cooking Tips", topheaderSnippetreadSettings);

                    // Create the label image.
                    var bottomheading = new MagickImage("label:Great Cooking Tips From Our Kitchen to Your Table", headerSnippetreadSettings);

                    branding.Extent(image.Width, 50, Gravity.Center);

                    topheading.Extent(image.Width, 50, Gravity.Center);

                    bottomheading.Extent(image.Width, 50, Gravity.Center);

                    // add the images to the collection
                    images.Add(topheading);
                    images.Add(image);
                    images.Add(bottomheading);
                    images.Add(branding);

                    // Append the images to create the output image.
                    using (var result = images.AppendVertically())
                    {
                        imageresult = new MagickImage(result);

                        // imageresult gets disposed of in the rounding code below, so need to write the imageresult to a new image
                        modimage = new MagickImage(imageresult);
                    }
                }

                using (imageresult)
                {
                    using (var mask = new MagickImage(MagickColors.White, imageresult.Width, imageresult.Height))
                    {
                        var size = 150;

                        new ImageMagick.Drawables()
                            .FillColor(MagickColors.Black)
                            .StrokeColor(MagickColors.Black)
                            .Polygon(new PointD(0, 0), new PointD(0, size), new PointD(size, 0))
                            .Polygon(new PointD(mask.Width, 0), new PointD(mask.Width, size), new PointD(mask.Width - size, 0))
                            .Polygon(new PointD(0, mask.Height), new PointD(0, mask.Height - size), new PointD(size, mask.Height))
                            .Polygon(new PointD(mask.Width, mask.Height), new PointD(mask.Width, mask.Height - size), new PointD(mask.Width - size, mask.Height))
                            .FillColor(MagickColors.White)
                            .StrokeColor(MagickColors.White)
                            .Circle(size, size, size, 0)
                            .Circle(mask.Width - size, size, mask.Width - size, 0)
                            .Circle(size, mask.Height - size, 0, mask.Height - size)
                            .Circle(mask.Width - size, mask.Height - size, mask.Width - size, mask.Height)
                            // .BorderColor(MagickColors.Red)
                            .Draw(mask);

                        // This copies the pixels that were already transparent on the mask.
                        using (var imageAlpha = imageresult.Clone())
                        {
                            imageAlpha.Alpha(AlphaOption.Extract);
                            imageAlpha.Opaque(MagickColors.White, MagickColors.None);
                            mask.Composite(imageAlpha, CompositeOperator.Over);
                        }

                        mask.HasAlpha = false;
                        imageresult.HasAlpha = false;
                        imageresult.Composite(mask, CompositeOperator.CopyAlpha);

                        // imageresult gets disposed of so need to write the imageresult to a new image
                        modimage = new MagickImage(imageresult);
                    }
                 }

                pinImagePB.Image = modimage.ToBitmap();
                // resize the image to fit the PictureBox size
                pinImagePB.SizeMode = PictureBoxSizeMode.StretchImage;

                modimage.Write(@"D:\TestImage.png");

                FileInfo imageMod = new FileInfo(@"D:\TestImage.png");

                ImageOptimizer optimizer = new ImageOptimizer();
                optimizer.OptimalCompression = true;

                // optimizer.LosslessCompress(imageMod);
                optimizer.Compress(imageMod);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Why Is File size SO Much Larger ?

Post by snibgo »

You don't show your images so comment is difficult. However, for ordinary photos, JPG compresses far more effectively than PNG. JPG was designed for photos; PNG was designed for graphics.
snibgo's IM pages: im.snibgo.com
GlennIM
Posts: 23
Joined: 2018-07-22T22:22:12-07:00
Authentication code: 1152

Re: Why Is File size SO Much Larger ?

Post by GlennIM »

The reason I am writing the file to .PNG is because this function includes code to round the corners of the image. If I write it to .JPG format the rounded corners to show. The whole image is just square but the file size is OK at 198K

- Please advise on how I can have rounded corners but use .JPG instead of .PNG which is many times larger than the original.

-Glenn
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why Is File size SO Much Larger ?

Post by fmw42 »

You could try a compressed TIFF format for output. TIFF supports transparency and also compression.
Post Reply