/* RSP_Software 1982->2009 +55 (41) 9680_2731 - 3473'4370 arab@rspsoftware.com.br rspsoftware.com.br */ /* This code can convert all the Windows XP supported image formats to jpg with very good quality, the variable quality in the function microsoft_convert defines this, feel free to contact me for modifications and enhancements. */ using System; using System.Diagnostics; using System.IO; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; using System.Drawing.Imaging; using System.Drawing; namespace _your_namespace_here { public class JPG_convert_002 { [DllImport("kernel32.dll")] static extern void OutputDebugString(string lpOutputString); public static void dprintf(string data) { data = data + " _debug_"; OutputDebugString(data); } /* convert but with bad quality, avoid using this, use function microsoft_convert instead available below */ public static void save_thumbnail(string source_file, string thumb_file, int width, int height) { System.Drawing.Image pic_tmp; pic_tmp = System.Drawing.Image.FromFile(source_file); System.Drawing.Image thumb_pic; thumb_pic = pic_tmp.GetThumbnailImage(width, height, null, System.IntPtr.Zero); thumb_pic.Save(thumb_file); pic_tmp.Dispose(); thumb_pic.Dispose(); } /* this is the function to convert to jpeg with good quality */ public static void microsoft_convert(string input,string output,int width, int quality) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo codec = null; for (int i = 0; i < codecs.Length; i++) { if ("image/jpeg"==codecs[i].MimeType) { codec = codecs[i]; } } Image mg = Image.FromFile(input); int height; volta: height = Convert.ToInt32((((double)width) / ((double)mg.Width)) * ((double)mg.Height)); /* change the values as you wish, for me this is ok, it will limit the width as 800 and the height as 600 */ if ((width > 800) || (height > 600)) { width--; goto volta; } Size newSize = new Size(width, height); Bitmap bp = new Bitmap(newSize.Width, newSize.Height); Graphics e = Graphics.FromImage(bp); e.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; e.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; e.PixelOffsetMode = System.Drawing.Drawing2D . PixelOffsetMode.HighQuality; Rectangle rect=new Rectangle(0,0,newSize.Width,newSize.Height); e.DrawImage(mg,rect,0,0,mg.Width,mg.Height,GraphicsUnit.Pixel); if (codec != null) { System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; // Create an EncoderParameters object. // An EncoderParameters object has an array of EncoderParameter // objects. In this case, there is only one // EncoderParameter object in the array. EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality); myEncoderParameters.Param[0] = myEncoderParameter; dprintf("width 7 " + width + " " + height); bp.Save(output, codec, myEncoderParameters); } else { dprintf("width 7 b " + width + " " + height); bp.Save(output); } } } }