locked
Reduce file size of a print quality image with programmatic text without losing print quality RRS feed

  • Question

  • I need to load an image file, set some text on it, and save it as an image for printing using the code below.       

     private static void A4SizeDpi600() {
                    const int dotsPerInch = 600; // define the quality in DPI
                    const double widthInInch = 10; // width of the bitmap in INCH
                    const double heightInInch = 8; // height of the bitmap in INCH
        
                    using (Bitmap bitmap = new Bitmap(Image.FromFile(imageFilePath), (int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch))) //10*600=6000, 8*600=4800
                    {
                        bitmap.SetResolution(dotsPerInch, dotsPerInch);
        
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                          
                            graphics.DrawString("Some text here", new Font("Times New Roman", 0.3f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 1400f, 2100f);
        ... //more text here
                        // Save the bitmap
                        bitmap.Save(imageBaseFilePath+ "CertDpi600.png");//file type can be any               
                    }
                }

    The problem is that the resulting image is too big (10MB), the input file is only 600K, which is more than 10 times in size . I want to know if there are ways to reduce the file size without losing printing quality.

    The resulting image file type can be any type (pdf, png, bmp, jpeg), as long as it has good printing quality and small file size.

    I am open to the input file type too (either any image file type, or pdf), as long as it produces the same requirement above.
    • Edited by Pingpong689 Monday, July 24, 2017 10:04 PM
    • Moved by CoolDadTx Tuesday, July 25, 2017 2:10 PM Winforms related
    Monday, July 24, 2017 6:39 PM

All replies

  • Hi Pingpong689,

    Sure you can reduce the picture with the following complete demo:

            public static string imageFilePath = @"C:\Users\v-baf\Desktop\Picon\8.jpg";
            public static string imageBaseFilePath = @"D:\New folder (9)\";
    
            private void button1_Click(object sender, EventArgs e)
            {
                A4SizeDpi600();
    
                Image img = Image.FromFile(imageBaseFilePath + "CertDpi600.png");
                SaveJpeg(imageBaseFilePath + "Result.png", img, 50);
            }
    
            private static void A4SizeDpi600()
            {
                const int dotsPerInch = 600; // define the quality in DPI
                const double widthInInch = 10; // width of the bitmap in INCH
                const double heightInInch = 8; // height of the bitmap in INCH
                using (Bitmap bitmap = new Bitmap(Image.FromFile(imageFilePath), (int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch))) //10*600=6000, 8*600=4800
                {
                    bitmap.SetResolution(dotsPerInch, dotsPerInch);
    
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
    
                        graphics.DrawString("Some text here", new Font("Times New Roman", 0.3f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 1400f, 2100f);
                        //more text here
                        // Save the bitmap
    
                        bitmap.Save(imageBaseFilePath + "CertDpi600.png");//file type can be any 
                    }
                }
            }
    
            public static void SaveJpeg(string path, Image img, int quality)
            {
                if (quality < 0 || quality > 100)
                    throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");
    
                // Encoder parameter for image quality 
                EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                // JPEG image codec 
                ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = qualityParam;
    
                img.Save(path, jpegCodec, encoderParams);
            }
            private static ImageCodecInfo GetEncoderInfo(string mimeType)
            {
                // Get image codecs for all image formats 
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    
                // Find the correct image codec 
                for (int i = 0; i < codecs.Length; i++)
                    if (codecs[i].MimeType == mimeType)
                        return codecs[i];
    
                return null;
            }

    The Result screenshot:

    Refer to Reduce Image size C# [closed].

    Hope this helps!

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Wednesday, July 26, 2017 2:53 AM
    • Proposed as answer by Stanly Fan Friday, August 4, 2017 6:07 AM
    Wednesday, July 26, 2017 2:50 AM
  • Hi Stanly,

    Thank you for your advice. I will try it, and let you know.

    Regards

    Sunday, July 30, 2017 2:04 PM
  • Hi Pingpong689,

    So have you solved this problem now?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 1, 2017 5:19 AM
  • Hi,

    Please give me some time. will test it asap.

    Tuesday, August 1, 2017 7:40 PM
  • Hi Pingpong689,

    Have you solved this problem now?

    Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 10, 2017 7:43 AM