locked
Reduce Image For Email Attachment RRS feed

  • Question

  • Hi,

    I am using below code to retrieve images from MySQL and attach it to an email. Sometimes images are large so email attachments will be too big.

    How can I make sure to reduce the image (file) size before attaching?

    while (sql_reader_attachment.Read())
    {
        if (sql_reader_attachment["customer_image"] != DBNull.Value)
        {
            byte[] rawData = (byte[])sql_reader_attachment["customer_image"];
    
            using (image_memory_stream = new MemoryStream(rawData))
            {
                attachment_memory_stream = new MemoryStream();
    
                Image.FromStream(image_memory_stream).Save(attachment_memory_stream, System.Drawing.Imaging.ImageFormat.Png);
    
                attachment_memory_stream.Seek(0, System.IO.SeekOrigin.Begin);
    
                Attachment customer_image = new Attachment(attachment_memory_stream, "Image-" + sql_reader_attachment["customer_image_id"].ToString() + ".png", "image/png");
                mail_message.Attachments.Add(customer_image);
            }
        }
    }
    
    

    Thanks,

    Jassim

    Saturday, June 18, 2016 7:10 PM

Answers

  • Please refer to the following thread for an example of how you could compress the System.Drawing.Image returned from the Image.FromStream method:

    Create thumbnail and reduce image size: http://stackoverflow.com/questions/684092/create-thumbnail-and-reduce-image-size

    using (image_memory_stream = new MemoryStream(rawData))
            {
                attachment_memory_stream = new MemoryStream();
    
                Image img = Image.FromStream(image_memory_stream)
                img.SaveCompressed(attachment_memory_stream, 10);
    
                ...
            }
    
    private static void SaveCompressed(this Image img, Stream stream, long quality) {
        EncoderParameters parameters = new EncoderParameters(1);
        parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
        img.Save(stream, GetCodecInfo("image/png"), parameters);
    }


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Monday, June 20, 2016 1:45 AM
    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:22 AM
    Sunday, June 19, 2016 5:57 PM

All replies

  • Please refer to the following thread for an example of how you could compress the System.Drawing.Image returned from the Image.FromStream method:

    Create thumbnail and reduce image size: http://stackoverflow.com/questions/684092/create-thumbnail-and-reduce-image-size

    using (image_memory_stream = new MemoryStream(rawData))
            {
                attachment_memory_stream = new MemoryStream();
    
                Image img = Image.FromStream(image_memory_stream)
                img.SaveCompressed(attachment_memory_stream, 10);
    
                ...
            }
    
    private static void SaveCompressed(this Image img, Stream stream, long quality) {
        EncoderParameters parameters = new EncoderParameters(1);
        parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
        img.Save(stream, GetCodecInfo("image/png"), parameters);
    }


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Monday, June 20, 2016 1:45 AM
    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:22 AM
    Sunday, June 19, 2016 5:57 PM
  • Hi Jassim,

    If you want to peform lossless compress, you could use ImageMagick which is  powerful image manipulation library. You could get it ImageMagick from NuGet.

    FileInfo snakewareLogo = new FileInfo("imagepath.jpg");
    
    Console.WriteLine("Bytes before: " + snakewareLogo.Length);
    //LosslessCompress this image
    ImageOptimizer optimizer = new ImageOptimizer();
    optimizer.LosslessCompress(snakewareLogo);
    
    snakewareLogo.Refresh();
    Console.WriteLine("Bytes after:  " + snakewareLogo.Length);

    For your issue, you need to store your image stream as a file then you could use optimizer.LosslessCompress method.

    Best Regards,
    Li Wang

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 20, 2016 5:49 AM