@@ -83,73 +83,74 @@ public static int ScaleIntY(int i)
83
83
return ( int ) Math . Round ( i * scaleY ) ;
84
84
}
85
85
86
- public static Image ScaleImage ( Image img )
86
+ public static Image ScaleImage ( Image sourceImage )
87
87
{
88
- if ( img == null )
88
+ if ( sourceImage == null )
89
89
{
90
90
return null ;
91
91
}
92
92
93
- int w = img . Width ;
94
- int h = img . Height ;
95
- int sw = ScaleIntX ( w ) ;
96
- int sh = ScaleIntY ( h ) ;
93
+ var width = sourceImage . Width ;
94
+ var height = sourceImage . Height ;
95
+ var scaledWidth = ScaleIntX ( width ) ;
96
+ var scaledHeight = ScaleIntY ( height ) ;
97
97
98
- if ( w == sw && h == sh )
98
+ if ( width == scaledWidth && height == scaledHeight )
99
99
{
100
- return img ;
100
+ return sourceImage ;
101
101
}
102
102
103
- return ScaleImage ( img , sw , sh ) ;
103
+ return ScaleImage ( sourceImage , scaledWidth , scaledHeight ) ;
104
104
}
105
105
106
- private static Image ScaleImage ( Image img , int w , int h )
106
+ private static Image ScaleImage ( Image sourceImage , int width , int height )
107
107
{
108
- Contract . Requires ( img != null ) ;
109
- Contract . Requires ( w >= 0 ) ;
110
- Contract . Requires ( h >= 0 ) ;
108
+ Contract . Requires ( sourceImage != null ) ;
109
+ Contract . Requires ( width >= 0 ) ;
110
+ Contract . Requires ( height >= 0 ) ;
111
111
112
- var bmp = new Bitmap ( w , h , PixelFormat . Format32bppArgb ) ;
113
- using var g = Graphics . FromImage ( bmp ) ;
112
+ var scaledImage = new Bitmap ( width , height , PixelFormat . Format32bppArgb ) ;
113
+
114
+ using var g = Graphics . FromImage ( scaledImage ) ;
114
115
g . Clear ( Color . Transparent ) ;
115
116
116
117
g . SmoothingMode = SmoothingMode . HighQuality ;
117
118
g . CompositingQuality = CompositingQuality . HighQuality ;
118
119
119
- var wSrc = img . Width ;
120
- var hSrc = img . Height ;
120
+ var sourceWidth = sourceImage . Width ;
121
+ var sourceHeight = sourceImage . Height ;
121
122
122
- var im = InterpolationMode . HighQualityBicubic ;
123
- if ( wSrc > 0 && hSrc > 0 )
123
+ var interpolationMode = InterpolationMode . HighQualityBicubic ;
124
+ if ( sourceWidth > 0 && sourceHeight > 0 )
124
125
{
125
- if ( ( w % wSrc ) == 0 && ( h % hSrc ) == 0 )
126
+ if ( ( width % sourceWidth ) == 0 && ( height % sourceHeight ) == 0 )
126
127
{
127
- im = InterpolationMode . NearestNeighbor ;
128
+ interpolationMode = InterpolationMode . NearestNeighbor ;
128
129
}
129
130
}
130
131
131
- g . InterpolationMode = im ;
132
+ g . InterpolationMode = interpolationMode ;
132
133
133
- var rSource = new RectangleF ( 0.0f , 0.0f , wSrc , hSrc ) ;
134
- var rDest = new RectangleF ( 0.0f , 0.0f , w , h ) ;
135
- AdjustScaleRects ( ref rSource , ref rDest ) ;
134
+ var srcRect = new RectangleF ( 0.0f , 0.0f , sourceWidth , sourceHeight ) ;
135
+ var destRect = new RectangleF ( 0.0f , 0.0f , width , height ) ;
136
+ AdjustScaleRects ( ref srcRect , ref destRect ) ;
136
137
137
- g . DrawImage ( img , rDest , rSource , GraphicsUnit . Pixel ) ;
138
+ g . DrawImage ( sourceImage , destRect , srcRect , GraphicsUnit . Pixel ) ;
138
139
139
- return bmp ;
140
+ return scaledImage ;
140
141
}
141
142
142
- private static void AdjustScaleRects ( ref RectangleF rSource , ref RectangleF rDest )
143
+ private static void AdjustScaleRects ( ref RectangleF srcRect , ref RectangleF destRect )
143
144
{
144
- if ( rDest . Width > rSource . Width )
145
- rSource . X = rSource . X - 0.5f ;
146
- if ( rDest . Height > rSource . Height )
147
- rSource . Y = rSource . Y - 0.5f ;
148
-
149
- if ( rDest . Width < rSource . Width )
150
- rSource . X = rSource . X + 0.5f ;
151
- if ( rDest . Height < rSource . Height )
152
- rSource . Y = rSource . Y + 0.5f ;
145
+ if ( destRect . Width > srcRect . Width )
146
+ srcRect . X -= 0.5f ;
147
+ if ( destRect . Height > srcRect . Height )
148
+ srcRect . Y -= 0.5f ;
149
+
150
+ if ( destRect . Width < srcRect . Width )
151
+ srcRect . X += 0.5f ;
152
+ if ( destRect . Height < srcRect . Height )
153
+ srcRect . Y += 0.5f ;
153
154
}
154
155
}
155
156
}
0 commit comments