Post Top Ad

How to Capture the screen selected by Current Form in WPF?

For my 3rd year project at SLIIT, we used WPF to develop the software. Once we wanated to get the selected part of the WPF current form as a image to be save in a separate locatioin. In WPF, the controls to handels the form is different than the normal winforms. So after searching hours in the internet, i came up with a solution.

Example output


WPF code

private void button5_Click(object sender, RoutedEventArgs e)

{
Image b = null;
int w = (int)this.Width;
int h = (int)this.Height;
System.Drawing.Size sz = new System.Drawing.Size((int)ActualWidth, 
       (int)ActualHeight);
System.Drawing.Point loc = new System.Drawing.Point((int)Left, (int)Top);

Hide();
System.Threading.Thread.Sleep(500);
 
using
(b = new Bitmap(w, h))
{
using
(Graphics g = Graphics.FromImage(b))
{
g
.CopyFromScreen(loc, new System.Drawing.Point(0, 0), sz);
}

Image x = new Bitmap(b);

ImageBrush myBrush = new ImageBrush();
x
.Save(@"C:\Users\Dell\Desktop\testImg.jpeg", ImageFormat.Jpeg);
myBrush
.ImageSource = new BitmapImage(new Uri(@"C:\Users\Dell\Desktop\
testImg.jpeg", 
            UriKind.Absolute));
this.Background = myBrush;

}
Show();
}
 
 

This code will extract the image taking the current form size as Width and Height and grab its content using Bitmap class. 











No comments:

Post a Comment

My Instagram