幅と高さを指定してスクリーンショットを取得する事が出来ます。
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
| using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows; using System.Windows.Media.Imaging;
namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }
private void Button_Click(object sender, RoutedEventArgs e) { // 矩形領域 var rectangle = new Rectangle(0, 0, (int)sliderH.Value, (int)sliderV.Value); var bitmap = new Bitmap(rectangle.Width, rectangle.Height); var graphics = Graphics.FromImage(bitmap); graphics.CopyFromScreen(new System.Drawing.Point(rectangle.X, rectangle.Y), new System.Drawing.Point(0, 0), bitmap.Size); // グラフィックスの解放 graphics.Dispose();
// 画像の表示 using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); image.Source = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); } } } }
|

ソースコード
参考
https://www.fenet.jp/dotnet/column/language/4633/
https://water2litter.net/rye/post/c_graphic_bitmap_image/