Reverses a bitmap, effectively rotating it 180 degrees in 3D space about the Y axis. Results in a "mirror image" of the bitmap, reversed much as it would be in a mirror - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Reverses a bitmap, effectively rotating it 180 degrees in 3D space about the Y axis. Results in a "mirror image" of the bitmap, reversed much as it would be in a mirror

Demo Code


using System.Net;
using System.IO;//  w  w w  .j a v a 2s .c  om
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System;

public class Main{
        /// <summary>
      /// Reverses a bitmap, effectively rotating it 180 degrees in 3D space about
      /// the Y axis.  Results in a "mirror image" of the bitmap, reversed much
      /// as it would be in a mirror
      /// </summary>
      /// <param name="inputBmp"></param>
      /// <returns></returns>
      public static Bitmap ReverseBitmap(Bitmap inputBmp) {
         //Copy the bitmap to a new bitmap object
         Bitmap newBmp = (Bitmap)inputBmp.Clone();

         //Flip the bitmap
         newBmp.RotateFlip(RotateFlipType.RotateNoneFlipX);


         //The RotateFlip transformation converts bitmaps to memoryBmp,
         //which is uncool.  Convert back now
         return ConvertBitmap(newBmp, inputBmp.RawFormat);
      }
                /// <summary>Converts a bitmap to another bitmap format, returning the new converted
      ///     bitmap
      /// </summary>
      /// 
      /// <param name="inputBmp">Bitmap to convert</param>
      /// <param name="destFormat">Bitmap format to convert to</param>
      /// 
      /// <returns>A new bitmap object containing the input bitmap converted.
      ///     If the destination format and the target format are the same, returns
      ///     a clone of the destination bitmap.</returns>
      public static Bitmap ConvertBitmap(Bitmap inputBmp, System.Drawing.Imaging.ImageFormat destFormat) {
         //If the dest format matches the source format and quality/bpp not changing, just clone
         if (inputBmp.RawFormat.Equals(destFormat)) {
            return(Bitmap)inputBmp.Clone();
         }

         //Create an in-memory stream which will be used to save
         //the converted image
         System.IO.Stream imgStream = new System.IO.MemoryStream();

         //Save the bitmap out to the memory stream, using the format indicated by the caller
         inputBmp.Save(imgStream, destFormat);

         //At this point, imgStream contains the binary form of the
         //bitmap in the target format.  All that remains is to load it
         //into a new bitmap object
         Bitmap destBitmap = new Bitmap(imgStream);

         //Free the stream
         //imgStream.Close();
         //For some reason, the above causes unhandled GDI+ exceptions
         //when destBitmap.Save is called.  Perhaps the bitmap object reads
         //from the stream asynchronously?

         return destBitmap;
      }
        /// <summary>Converts a bitmap to another bitmap format, returning the new converted
      ///     bitmap
      /// </summary>
      /// 
      /// <param name="inputBmp">Bitmap to convert</param>
      /// <param name="destMimeType">MIME type of format to convert to</param>
      /// 
      /// <returns>A new bitmap object containing the input bitmap converted.
      ///     If the destination format and the target format are the same, returns
      ///     a clone of the destination bitmap.</returns>
      public static Bitmap ConvertBitmap(Bitmap inputBmp, String destMimeType) {
         return ConvertBitmap(inputBmp, ImageFormatFromMimeType(destMimeType));
      }
}

Related Tutorials