forked from Agilitas/screengrab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenCapture.cs
More file actions
132 lines (106 loc) · 4.25 KB
/
ScreenCapture.cs
File metadata and controls
132 lines (106 loc) · 4.25 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ScreenGrab {
public class ScreenCapture {
public ImageSource Source { get; private set; }
public string DateCaptured { get; private set; }
public Screen SourceScreen { get; set; }
public Int32Rect SourcePosition { get; set; }
private ObservableCollection<AnnotationHighlight> _highlights;
public ObservableCollection<AnnotationHighlight> Highlights {
get {
if (_highlights == null) {
_highlights = new ObservableCollection<AnnotationHighlight>();
}
return _highlights;
}
set { _highlights = value; }
}
public ScreenCapture(ImageSource source) {
Source = source;
DateCaptured = DateTime.Now.ToString(CultureInfo.InvariantCulture);
Highlights = new ObservableCollection<AnnotationHighlight>();
}
public Stream ImageAsPng() {
PngBitmapEncoder encoder = new PngBitmapEncoder();
MemoryStream stream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(Source as BitmapSource));
encoder.Save(stream);
return stream;
}
//public Stream ImageAsJpg2() {
// JpegBitmapEncoder encoder = new JpegBitmapEncoder();
// MemoryStream stream = new MemoryStream();
// encoder.Frames.Add(BitmapFrame.Create(Source as BitmapSource));
// encoder.Save(stream);
// return stream;
//}
public Stream ImageAsJpg() {
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(Source, new Rect(0, 0, Source.Width, Source.Height));
//Brush rb
drawingContext.PushOpacity(0.5);
foreach (AnnotationHighlight highlight in Highlights) {
if (highlight != null) // overlapping highlights
{
var rectangle = highlight.Rectangle;
drawingContext.DrawRectangle(highlight.Color, null, new Rect(highlight.TopLeft.X, highlight.TopLeft.Y, rectangle.Width, rectangle.Height));
}
}
drawingContext.Close();
RenderTargetBitmap rtb = new RenderTargetBitmap((int)Source.Width, (int)Source.Height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(drawingVisual);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 75;
//PngBitmapEncoder encoder = new PngBitmapEncoder();
MemoryStream stream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
stream.Position = 0;
return stream;
}
private const double ThMax = 110;
Rect ImageDimensions() {
return new Rect(0, 0, Source.Width, Source.Height);
}
BitmapImage GetThumbnail() {
var d = ImageDimensions();
double tw;
double th;
double ratio = (d.Width / d.Height);
if (d.Width >= d.Height) {
//landscape
tw = ThMax;
th = ThMax / ratio;
} else {
//portrait
th = ThMax;
tw = ThMax * ratio;
}
Image.GetThumbnailImageAbort myCallback = () => false;
Bitmap myBitmap = new Bitmap(ImageAsPng());
Image myThumbnail = myBitmap.GetThumbnailImage((int)tw, (int)th, myCallback, IntPtr.Zero);
MemoryStream ms = new MemoryStream();
myThumbnail.Save(ms, ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
return bi;
}
private BitmapImage _thumbnail;
public BitmapImage Thumbnail {
get { return _thumbnail ?? (_thumbnail = GetThumbnail()); }
}
}
}