using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows.Media.Imaging; public static class BitmapSourceExtensions { /// /// Converts the bitmap source to GDI+ bitmap. /// /// The bitmap source. /// public static System.Drawing.Bitmap ToGDIBitmap(this BitmapSource bitmapsource) { if (bitmapsource != null) { using (MemoryStream stream = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapsource)); enc.Save(stream); using (var tempBitmap = new System.Drawing.Bitmap(stream)) { // According to MSDN, one "must keep the stream open for the lifetime of the Bitmap." // So we return a copy of the new bitmap, allowing us to dispose both the bitmap and the stream. return new System.Drawing.Bitmap(tempBitmap); } } } else { return new System.Drawing.Bitmap(1, 1); } } /// /// Save to the BMP file. /// /// The bitmap source. /// Name of the file. public static void SaveBmpFile(this BitmapSource bitmapSource, String fileName) { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapSource)); enc.Save(fs); } } /// /// Save to the PNG file. /// /// The bitmap source. /// Name of the file. public static void SavePngFile(this BitmapSource bitmapSource, String fileName) { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { PngBitmapEncoder enc = new PngBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapSource)); enc.Save(fs); } } /// /// Save to the JPEG file. /// /// The bitmap source. /// Name of the file. /// The quality. public static void SaveJpegFile(this BitmapSource bitmapSource, String fileName, int quality = 100) { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { JpegBitmapEncoder enc = new JpegBitmapEncoder(); enc.QualityLevel = quality; enc.Frames.Add(BitmapFrame.Create(bitmapSource)); enc.Save(fs); } } /// /// Converts the bitmap source to bitmap image. /// /// The bitmap source. /// public static BitmapImage ConvertToBitmapImage(this BitmapSource bitmapSource) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); MemoryStream memoryStream = new MemoryStream(); BitmapImage bImg = new BitmapImage(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(memoryStream); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(memoryStream.ToArray()); bImg.EndInit(); memoryStream.Close(); return bImg; } /// /// Converts the Bitmap Source to Writeable Bitmap. /// /// The bitmap source. /// public static WriteableBitmap ToWritableBitmap(this BitmapSource bitmapsource) { WriteableBitmap w = new WriteableBitmap(bitmapsource); return w; } /// /// Converts the BitmapSource to byte array. /// /// The bitmap source. /// The pixel format. /// public static byte[] ToBytes(this BitmapSource bitmapsource, PixelFormat format) { var bm = new FormatConvertedBitmap(bitmapsource, format, null, 0); BitmapEncoder encoder = new BmpBitmapEncoder(); byte[] bytes = null; using (MemoryStream stream = new MemoryStream()) { encoder.Frames.Add(BitmapFrame.Create(bm)); encoder.Save(stream); bytes = stream.ToArray(); stream.Close(); } return bytes; } /// /// Converts the Bitmap Source to JPEG byte array. /// /// The bitmap source. /// Compression quality. /// public static byte[] ToJpegBytes(this BitmapSource bitmapsource, int quality = 100) { var bm = new FormatConvertedBitmap(bitmapsource, PixelFormats.Bgr24, null, 0); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = quality; byte[] bytes = null; using (MemoryStream stream = new MemoryStream()) { encoder.Frames.Add(BitmapFrame.Create(bm)); encoder.Save(stream); bytes = stream.ToArray(); stream.Close(); } return bytes; } /// /// Converts the BitmapSource to PNG byte array. /// /// The bitmap source. /// public static byte[] ToPngBytes(this BitmapSource bitmapsource) { PngBitmapEncoder encoder = new PngBitmapEncoder(); byte[] bytes = null; using (MemoryStream stream = new MemoryStream()) { encoder.Frames.Add(BitmapFrame.Create(bitmapsource)); encoder.Save(stream); bytes = stream.ToArray(); stream.Close(); } return bytes; } /// /// Converts the BitmapSource to BMP byte array. /// /// The bitmap source. /// public static byte[] ToBmpBytes(this BitmapSource bitmapsource) { BmpBitmapEncoder encoder = new BmpBitmapEncoder(); byte[] bytes = null; using (MemoryStream stream = new MemoryStream()) { encoder.Frames.Add(BitmapFrame.Create(bitmapsource)); encoder.Save(stream); bytes = stream.ToArray(); stream.Close(); } return bytes; } /// /// Converts the byte array to a Bitmap Source. /// /// The bytes. /// public static BitmapSource ToBitmapSource(this byte[] bytes) { var image = new BitmapImage(); using (var mem = new MemoryStream(bytes)) { mem.Position = 0; image.BeginInit(); image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; image.CacheOption = BitmapCacheOption.OnLoad; image.UriSource = null; image.StreamSource = mem; image.EndInit(); } image.Freeze(); return image; } /// /// Converts the bitmap source format. /// /// The bitmap source. /// The format. /// public static BitmapSource ConvertFormat(this BitmapSource bitmapsource, PixelFormat format) { var bm = new FormatConvertedBitmap(bitmapsource, format, null, 0); return bm; } /// /// Converts this GDI bitmap to WPF BitmapSource. /// /// The bitmap. /// public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap bitmap) { var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var bitmapSource = BitmapSource.Create( bitmapData.Width, bitmapData.Height, 96, 96, System.Windows.Media.PixelFormats.Bgra32, null, bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride); bitmap.UnlockBits(bitmapData); bitmapSource.Freeze(); return bitmapSource; } }