11using System ;
2- using System . Collections . Generic ;
32using System . IO ;
4- using System . Linq ;
5- using System . Text ;
6- using System . Threading . Tasks ;
73using System . Windows ;
84using System . Windows . Media ;
95using System . Windows . Media . Imaging ;
106
11- namespace IconImage
7+ namespace IconImage ;
8+
9+ public class FrameworkElementImage
1210{
13- public class FrameworkElementImage
11+ /// <summary>
12+ /// Saves the image of a specified FrameworkElement to file in PNG format.
13+ /// </summary>
14+ /// <param name="source">FrameworkElement</param>
15+ /// <param name="filePath">File path</param>
16+ /// <param name="width">Width of saved image (optional)</param>
17+ /// <param name="height">Height of saved image (optional)</param>
18+ public static void SaveImage ( FrameworkElement source , string filePath , double width = 0D , double height = 0D )
1419 {
15- /// <summary>
16- /// Saves the image of a specified FrameworkElement to file in PNG format.
17- /// </summary>
18- /// <param name="source">FrameworkElement</param>
19- /// <param name="filePath">File path</param>
20- /// <param name="width">Width of saved image (optional)</param>
21- /// <param name="height">Height of saved image (optional)</param>
22- public static void SaveImage ( FrameworkElement source , string filePath , double width = 0D , double height = 0D )
23- {
24- if ( source is null )
25- throw new ArgumentNullException ( nameof ( source ) ) ;
26- if ( string . IsNullOrWhiteSpace ( filePath ) )
27- throw new ArgumentNullException ( nameof ( filePath ) ) ;
28-
29- var rtb = new RenderTargetBitmap (
30- ( int ) source . Width ,
31- ( int ) source . Height ,
32- 96D , 96D ,
33- PixelFormats . Pbgra32 ) ;
20+ if ( source is null )
21+ throw new ArgumentNullException ( nameof ( source ) ) ;
22+ if ( string . IsNullOrWhiteSpace ( filePath ) )
23+ throw new ArgumentNullException ( nameof ( filePath ) ) ;
3424
35- rtb . Render ( source ) ;
25+ var rtb = new RenderTargetBitmap (
26+ ( int ) source . Width ,
27+ ( int ) source . Height ,
28+ 96D , 96D ,
29+ PixelFormats . Pbgra32 ) ;
3630
37- var encoder = new PngBitmapEncoder ( ) ;
38- encoder . Frames . Add ( BitmapFrame . Create ( rtb ) ) ;
31+ rtb . Render ( source ) ;
3932
40- if ( ( 0 < width ) || ( 0 < height ) )
41- {
42- var bi = new BitmapImage ( ) ;
33+ var encoder = new PngBitmapEncoder ( ) ;
34+ encoder . Frames . Add ( BitmapFrame . Create ( rtb ) ) ;
4335
44- using ( var ms = new MemoryStream ( ) )
45- {
46- encoder . Save ( ms ) ;
47- ms . Seek ( 0 , SeekOrigin . Begin ) ;
36+ if ( ( 0 < width ) || ( 0 < height ) )
37+ {
38+ var bi = new BitmapImage ( ) ;
4839
49- bi . BeginInit ( ) ;
50- bi . CacheOption = BitmapCacheOption . OnLoad ;
51- bi . StreamSource = ms ;
40+ using ( var ms = new MemoryStream ( ) )
41+ {
42+ encoder . Save ( ms ) ;
43+ ms . Seek ( 0 , SeekOrigin . Begin ) ;
5244
53- if ( 0 < width )
54- bi . DecodePixelWidth = ( int ) width ;
55- if ( 0 < height )
56- bi . DecodePixelHeight = ( int ) height ;
45+ bi . BeginInit ( ) ;
46+ bi . CacheOption = BitmapCacheOption . OnLoad ;
47+ bi . StreamSource = ms ;
5748
58- bi . EndInit ( ) ;
59- }
49+ if ( 0 < width )
50+ bi . DecodePixelWidth = ( int ) width ;
51+ if ( 0 < height )
52+ bi . DecodePixelHeight = ( int ) height ;
6053
61- encoder = new PngBitmapEncoder ( ) ; // Save method cannot be used twice.
62- encoder . Frames . Add ( BitmapFrame . Create ( bi ) ) ;
54+ bi . EndInit ( ) ;
6355 }
6456
65- using ( var fs = new FileStream ( filePath , FileMode . Create , FileAccess . Write , FileShare . ReadWrite ) )
66- {
67- encoder . Save ( fs ) ;
68- }
57+ encoder = new PngBitmapEncoder ( ) ; // Save method cannot be used twice.
58+ encoder . Frames . Add ( BitmapFrame . Create ( bi ) ) ;
59+ }
60+
61+ using ( var fs = new FileStream ( filePath , FileMode . Create , FileAccess . Write , FileShare . ReadWrite ) )
62+ {
63+ encoder . Save ( fs ) ;
6964 }
7065 }
7166}
0 commit comments