bitmapmanipulator.cs

来自「图像管理源码」· CS 代码 · 共 856 行 · 第 1/3 页

CS
856
字号
		public static Bitmap RotateBitmapRight90(Bitmap inputBmp) {
			//Copy bitmap
			Bitmap newBmp = (Bitmap)inputBmp.Clone();

			newBmp.RotateFlip(RotateFlipType.Rotate90FlipNone);

			//The RotateFlip transformation converts bitmaps to memoryBmp,
			//which is uncool.  Convert back now
			return ConvertBitmap(newBmp, inputBmp.RawFormat);
		}

		public static Bitmap RotateBitmapRight180(Bitmap inputBmp) {
			//Copy bitmap
			Bitmap newBmp = (Bitmap)inputBmp.Clone();

			newBmp.RotateFlip(RotateFlipType.Rotate180FlipNone);


			//The RotateFlip transformation converts bitmaps to memoryBmp,
			//which is uncool.  Convert back now
			return ConvertBitmap(newBmp, inputBmp.RawFormat);
		}

		public static Bitmap RotateBitmapRight270(Bitmap inputBmp) {
			//Copy bitmap
			Bitmap newBmp = (Bitmap)inputBmp.Clone();

			newBmp.RotateFlip(RotateFlipType.Rotate270FlipNone);


			//The RotateFlip transformation converts bitmaps to memoryBmp,
			//which is uncool.  Convert back now
			return ConvertBitmap(newBmp, inputBmp.RawFormat);
		}

		/// <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>
		/// Reverses a bitmap, effectively rotating it 180 degrees in 3D space about
		/// the X axis.  Results in an upside-down view of the image
		/// </summary>
		/// <param name="inputBmp"></param>
		/// <returns></returns>
		public static Bitmap FlipBitmap(Bitmap inputBmp) {
			//Copy the bitmap to a new bitmap object
			Bitmap newBmp = (Bitmap)inputBmp.Clone();

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


			//The RotateFlip transformation converts bitmaps to memoryBmp,
			//which is uncool.  Convert back now
			return ConvertBitmap(newBmp, inputBmp.RawFormat);
		}

		/// <summary>
		/// Renders a bitmap over another bitmap, with a specific alpha value.
		/// This can be used to overlay a logo or watermark over a bitmap
		/// </summary>
		/// <param name="destBmp">Bitmap over which image is to be overlaid</param>
		/// <param name="bmpToOverlay">Bitmap to overlay</param>
		/// <param name="overlayAlpha">Alpha value fo overlay bitmap.  0 = fully transparent, 100 = fully opaque</param>
		/// <param name="overlayPoint">Location in destination bitmap where overlay image will be placed</param>
		/// <returns></returns>
		public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, int overlayAlpha, Point overlayPoint) {
			//Convert alpha to a 0..1 scale
			float overlayAlphaFloat = (float)overlayAlpha / 100.0f;

			//Copy the destination bitmap
			//NOTE: Can't clone here, because if destBmp is indexed instead of just RGB, 
			//Graphics.FromImage will fail
			Bitmap newBmp = new Bitmap(destBmp.Size.Width,
									   destBmp.Size.Height);

			//Create a graphics object attached to the bitmap
			Graphics newBmpGraphics = Graphics.FromImage(newBmp);

			//Draw the input bitmap into this new graphics object
			newBmpGraphics.DrawImage(destBmp,
									 new Rectangle(0, 0, 
												   destBmp.Size.Width,
												   destBmp.Size.Height),
									 0, 0, destBmp.Size.Width, destBmp.Size.Height, 
									 GraphicsUnit.Pixel);

			//Create a new bitmap object the same size as the overlay bitmap
			Bitmap overlayBmp = new Bitmap(bmpToOverlay.Size.Width, bmpToOverlay.Size.Height);

			//Make overlayBmp transparent
			overlayBmp.MakeTransparent(overlayBmp.GetPixel(0,0));

			//Create a graphics object attached to the bitmap
			Graphics overlayBmpGraphics = Graphics.FromImage(overlayBmp);

			//Create a color matrix which will be applied to the overlay bitmap
			//to modify the alpha of the entire image
			float[][] colorMatrixItems = {
				new float[] {1, 0, 0, 0, 0},
					new float[] {0, 1, 0, 0, 0},
					new float[] {0, 0, 1, 0, 0},
					new float[] {0, 0, 0, overlayAlphaFloat, 0}, 
					new float[] {0, 0, 0, 0, 1}
			};

			ColorMatrix colorMatrix = new ColorMatrix(colorMatrixItems);

			//Create an ImageAttributes class to contain a color matrix attribute
			ImageAttributes imageAttrs = new ImageAttributes();
			imageAttrs.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

			//Draw the overlay bitmap into the graphics object, applying the image attributes
			//which includes the reduced alpha
			Rectangle drawRect = new Rectangle(0, 0, bmpToOverlay.Size.Width, bmpToOverlay.Size.Height);
			overlayBmpGraphics.DrawImage(bmpToOverlay,
										 drawRect,
										 0, 0, bmpToOverlay.Size.Width, bmpToOverlay.Size.Height,
										 GraphicsUnit.Pixel,
										 imageAttrs);
			overlayBmpGraphics.Dispose();

			//overlayBmp now contains bmpToOverlay w/ the alpha applied.
			//Draw it onto the target graphics object
			//Note that pixel units must be specified to ensure the framework doesn't attempt
			//to compensate for varying horizontal resolutions in images by resizing; in this case,
			//that's the opposite of what we want.
			newBmpGraphics.DrawImage(overlayBmp, 
									 new Rectangle(overlayPoint.X, overlayPoint.Y, bmpToOverlay.Width, bmpToOverlay.Height),
									 drawRect,
									 GraphicsUnit.Pixel);

			newBmpGraphics.Dispose();

			//Recall that newBmp was created as a memory bitmap; convert it to the format
			//of the input bitmap
			return ConvertBitmap(newBmp, destBmp.RawFormat);;
		}

		/// <summary>
		/// Renders a bitmap over another bitmap, with a specific alpha value.
		/// This can be used to overlay a logo or watermark over a bitmap
		/// </summary>
		/// <param name="destBmp">Bitmap over which image is to be overlaid</param>
		/// <param name="bmpToOverlay">Bitmap to overlay</param>
		/// <param name="overlayAlpha">Alpha value fo overlay bitmap.  0 = fully transparent, 100 = fully opaque</param>
		/// <param name="corner">Corner of destination bitmap to place overlay bitmap</param>
		/// <returns></returns>
		public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, int overlayAlpha, ImageCornerEnum corner) {
			//Translate corner to rectangle and pass through to other impl
			Point overlayPoint;

			if (corner.Equals(ImageCornerEnum.TopLeft)) {
				overlayPoint = new Point(0, 0);
			} else if (corner.Equals(ImageCornerEnum.TopRight)) {
				overlayPoint = new Point(destBmp.Size.Width -  bmpToOverlay.Size.Width, 0);
			} else if (corner.Equals(ImageCornerEnum.BottomRight)) {
				overlayPoint = new Point(destBmp.Size.Width - bmpToOverlay.Size.Width,
										 destBmp.Size.Height - bmpToOverlay.Size.Height);
			} else if (corner.Equals(ImageCornerEnum.Center)) {
				overlayPoint = new Point(destBmp.Size.Width / 2 - bmpToOverlay.Size.Width / 2,
										 destBmp.Size.Height / 2 - bmpToOverlay.Size.Height / 2);
			} else {
				overlayPoint = new Point(0,
										 destBmp.Size.Height - bmpToOverlay.Size.Height);
			}

			return OverlayBitmap(destBmp, bmpToOverlay, overlayAlpha, overlayPoint);
		}

		public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, Point overlayPoint) {
			return OverlayBitmap(destBmp, bmpToOverlay, 0, overlayPoint);
		}
		public static Bitmap OverlayBitmap(Bitmap destBmp, Bitmap bmpToOverlay, ImageCornerEnum corner) {
			return OverlayBitmap(destBmp, bmpToOverlay, 0, corner);
		}

		/// <summary>
		/// Crops an image, resulting in a new image consisting of the portion of the
		/// original image contained in a provided bounding rectangle
		/// </summary>
		/// <param name="inputBmp">Bitmap to crop</param>
		/// <param name="cropRectangle">Rectangle specifying the range of pixels
		/// within the image which is to be retained</param>
		/// <returns>New bitmap consisting of the contents of the crop rectangle</returns>
		public static Bitmap CropBitmap(Bitmap inputBmp, Rectangle cropRectangle) {
			//Create a new bitmap object based on the input
			Bitmap newBmp = new Bitmap(cropRectangle.Width, 
									   cropRectangle.Height, 
									   PixelFormat.Format24bppRgb);//Graphics.FromImage doesn't like Indexed pixel format

			//Create a graphics object and attach it to the bitmap
			Graphics newBmpGraphics = Graphics.FromImage(newBmp);

			//Draw the portion of the input image in the crop rectangle
			//in the graphics object
			newBmpGraphics.DrawImage(inputBmp,
									 new Rectangle(0, 0, cropRectangle.Width, cropRectangle.Height),
									 cropRectangle,
									 GraphicsUnit.Pixel);

			//Return the bitmap
			newBmpGraphics.Dispose();

			//newBmp will have a RawFormat of MemoryBmp because it was created
			//from scratch instead of being based on inputBmp.  Since it it inconvenient
			//for the returned version of a bitmap to be of a different format, now convert
			//the scaled bitmap to the format of the source bitmap
			return ConvertBitmap(newBmp, inputBmp.RawFormat);
		}

		public static String MimeTypeFromImageFormat(ImageFormat format) {
			if (format.Equals(ImageFormat.Jpeg)) {
				return MIME_JPEG;
			} else if (format.Equals(ImageFormat.Gif)) {
				return MIME_GIF;
			} else if (format.Equals(ImageFormat.Bmp)) {
				return MIME_BMP;
			} else if (format.Equals(ImageFormat.Tiff)) {
				return MIME_TIFF;
			} else if (format.Equals(ImageFormat.Png)) {
				return MIME_PNG;
			} else {
				throw new ArgumentException("Unsupported  image format '" + format + "'", "format");
			}
		}

		public static ImageFormat ImageFormatFromMimeType(String mimeType) {
			switch (mimeType) {
			case MIME_JPEG:
			case MIME_PJPEG:
				return ImageFormat.Jpeg;

			case MIME_GIF:
				return ImageFormat.Gif;

			case MIME_BMP:
				return ImageFormat.Bmp;

			case MIME_TIFF:
				return ImageFormat.Tiff;

			case MIME_PNG:
				return ImageFormat.Png;

			default:
				throw new ArgumentException("Unsupported  MIME type '" + mimeType + "'", "mimeType");
			}
		}

		private static ImageCodecInfo FindCodecForType(String mimeType) {
			ImageCodecInfo[] imgEncoders = ImageCodecInfo.GetImageEncoders();

			for (int i = 0; i < imgEncoders.GetLength(0); i++) {
				if (imgEncoders[i].MimeType == mimeType) {
					//Found it
					return imgEncoders[i];
				}
			}

			//No encoders match
			return null;
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?