aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/ExtensionMethods
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-03-04 15:28:15 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-03-04 15:28:15 +0200
commit2b8e41b5279c2d3ab370595f6593b64ea734ef87 (patch)
tree084ceaae9e1b65454e9e2264ce6fdb0511ca4cf9 /Software/Visual_Studio/Tango.Core/ExtensionMethods
parentd734bb5cf08ba2433b74fc86a8858d2437d1a237 (diff)
downloadTango-2b8e41b5279c2d3ab370595f6593b64ea734ef87.tar.gz
Tango-2b8e41b5279c2d3ab370595f6593b64ea734ef87.zip
Implemented job embroidery image capture, display export,
Implemented running job text to speech.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods')
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/BitmapSourceExtensions.cs242
1 files changed, 242 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/BitmapSourceExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/BitmapSourceExtensions.cs
new file mode 100644
index 000000000..11844df36
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/BitmapSourceExtensions.cs
@@ -0,0 +1,242 @@
+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
+{
+ /// <summary>
+ /// Converts the bitmap source to GDI+ bitmap.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <returns></returns>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Save to the BMP file.
+ /// </summary>
+ /// <param name="bitmapSource">The bitmap source.</param>
+ /// <param name="fileName">Name of the file.</param>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Save to the PNG file.
+ /// </summary>
+ /// <param name="bitmapSource">The bitmap source.</param>
+ /// <param name="fileName">Name of the file.</param>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Save to the JPEG file.
+ /// </summary>
+ /// <param name="bitmapSource">The bitmap source.</param>
+ /// <param name="fileName">Name of the file.</param>
+ /// <param name="quality">The quality.</param>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Converts the bitmap source to bitmap image.
+ /// </summary>
+ /// <param name="bitmapSource">The bitmap source.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Converts the Bitmap Source to Writeable Bitmap.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <returns></returns>
+ public static WriteableBitmap ToWritableBitmap(this BitmapSource bitmapsource)
+ {
+ WriteableBitmap w = new WriteableBitmap(bitmapsource);
+ return w;
+ }
+
+ /// <summary>
+ /// Converts the BitmapSource to byte array.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <param name="format">The pixel format.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Converts the Bitmap Source to JPEG byte array.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <param name="quality">Compression quality.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Converts the BitmapSource to PNG byte array.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Converts the BitmapSource to BMP byte array.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Converts the byte array to a Bitmap Source.
+ /// </summary>
+ /// <param name="bytes">The bytes.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Converts the bitmap source format.
+ /// </summary>
+ /// <param name="bitmapsource">The bitmap source.</param>
+ /// <param name="format">The format.</param>
+ /// <returns></returns>
+ public static BitmapSource ConvertFormat(this BitmapSource bitmapsource, PixelFormat format)
+ {
+ var bm = new FormatConvertedBitmap(bitmapsource, format, null, 0);
+ return bm;
+ }
+}