Below coding is used to describe how to compress the image to given width and height. It will used to compress any type of image file to jpg of provided width and height.
CommonConstant.CS
namespace HelperClass
{
#region[Directive]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion[Directive]
/// <summary>
/// This class is used to
get the constants
/// </summary>
public class CommonConstant
{
public const string JPEG = ".jpeg";
public const string PNG = ".png";
public const string JPG = ".jpg";
public const string BTM = ".btm";
}
}
ImageCompress.CS
namespace HelperClass
{
#region[Directive]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
#endregion[Directive]
/// <summary>
/// This class is used to
compress the image to
/// provided size
/// </summary>
public class ImageCompress
{
#region[PrivateData]
private static volatile ImageCompress
imageCompress;
private Bitmap
bitmap;
private int width;
private int height;
private Image img;
#endregion[Privatedata]
#region[Constructor]
/// <summary>
/// It is used to restrict
to create the instance of the
ImageCompress
/// </summary>
private ImageCompress()
{
}
#endregion[Constructor]
#region[Poperties]
/// <summary>
/// Gets ImageCompress
object
/// </summary>
public static ImageCompress GetImageCompressObject
{
get
{
if (imageCompress == null)
{
imageCompress = new ImageCompress();
}
return imageCompress;
}
}
/// <summary>
/// Gets or sets Width
/// </summary>
public int Height
{
get { return height;
}
set { height = value;
}
}
/// <summary>
/// Gets or sets Width
/// </summary>
public int Width
{
get { return width; }
set { width = value;
}
}
/// <summary>
/// Gets or sets Image
/// </summary>
public Bitmap
GetImage
{
get { return bitmap;
}
set { bitmap = value;
}
}
#endregion[Poperties]
#region[PublicFunction]
/// <summary>
/// This function is used
to save the image
/// </summary>
/// <param name="fileName"></param>
/// <param name="path"></param>
public void Save(string fileName, string
path)
{
if (ISValidFileType(fileName))
{
string pathaname = path + @"\"
+ fileName;
save(pathaname, 60);
}
}
#endregion[PublicFunction]
#region[PrivateData]
/// <summary>
/// This function is use to
compress the image to
/// predefine size
/// </summary>
/// <returns>return
bitmap in compress size</returns>
private Image
CompressImage()
{
if (GetImage != null)
{
Width =
(Width == 0) ? GetImage.Width : Width;
Height
= (Height == 0) ? GetImage.Height : Height;
Bitmap newBitmap = new
Bitmap(Width, Height, PixelFormat.Format24bppRgb);
newBitmap = bitmap;
newBitmap.SetResolution(80,80);
return newBitmap.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
}
else
{
throw new Exception("Please
provide bitmap");
}
}
/// <summary>
/// This function is used
to check the file Type
/// </summary>
/// <param name="fileName">String data type:contain the file name</param>
/// <returns>true or
false on the file extention</returns>
private bool
ISValidFileType(string fileName)
{
bool isValidExt = false;
string fileExt = Path.GetExtension(fileName);
switch (fileExt.ToLower())
{
case CommonConstant.JPEG:
case CommonConstant.BTM:
case CommonConstant.JPG:
case CommonConstant.PNG:
isValidExt = true;
break;
}
return isValidExt;
}
/// <summary>
/// This function is used
to get the imageCode info
/// on the basis of
mimeType
/// </summary>
/// <param name="mimeType">string data type</param>
/// <returns>ImageCodecInfo
data type</returns>
private ImageCodecInfo
GetImageCoeInfo(string mimeType)
{
ImageCodecInfo[] codes = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i
< codes.Length; i++)
{
if (codes[i].MimeType == mimeType)
{
return codes[i];
}
}
return null;
}
Renu gundetiPosted Jan 15, 2020, 3:19 PM
Private void save(string path,int quality) what quality need to pass and declared private but calling imgCompress.Save(upload1.FileName, @"C:\Documents and Settings\Rasmi\My Documents\Visual Studio2008\WebSites\compressImageFile\Logo"); not working
Mohamed FathyPosted Apr 28, 2016, 3:55 PM
Thank you so much. It was helpful for me.
Parvaiz AhmadPosted Aug 31, 2015, 8:41 AM
used the similar code on http://CompressPic.com and it worked. Thanks http://www.c-sharpcorner.com
nur fatehPosted Jul 8, 2014, 12:39 AM
how could u implement ths
Shwe Nann ThuPosted Dec 11, 2012, 4:56 AM
thanks you so much :D it is really helpful to me !!