Make autoscreenshot and autoalign pictures
This commit is contained in:
parent
4c58ec196a
commit
ceca9ef25a
@ -7,9 +7,11 @@
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="800" Width="1500">
|
||||
<Canvas HorizontalAlignment="Left" Height="800" Margin="0,0,0,0" VerticalAlignment="Top" Width="1500">
|
||||
<Image x:Name="image1" Stretch="None" Height="500" Canvas.Left="130" Canvas.Top="10" Width="500" local:DraggableExtender.CanDrag="True"/>
|
||||
<Image x:Name="image1" Stretch="None" Height="450" Canvas.Left="150" Canvas.Top="50" Width="450" local:DraggableExtender.CanDrag="True"/>
|
||||
<Button x:Name="btn_LoadScreenshot" Content="Load Screenshot" Canvas.Left="10" Canvas.Top="10" Width="75" Click="Btn_LoadScreenshot_Click"/>
|
||||
<Image x:Name="image2" Stretch="None" Height="500" Canvas.Left="850" Canvas.Top="140" Width="500" local:DraggableExtender.CanDrag="True"/>
|
||||
<Image x:Name="image2" Stretch="None" Height="450" Canvas.Left="150" Canvas.Top="50" Width="450" local:DraggableExtender.CanDrag="True"/>
|
||||
<Button x:Name="btn_Switch" Content="Toggle Switch" Canvas.Left="10" Canvas.Top="60" Width="75" Click="Btn_Switch_Click"/>
|
||||
<ComboBox x:Name="cb_Screens" Canvas.Left="10" Canvas.Top="110" Width="120"/>
|
||||
<Button x:Name="btn_DbgSave" Content="DBG save" Canvas.Left="10" Canvas.Top="155" Width="75" Click="Btn_DbgSave_Click"/>
|
||||
</Canvas>
|
||||
</Window>
|
||||
|
@ -9,6 +9,7 @@ using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Interop;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
|
||||
namespace STDHelper
|
||||
{
|
||||
@ -18,32 +19,50 @@ namespace STDHelper
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private DispatcherTimer t;
|
||||
private BitmapSource lastImage;
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
System.Windows.Controls.ComboBox cb = FindName("cb_Screens") as System.Windows.Controls.ComboBox;
|
||||
Screen[] screens = Screen.AllScreens;
|
||||
foreach(Screen s in screens)
|
||||
{
|
||||
cb.Items.Add(s.DeviceName);
|
||||
}
|
||||
cb.SelectedItem = cb.Items.GetItemAt(0);
|
||||
|
||||
t = new DispatcherTimer();
|
||||
// Hook up the Elapsed event for the timer.
|
||||
t.Tick += new EventHandler(OnTimedEvent);
|
||||
t.Interval = new TimeSpan(0, 0, 1);
|
||||
}
|
||||
|
||||
private void Btn_Load1_Click(object sender, RoutedEventArgs e)
|
||||
private WriteableBitmap getAreaFromBitmap(BitmapSource src, Int32Rect area)
|
||||
{
|
||||
System.Windows.Controls.Image i = FindName("image1") as System.Windows.Controls.Image;
|
||||
LoadClipboardToImage(i);
|
||||
}
|
||||
// Calculate stride of source
|
||||
int stride = src.PixelWidth * (src.Format.BitsPerPixel + 7) / 8;
|
||||
|
||||
private void LoadClipboardToImage(System.Windows.Controls.Image img)
|
||||
{
|
||||
BitmapSource bms = CopyScreen();
|
||||
img.Opacity = 0.5;
|
||||
img.Source = bms;
|
||||
}
|
||||
// Create data array to hold source pixel data
|
||||
byte[] data = new byte[stride * area.Height];
|
||||
|
||||
private void Btn_Load2_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
System.Windows.Controls.Image i = FindName("image2") as System.Windows.Controls.Image;
|
||||
LoadClipboardToImage(i);
|
||||
// Copy source image pixels to the data array
|
||||
src.CopyPixels(area, data, stride, 0);
|
||||
|
||||
// Create WriteableBitmap to copy the pixel data to.
|
||||
WriteableBitmap target = new WriteableBitmap(
|
||||
area.Width,
|
||||
area.Height,
|
||||
src.DpiX, src.DpiY,
|
||||
src.Format, null);
|
||||
|
||||
// Write the pixel data to the WriteableBitmap.
|
||||
target.WritePixels(
|
||||
new Int32Rect(0, 0, area.Width, area.Height),
|
||||
data, stride, 0);
|
||||
|
||||
// Set the WriteableBitmap as the source for the <Image> element
|
||||
// in XAML so you can see the result of the copy
|
||||
return target;
|
||||
}
|
||||
|
||||
private void Btn_Switch_Click(object sender, RoutedEventArgs e)
|
||||
@ -86,16 +105,26 @@ namespace STDHelper
|
||||
|
||||
private void Btn_LoadScreenshot_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
System.Windows.Controls.Image i = FindName("image1") as System.Windows.Controls.Image;
|
||||
LoadClipboardToImage(i);
|
||||
System.Windows.Controls.Image i1 = FindName("image1") as System.Windows.Controls.Image;
|
||||
System.Windows.Controls.Image i2 = FindName("image2") as System.Windows.Controls.Image;
|
||||
System.Windows.Controls.ComboBox cb = FindName("cb_Screens") as System.Windows.Controls.ComboBox;
|
||||
string scrStr = cb.SelectedItem as string;
|
||||
Screen screen = Screen.AllScreens.Where(elem => elem.DeviceName == scrStr).SingleOrDefault();
|
||||
BitmapSource bms = CopyScreen(screen);
|
||||
WriteableBitmap crop1 = getAreaFromBitmap(bms, new Int32Rect(226, 123, 450, 450));
|
||||
WriteableBitmap crop2 = getAreaFromBitmap(bms, new Int32Rect(685, 123, 450, 450));
|
||||
i1.Source = crop1;
|
||||
i1.Opacity = 0.5;
|
||||
i2.Source = crop2;
|
||||
i2.Opacity = 0.5;
|
||||
}
|
||||
|
||||
private static BitmapSource CopyScreen()
|
||||
private static BitmapSource CopyScreen(Screen screen)
|
||||
{
|
||||
var left = Screen.AllScreens.Min(screen => screen.Bounds.X);
|
||||
var top = Screen.AllScreens.Min(screen => screen.Bounds.Y);
|
||||
var right = Screen.AllScreens.Max(screen => screen.Bounds.X + screen.Bounds.Width);
|
||||
var bottom = Screen.AllScreens.Max(screen => screen.Bounds.Y + screen.Bounds.Height);
|
||||
var left = screen.Bounds.X;
|
||||
var top = screen.Bounds.Y;
|
||||
var right = screen.Bounds.X + screen.Bounds.Width;
|
||||
var bottom = screen.Bounds.Y + screen.Bounds.Height;
|
||||
var width = right - left;
|
||||
var height = bottom - top;
|
||||
|
||||
@ -112,6 +141,18 @@ namespace STDHelper
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Btn_DbgSave_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Debug.WriteLine("SAVE CLICK");
|
||||
using (var fileStream = new FileStream("debug.png", FileMode.Create))
|
||||
{
|
||||
Debug.WriteLine("START SAVE");
|
||||
BitmapEncoder encoder = new PngBitmapEncoder();
|
||||
encoder.Frames.Add(BitmapFrame.Create(this.lastImage));
|
||||
encoder.Save(fileStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DraggableExtender : DependencyObject
|
||||
|
Loading…
x
Reference in New Issue
Block a user