PurplePen.BitmapUtil.SaveJpeg C# (CSharp) Method

SaveJpeg() public static method

public static SaveJpeg ( Bitmap bitmap, string fileName, int quality ) : void
bitmap System.Drawing.Bitmap
fileName string
quality int
return void
        public static void SaveJpeg(Bitmap bitmap, string fileName, int quality)
        {
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            Encoder encoder = 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 encoderParameters = new EncoderParameters(1);

            EncoderParameter encoderParameter = new EncoderParameter(encoder, (long)quality);
            encoderParameters.Param[0] = encoderParameter;
            bitmap.Save(fileName, jgpEncoder, encoderParameters);
        }

Usage Example

Example #1
0
        // Create a bitmap file of the mapDisplay supplied at construction.
        // If mapperForWorldFile is not null and real world coords are defined, also create a world file.
        public void CreateBitmap(string fileName, RectangleF rect, ImageFormat imageFormat, float dpi, CoordinateMapper mapperForWorldFile)
        {
            float bitmapWidth, bitmapHeight; // size of the bitmap in pixels.
            int   pixelWidth, pixelHeight;   // bitmapWidth/Height, rounded up to integer.

            bitmapWidth  = (rect.Width / 25.4F) * dpi;
            bitmapHeight = (rect.Height / 25.4F) * dpi;
            pixelWidth   = (int)Math.Ceiling(bitmapWidth);
            pixelHeight  = (int)Math.Ceiling(bitmapHeight);

            Bitmap bitmap = new Bitmap(pixelWidth, pixelHeight, PixelFormat.Format24bppRgb);

            bitmap.SetResolution(dpi, dpi);

            // Set the transform
            Matrix transform = Geometry.CreateInvertedRectangleTransform(rect, new RectangleF(0, 0, bitmapWidth, bitmapHeight));

            // And draw.
            mapDisplay.Draw(bitmap, transform);

            // JPEG and GIF have special code paths because the default Save method isn't
            // really good enough.
            if (imageFormat == ImageFormat.Jpeg)
            {
                BitmapUtil.SaveJpeg(bitmap, fileName, 80);
            }
            else if (imageFormat == ImageFormat.Gif)
            {
                BitmapUtil.SaveGif(bitmap, fileName);
            }
            else
            {
                bitmap.Save(fileName, imageFormat);
            }

            bitmap.Dispose();

            if (mapperForWorldFile != null && mapperForWorldFile.HasRealWorldCoords)
            {
                string extension     = Path.GetExtension(fileName);
                string worldFileName = Path.ChangeExtension(fileName, WorldFileExtension(extension));
                CreateWorldFile(worldFileName, rect, bitmapWidth, bitmapHeight, mapperForWorldFile);
            }
        }