This is a discussion on Image Compression. Save images with any quality.. within the C# Programming forums, part of the Software Development category; Image Compression. Save images with any quality.. JPEG Compression --> save JPEG images with any quality Using this code you ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Image Compression. Save images with any quality.. JPEG Compression --> save JPEG images with any quality Using this code you can save an image (System.Drawing.Image) item as a jpeg image, specifying the quality of it. Jpeg quality is the same as compression so the lower the quality, the smaller the file size. It's simple to use, ie just do this: Code: Image myImage = SaveJpeg(destImagePath, myImage, 50); Code: // add this!
using System.Drawing.Imaging;
// Saves an image as a jpeg image, with the given quality
// Gets:
// path - Path to which the image would be saved.
// quality - An integer from 0 to 100, with 100 being the
// highest quality
public static void SaveJpeg(string path, Image img, long 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(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);
}
// Returns the image codec with the given mime type
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 - 1)); i++)
{
if ((codecs[i].MimeType == mimeType))
{
return codecs[i];
}
}
return null;
}
__________________ M.Ramesh Kumar Last edited by Mramesh : 02-25-2008 at 07:34 AM. |
| Sponsored Links |
| |||
| Import Namespaces : Code: using System.Drawing .NET Classes used : Creating a ThumbNail Image on Fly.. Code: private void SaveThumbNail (string folder, string filename)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(folder + filename);
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(250, 250, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
string thumbnailPath = folder + "\Thumbnail.JPG";
thumbnailImage.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
/// <summary>
/// Required, but not used
/// </summary>
/// <returns>true</returns>
public bool ThumbnailCallback()
{
return true;
}
__________________ M.Ramesh Kumar |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Image Compression. Save images with any quality.. | Mramesh | PHP Programming | 2 | 03-11-2008 01:16 AM |
| Image Compression. Save images with any quality.. | Mramesh | VB.NET Programming | 0 | 02-22-2008 02:43 AM |
| how do I save images to SQL DB? | H2o | ASP and ASP.NET Programming | 2 | 09-19-2007 12:00 AM |
| Select Thumbnail images and view the larger image | H2o | HTML, CSS and Javascript Coding Techniques | 1 | 08-07-2007 08:58 AM |
| Align images relative to a particular image? | oyu2o | HTML, CSS and Javascript Coding Techniques | 0 | 03-10-2007 10:30 PM |