blob: 104bbccb120090b493e998addb3c05eb5c7c9d60 (
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace Tango.ScreenCapture
{
public class ScreenCaptureFrame : IDisposable
{
private Bitmap _bitmap;
private Bitmap _diffBitmap;
public int Width
{
get { return _bitmap.Width; }
}
public int Height
{
get { return _bitmap.Height; }
}
public bool HasDifferenceFrame
{
get { return _diffBitmap != null; }
}
public ScreenCaptureFrame(Bitmap bitmap, Bitmap diffBitmap)
{
_diffBitmap = diffBitmap;
_bitmap = bitmap;
}
public void Dispose()
{
_bitmap.Dispose();
if (_diffBitmap != null)
{
_diffBitmap.Dispose();
}
}
public BitmapSource ToBitmapSource()
{
var source = GetBitmapImage(ToStream());
source.Freeze();
return source;
}
public Bitmap ToBitmap()
{
return _bitmap;
}
public ScreenCaptureFrame ToDifferenceCaptureFrame()
{
return new ScreenCaptureFrame(_diffBitmap, null);
}
public byte[] ToArray()
{
using (var ms = ToStream())
{
return ms.ToArray();
}
}
public byte[] ToJpeg(int quality = 100)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
using (MemoryStream ms = new MemoryStream())
{
_bitmap.Save(ms, GetEncoder(ImageFormat.Jpeg), encoderParameters);
ms.Position = 0;
return ms.ToArray();
}
}
public byte[] ToPng()
{
using (MemoryStream ms = new MemoryStream())
{
_bitmap.Save(ms, ImageFormat.Png);
ms.Position = 0;
return ms.ToArray();
}
}
public MemoryStream ToStream()
{
MemoryStream ms = new MemoryStream();
_bitmap.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
return ms;
}
private BitmapImage GetBitmapImage(MemoryStream ms)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
return bitmapImage;
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
return codecs.Single(codec => codec.FormatID == format.Guid);
}
}
}
|