SSIS Package to Resize image

SSIS Package to Resize image.

This script helps you to resize images as you needed in percentage(Dynamically).

Step1 : drag n drop foreeach loop container.

Step2 : drag n drop script task in to foreach loop container.

Step3 : Right click on any part of your control flow and select variables to see list of variables.

Create two new string variable with name “ImageFileName” and “ImagePath” and assign your local image folder path to ImagePath Variable value.

Note: Here local path is “D:\Work\Images”.

[image5.png]

Step4: Right click on foreeachloop container and click on edit.Select Collection from left pane.

Select Foreeach File Enumerator in Enumerator and select your variable “@[User:ImagePath]” in directory and click ok.

SSIS Resize Image 2

Step5 : Go to variable mappings and select imagefilename variable with index 0 and click ok.

SSIS Resize Image 3

Step6: Right click on script task and click on edit.

Select Script from left pane.

Select variable imagefilename for readonlyvariables.

SSIS Resize Image 4

Step7: Click on Edit Script and paste below code.

#region Help:  Introduction to the script task

/* The Script Task allows you to perform virtually any operation that can be accomplished in

  * a .Net application within the context of an Integration Services control flow.

  *

  * Expand the other regions which have "Help" prefixes for examples of specific ways to use

  * Integration Services features within this script task. */

 #endregion

 #region Namespaces

 using System;

 using System.Data;

 using System.Drawing;

 using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

using Microsoft.SqlServer.Dts.Runtime;

using System.Windows.Forms;

#endregion

namespace ST_f8e4dbfd074e4f9296466d7c3bc1f83d

{

    ///

    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,

    /// or parent of this class.

    ///
   [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]

    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

  {

       public void Main()

       {

             try

             {// TODO: Add your code here

                 string ImageFileName;

                 ImageFileName = Dts.Variables["ImageFileName"].Value.ToString();

                 // Get the scale factor.

                 using (Image img = Image.FromFile(ImageFileName))

                 {

                     int percent = 25;

                     //int i;

                     //for (i= img.Height; i> 100; i= (img.Height * 90) / 100)

                     //{

                     //    percent = percent - 10;

                     //}

                     int width = Convert.ToInt32(img.Width * (percent * 0.01));

                     int height = Convert.ToInt32(img.Height * (percent * 0.01));

                     ImageFormat format = img.RawFormat;

                     Image thumbNail = new Bitmap(width, height, img.PixelFormat);

                     Graphics g = Graphics.FromImage(thumbNail);

                     g.CompositingQuality = CompositingQuality.HighQuality;

                     g.SmoothingMode = SmoothingMode.HighQuality;

                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                     Rectangle rect = new Rectangle(0, 0, width, height);

                     g.DrawImage(img, rect);                   

                     img.Dispose();

                     thumbNail.Save(ImageFileName, format);

                 }

                 Dts.TaskResult = (int)ScriptResults.Success;

             }

             catch (Exception ex)

             {

                 MessageBox.Show(ex.Message.ToString());

             }

      }

         #region ScriptResults declaration

         ///

        /// This enum provides a convenient shorthand within the scope of this class for setting the

        /// result of the script.

        ///

        /// This code was generated automatically.

        ///
        enum ScriptResults

        {

            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,

            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure

        };

        #endregion

  }

}

Regards,
Nirav Gajjar