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

WPF: Save the background of a Windows app as a ScreenShot

 

The following C # code saves the current background or the appearance of a Windows 32 WPF app as a bitmap

This example also shows that only the app application is captured and not the background desktop behind it.

 

C # Code to render:

Step 1: Create a renderer

Step 2: Output as bitmap

Step 3: Save the bitmap as a file

 

 

When using a transparent background, it is only rendered to the app.

So you can neither in UWP nor WPF the background of the desktop thereby grasp

 

 

C # Code to render:

Step 1: Create a renderer

Step 2: Output as bitmap

Step 3: Save the bitmap as a file

//*render a xaml Element to a Bitmap File

//< render >

RenderTargetBitmap bitmap_Renderer = new RenderTargetBitmap((int) element.RenderSize.Width, (int)element.RenderSize.Height,96, 96, PixelFormats.Pbgra32);

bitmap_Renderer.Render(element);

//</ render >

 

//< get Bitmap >

BitmapFrame outputFrame = BitmapFrame.Create(bitmap_Renderer);

//</ get Bitmap >

 

//< save as File >

PngBitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(outputFrame);

 

using (var file = File.OpenWrite("C:\\_Daten\\Desktop\\Neuer Ordner\\TestImage.png"))

{

    encoder.Save(file);

}

//</ save as File >

 

 

 

 

Completter C # Code

private void btnScreenShot_Click(object sender, RoutedEventArgs e)

{

render_UIElement_to_Bitmap_File(this);

//RenderVisual(this);

}

 

 

 

public void render_UIElement_to_Bitmap_File(UIElement element)

{

//-----------------< render_UIElement_to_Bitmap_File() >-----------------

//*render a xaml Element to a Bitmap File

//< render >

RenderTargetBitmap bitmap_Renderer = new RenderTargetBitmap((int) element.RenderSize.Width, (int)element.RenderSize.Height,96, 96, PixelFormats.Pbgra32);

bitmap_Renderer.Render(element);

//</ render >

 

//< get Bitmap >

BitmapFrame outputFrame = BitmapFrame.Create(bitmap_Renderer);

//</ get Bitmap >

 

//< save as File >

PngBitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(outputFrame);

 

using (var file = File.OpenWrite("C:\\_Daten\\Desktop\\Neuer Ordner\\TestImage.png"))

{

    encoder.Save(file);

}

//</ save as File >

//-----------------</ render_UIElement_to_Bitmap_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