-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoomWindow.xaml.cs
More file actions
57 lines (48 loc) · 1.88 KB
/
Copy pathZoomWindow.xaml.cs
File metadata and controls
57 lines (48 loc) · 1.88 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
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace ColorPicker
{
public partial class ZoomWindow : Window
{
private const int INITIAL_CAPTURE_SIZE = 50;
private int currentCaptureSize = INITIAL_CAPTURE_SIZE;
public ZoomWindow()
{
InitializeComponent();
}
public void UpdateZoomImage(System.Drawing.Point position, int zoomLevel)
{
currentCaptureSize = INITIAL_CAPTURE_SIZE + (zoomLevel * 10);
if (currentCaptureSize < INITIAL_CAPTURE_SIZE) currentCaptureSize = INITIAL_CAPTURE_SIZE;
using (var bitmap = new Bitmap(currentCaptureSize, currentCaptureSize))
{
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen((int)position.X - currentCaptureSize / 2, (int)position.Y - currentCaptureSize / 2, 0, 0, new System.Drawing.Size(currentCaptureSize, currentCaptureSize));
}
var bitmapSource = ConvertToBitmapSource(bitmap);
ZoomImageBrush.ImageSource = bitmapSource;
this.Left = position.X - (currentCaptureSize / 2);
this.Top = position.Y - (currentCaptureSize / 2);
}
}
public BitmapSource ConvertToBitmapSource(Bitmap bitmap)
{
var hBitmap = bitmap.GetHbitmap();
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
return bitmapSource;
}
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
}
}