SaveJpeg() public static method

public static SaveJpeg ( Bitmap bitmap, string fileName, int quality ) : void
bitmap System.Drawing.Bitmap
fileName string
quality int
return void
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);
            }
        }