1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ #nullable enable
5
+
4
6
// Description: Helper methods for code that uses types from System.Drawing.
5
7
6
8
using System ;
15
17
namespace MS . Internal
16
18
{
17
19
//FxCop can't tell that this class is instantiated via reflection, so suppress the FxCop warning.
18
- [ SuppressMessage ( "Microsoft.Performance" , "CA1812:AvoidUninstantiatedInternalClasses" ) ]
20
+ [ SuppressMessage ( "Microsoft.Performance" , "CA1812:AvoidUninstantiatedInternalClasses" ) ]
19
21
internal class SystemDrawingExtension : SystemDrawingExtensionMethods
20
22
{
21
- // return true if the data is a bitmap
22
- internal override bool IsBitmap ( object data )
23
- {
24
- return data is Bitmap ;
25
- }
23
+ internal override bool IsBitmap ( object ? data ) => data is Bitmap ;
26
24
27
- // return true if the data is an Image
28
- internal override bool IsImage ( object data )
29
- {
30
- return data is Image ;
31
- }
25
+ internal override bool IsImage ( object ? data ) => data is Image ;
32
26
33
- // return true if the data is a graphics metafile
34
- internal override bool IsMetafile ( object data )
35
- {
36
- return data is Metafile ;
37
- }
27
+ internal override bool IsMetafile ( object ? data ) => data is Metafile ;
38
28
39
- // return the handle from a metafile
40
- internal override IntPtr GetHandleFromMetafile ( Object data )
29
+ internal override nint GetHandleFromMetafile ( object ? data ) => data switch
41
30
{
42
- IntPtr hMetafile = IntPtr . Zero ;
43
- Metafile metafile = data as Metafile ;
31
+ Metafile metafile => metafile . GetHenhmetafile ( ) ,
32
+ _ => 0
33
+ } ;
44
34
45
- if ( metafile != null )
46
- {
47
- // Get the Windows handle from the metafile object.
48
- hMetafile = metafile . GetHenhmetafile ( ) ;
49
- }
35
+ internal override object GetMetafileFromHemf ( nint hMetafile ) => new Metafile ( hMetafile , deleteEmf : false ) ;
50
36
51
- return hMetafile ;
52
- }
37
+ internal override object ? GetBitmap ( object ? data ) => GetBitmapImpl ( data ) ;
53
38
54
- // Get the metafile from the handle of the enhanced metafile.
55
- internal override Object GetMetafileFromHemf ( IntPtr hMetafile )
39
+ internal override nint GetHBitmap ( object ? data , out int width , out int height )
56
40
{
57
- return new Metafile ( hMetafile , false ) ;
58
- }
41
+ Bitmap ? bitmapData = GetBitmapImpl ( data ) ;
59
42
60
- // Get a bitmap from the given data (either BitmapSource or Bitmap)
61
- internal override object GetBitmap ( object data )
62
- {
63
- return GetBitmapImpl ( data ) ;
64
- }
65
-
66
- // Get a bitmap handle from the given data (either BitmapSource or Bitmap)
67
- // Also return its width and height.
68
- internal override IntPtr GetHBitmap ( object data , out int width , out int height )
69
- {
70
- Bitmap bitmapData = GetBitmapImpl ( data ) ;
71
-
72
- if ( bitmapData == null )
43
+ if ( bitmapData is null )
73
44
{
74
45
width = height = 0 ;
75
46
return IntPtr . Zero ;
@@ -83,54 +54,48 @@ internal override IntPtr GetHBitmap(object data, out int width, out int height)
83
54
return bitmapData . GetHbitmap ( ) ;
84
55
}
85
56
86
- // Get a bitmap handle from a Bitmap
87
- internal override IntPtr GetHBitmapFromBitmap ( object data )
88
- {
89
- Bitmap bitmap = data as Bitmap ;
90
- return ( bitmap != null ) ? bitmap . GetHbitmap ( ) : IntPtr . Zero ;
91
- }
57
+ internal override nint GetHBitmapFromBitmap ( object ? data ) => data is Bitmap bitmap ? bitmap . GetHbitmap ( ) : 0 ;
92
58
93
- // Convert a metafile to HBitmap
94
- internal override IntPtr ConvertMetafileToHBitmap ( IntPtr handle )
59
+ internal override nint ConvertMetafileToHBitmap ( nint handle )
95
60
{
96
- Metafile metafile = new Metafile ( handle , false ) ;
61
+ Metafile metafile = new ( handle , deleteEmf : false ) ;
97
62
98
63
// Initialize the bitmap size to render the metafile.
99
64
int bitmapheight = metafile . Size . Height ;
100
- int bitmapwidth = metafile . Size . Width ;
65
+ int bitmapwidth = metafile . Size . Width ;
101
66
102
67
// We use System.Drawing to render metafile into the bitmap.
103
68
Bitmap bmp = new Bitmap ( bitmapwidth , bitmapheight ) ;
104
69
Graphics graphics = Graphics . FromImage ( bmp ) ;
105
- // graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), 0, 0, bitmapwidth, bitmapheight);
106
70
graphics . DrawImage ( metafile , 0 , 0 , bitmapwidth , bitmapheight ) ;
107
71
108
72
return bmp . GetHbitmap ( ) ;
109
73
}
110
74
111
- // return a stream for the ExifUserComment in the given Gif
112
75
internal override Stream GetCommentFromGifStream ( Stream stream )
113
76
{
114
- // Read the GIF header ...
115
- Bitmap img = new Bitmap ( stream ) ;
116
- // Read the comment as that is where the ISF is stored...
117
- // for reference the tag is PropertyTagExifUserComment [0x9286] or 37510 (int)
118
- PropertyItem piComment = img . GetPropertyItem ( 37510 ) ;
119
- return new MemoryStream ( piComment . Value ) ;
77
+ // Read the GIF header
78
+ Bitmap bitmap = new ( stream ) ;
79
+
80
+ // Read the comment as that is where the ISF is stored.
81
+ // For reference the tag is PropertyTagExifUserComment [0x9286] or 37510 (int)
82
+ PropertyItem ? piComment = bitmap . GetPropertyItem ( 37510 ) ;
83
+
84
+ // Want to make this throw in the same way it used to for now. It would be good
85
+ // to redo this to return null and handle the null case in the various callers.
86
+ return new MemoryStream ( piComment ! . Value ! ) ;
120
87
}
121
88
122
- // write a metafile stream to the output stream in PNG format
123
89
internal override void SaveMetafileToImageStream ( MemoryStream metafileStream , Stream imageStream )
124
90
{
125
- Metafile metafile = new Metafile ( metafileStream ) ;
91
+ Metafile metafile = new ( metafileStream ) ;
126
92
metafile . Save ( imageStream , ImageFormat . Png ) ;
127
93
}
128
94
129
95
// Get a bitmap from the given data (either BitmapSource or Bitmap)
130
- private static Bitmap GetBitmapImpl ( object data )
96
+ private static Bitmap ? GetBitmapImpl ( object ? data )
131
97
{
132
- BitmapSource bitmapSource = data as BitmapSource ;
133
- if ( bitmapSource != null )
98
+ if ( data is BitmapSource bitmapSource )
134
99
{
135
100
// Convert BitmapSource to System.Drawing.Bitmap to get Win32 HBITMAP.
136
101
BitmapEncoder bitmapEncoder ;
@@ -151,23 +116,18 @@ private static Bitmap GetBitmapImpl(object data)
151
116
}
152
117
}
153
118
154
- //returns bitmap snapshot of selected area
155
- //this code takes a BitmapImage and converts it to a Bitmap so it can be put on the clipboard
156
119
internal override object GetBitmapFromBitmapSource ( object source )
157
120
{
158
121
BitmapSource contentImage = ( BitmapSource ) source ;
159
122
int imageWidth = ( int ) contentImage . Width ;
160
123
int imageHeight = ( int ) contentImage . Height ;
161
124
162
- Bitmap bitmapFinal = new Bitmap (
163
- imageWidth ,
164
- imageHeight ,
165
- System . Drawing . Imaging . PixelFormat . Format32bppRgb ) ;
125
+ Bitmap bitmapFinal = new ( imageWidth , imageHeight , PixelFormat . Format32bppRgb ) ;
166
126
167
127
BitmapData bmData = bitmapFinal . LockBits (
168
- new Rectangle ( 0 , 0 , imageWidth , imageHeight ) ,
169
- ImageLockMode . WriteOnly ,
170
- System . Drawing . Imaging . PixelFormat . Format32bppRgb ) ;
128
+ new Rectangle ( 0 , 0 , imageWidth , imageHeight ) ,
129
+ ImageLockMode . WriteOnly ,
130
+ PixelFormat . Format32bppRgb ) ;
171
131
172
132
FormatConvertedBitmap formatConverter = new FormatConvertedBitmap ( ) ;
173
133
formatConverter . BeginInit ( ) ;
@@ -176,10 +136,10 @@ internal override object GetBitmapFromBitmapSource(object source)
176
136
formatConverter . EndInit ( ) ;
177
137
178
138
formatConverter . CopyPixels (
179
- new Int32Rect ( 0 , 0 , imageWidth , imageHeight ) ,
180
- bmData . Scan0 ,
181
- bmData . Stride * ( bmData . Height - 1 ) + ( bmData . Width * 4 ) ,
182
- bmData . Stride ) ;
139
+ new Int32Rect ( 0 , 0 , imageWidth , imageHeight ) ,
140
+ bmData . Scan0 ,
141
+ bmData . Stride * ( bmData . Height - 1 ) + ( bmData . Width * 4 ) ,
142
+ bmData . Stride ) ;
183
143
184
144
bitmapFinal . UnlockBits ( bmData ) ;
185
145
0 commit comments