aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Video/WriteableBitmapExtensions.cs
blob: 980e479c69182bdeec9201b58de3637051134e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .
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;
    }
}