Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

C #, WPF, UWP: ScreenShot to save an app or window area as a file

 

 

C #, WPF, UWP, RenderTarget Bitmap, Encoder, SaveSave as File, Desktop, Screen Area

Render Windows window, app, application or XAML element as bitmap and save the element as a ScreenShot file

 

//< get Screenshot of Element >

RenderTargetBitmap bmp = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight , 96, 96, PixelFormats.Pbgra32);

bmp.Render(element);

//</ get Screenshot of Element >

 

//< create Encoder >

PngBitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(bmp));

//</ create Encoder >

 

//< save >

FileStream fs = new FileStream(filename, FileMode.Create);

encoder.Save(fs);

fs.Close();

//</ save >

 

 

 

 

 

 

 

 

Video Tutorial

 

 

 

Complete C #, WPF code of the method

        private void btnApp_ScreenShot_as_File_Click(object sender, RoutedEventArgs e)

        {

//------------< btnApp_ScreenShot_as_File_Click() >------------

save_FrameworkElement_as_Screenshot_File(this);

//------------</ btnApp_ScreenShot_as_File_Click() >------------

        }

 

 

        private void save_FrameworkElement_as_Screenshot_File(FrameworkElement element)

        {

//------------< save_FrameworkElement_as_Screenshot_File() >------------

//< init >

String filename = "C:\\_Daten\\Desktop\\Backgrounds\\App_ScreenShots\\App_ScreenCapture-" + DateTime.Now.ToString("ddMMyyyy-hhmmss") + ".png";

//</ init >

 

//< get Screenshot of Element >

RenderTargetBitmap bmp = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight , 96, 96, PixelFormats.Pbgra32);

bmp.Render(element);

//</ get Screenshot of Element >

 

//< create Encoder >

PngBitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(bmp));

//</ create Encoder >

 

//< save >

FileStream fs = new FileStream(filename, FileMode.Create);

encoder.Save(fs);

fs.Close();

//</ save >

//------------</ save_FrameworkElement_as_Screenshot_File() >------------

        }

 

Mobile
»
WPF / UWP: Canvas with relative positions and sizes
»
C #, WPF, UWP: ScreenShot to save an app or window area as a file
»
C #: Windows ScreenShot save as file
»
WPF Color Picker on Color Cube
»
WPF: Draw ColorWheel in C# by drawing Lines
»
WPF: Snap In to Next Lines of Elements
»
WPF Code: Usercontrol with customizable size at runtime
»
WPF: Save the background of a Windows app as a ScreenShot
»
WPF, UWP: Create transparent app window
»
WPF: Determine the relative position of an element

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de