diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-01 16:40:13 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-01 16:40:13 +0200 |
| commit | eb2c264422b98458979bc96504ce8830a527d48c (patch) | |
| tree | 2c2e0bcb908867759e7bea31dad0e21bd82f9cff /Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs | |
| parent | a89c18abf7175f76f8673c12dac35d1658209d4e (diff) | |
| download | Tango-eb2c264422b98458979bc96504ce8830a527d48c.tar.gz Tango-eb2c264422b98458979bc96504ce8830a527d48c.zip | |
Added Tango.Video project.
Implemented USB video device capture for developer module.
Diffstat (limited to 'Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs b/Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs new file mode 100644 index 000000000..980e479c6 --- /dev/null +++ b/Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media.Imaging; + + + +public static class WriteableBitmapExtensions +{ + /// <summary> + /// Convert the WriteableBitmap to GDI+ Bitmap. + /// </summary> + /// <param name="bitmapsource">The writeable bitmap.</param> + /// <returns>returns a System.Drawing.Bitmap Bitmap.</returns> + public static System.Drawing.Bitmap ConvertToGDIBitmap(this WriteableBitmap bitmapsource) + { + 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. + var result = new System.Drawing.Bitmap(tempBitmap); + stream.Dispose(); + return result; + } + } + } + + + /// <summary> + /// Convert the WriteableBitmap to BitmapImage. + /// </summary> + /// <param name="bitmapSource">The writeable bitmap.</param> + /// <returns>returns a new instance of BitmapImage</returns> + public static BitmapImage ConvertToBitmapImage(this WriteableBitmap 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; + } +}
\ No newline at end of file |
