@@ -135,13 +135,16 @@ private void UpdateTrayIcon()
135135 bool global = config . sysProxyMode == ( int ) ProxyMode . Global ;
136136 bool random = config . random ;
137137
138- Bitmap icon = null ;
139138 try
140139 {
141- icon = new Bitmap ( "icon.png" ) ;
140+ using ( Bitmap icon = new Bitmap ( "icon.png" ) )
141+ {
142+ _notifyIcon . Icon = Icon . FromHandle ( icon . GetHicon ( ) ) ;
143+ }
142144 }
143145 catch
144146 {
147+ Bitmap icon = null ;
145148 if ( dpi < 97 )
146149 {
147150 // dpi = 96;
@@ -171,8 +174,8 @@ private void UpdateTrayIcon()
171174 mul_r = 0.4 ;
172175 }
173176
177+ using ( Bitmap iconCopy = new Bitmap ( icon ) )
174178 {
175- Bitmap iconCopy = new Bitmap ( icon ) ;
176179 for ( int x = 0 ; x < iconCopy . Width ; x ++ )
177180 {
178181 for ( int y = 0 ; y < iconCopy . Height ; y ++ )
@@ -185,10 +188,9 @@ private void UpdateTrayIcon()
185188 ( ( byte ) ( color . B * mul_b ) ) ) ) ;
186189 }
187190 }
188- icon = iconCopy ;
191+ _notifyIcon . Icon = Icon . FromHandle ( iconCopy . GetHicon ( ) ) ;
189192 }
190193 }
191- _notifyIcon . Icon = Icon . FromHandle ( icon . GetHicon ( ) ) ;
192194
193195 // we want to show more details but notify icon title is limited to 63 characters
194196 string text = ( enabled ?
@@ -857,20 +859,22 @@ private void Config_Click(object sender, EventArgs e)
857859
858860 private void Import_Click ( object sender , EventArgs e )
859861 {
860- OpenFileDialog dlg = new OpenFileDialog ( ) ;
861- dlg . InitialDirectory = System . Windows . Forms . Application . StartupPath ;
862- if ( dlg . ShowDialog ( ) == DialogResult . OK )
862+ using ( OpenFileDialog dlg = new OpenFileDialog ( ) )
863863 {
864- string name = dlg . FileName ;
865- Configuration cfg = Configuration . LoadFile ( name ) ;
866- if ( cfg . configs . Count == 1 && cfg . configs [ 0 ] . server == Configuration . GetDefaultServer ( ) . server )
867- {
868- MessageBox . Show ( "Load config file failed" , "ShadowsocksR" ) ;
869- }
870- else
864+ dlg . InitialDirectory = System . Windows . Forms . Application . StartupPath ;
865+ if ( dlg . ShowDialog ( ) == DialogResult . OK )
871866 {
872- controller . MergeConfiguration ( cfg ) ;
873- LoadCurrentConfiguration ( ) ;
867+ string name = dlg . FileName ;
868+ Configuration cfg = Configuration . LoadFile ( name ) ;
869+ if ( cfg . configs . Count == 1 && cfg . configs [ 0 ] . server == Configuration . GetDefaultServer ( ) . server )
870+ {
871+ MessageBox . Show ( "Load config file failed" , "ShadowsocksR" ) ;
872+ }
873+ else
874+ {
875+ controller . MergeConfiguration ( cfg ) ;
876+ LoadCurrentConfiguration ( ) ;
877+ }
874878 }
875879 }
876880 }
@@ -1169,32 +1173,33 @@ private void CopyAddress_Click(object sender, EventArgs e)
11691173
11701174 private bool ScanQRCode ( Screen screen , Bitmap fullImage , Rectangle cropRect , out string url , out Rectangle rect )
11711175 {
1172- Bitmap target = new Bitmap ( cropRect . Width , cropRect . Height ) ;
1173-
1174- using ( Graphics g = Graphics . FromImage ( target ) )
1175- {
1176- g . DrawImage ( fullImage , new Rectangle ( 0 , 0 , cropRect . Width , cropRect . Height ) ,
1177- cropRect ,
1178- GraphicsUnit . Pixel ) ;
1179- }
1180- var source = new BitmapLuminanceSource ( target ) ;
1181- var bitmap = new BinaryBitmap ( new HybridBinarizer ( source ) ) ;
1182- QRCodeReader reader = new QRCodeReader ( ) ;
1183- var result = reader . decode ( bitmap ) ;
1184- if ( result != null )
1176+ using ( Bitmap target = new Bitmap ( cropRect . Width , cropRect . Height ) )
11851177 {
1186- url = result . Text ;
1187- double minX = Int32 . MaxValue , minY = Int32 . MaxValue , maxX = 0 , maxY = 0 ;
1188- foreach ( ResultPoint point in result . ResultPoints )
1178+ using ( Graphics g = Graphics . FromImage ( target ) )
1179+ {
1180+ g . DrawImage ( fullImage , new Rectangle ( 0 , 0 , cropRect . Width , cropRect . Height ) ,
1181+ cropRect ,
1182+ GraphicsUnit . Pixel ) ;
1183+ }
1184+ var source = new BitmapLuminanceSource ( target ) ;
1185+ var bitmap = new BinaryBitmap ( new HybridBinarizer ( source ) ) ;
1186+ QRCodeReader reader = new QRCodeReader ( ) ;
1187+ var result = reader . decode ( bitmap ) ;
1188+ if ( result != null )
11891189 {
1190- minX = Math . Min ( minX , point . X ) ;
1191- minY = Math . Min ( minY , point . Y ) ;
1192- maxX = Math . Max ( maxX , point . X ) ;
1193- maxY = Math . Max ( maxY , point . Y ) ;
1190+ url = result . Text ;
1191+ double minX = Int32 . MaxValue , minY = Int32 . MaxValue , maxX = 0 , maxY = 0 ;
1192+ foreach ( ResultPoint point in result . ResultPoints )
1193+ {
1194+ minX = Math . Min ( minX , point . X ) ;
1195+ minY = Math . Min ( minY , point . Y ) ;
1196+ maxX = Math . Max ( maxX , point . X ) ;
1197+ maxY = Math . Max ( maxY , point . Y ) ;
1198+ }
1199+ //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1200+ rect = new Rectangle ( cropRect . Left + ( int ) minX , cropRect . Top + ( int ) minY , ( int ) ( maxX - minX ) , ( int ) ( maxY - minY ) ) ;
1201+ return true ;
11941202 }
1195- //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1196- rect = new Rectangle ( cropRect . Left + ( int ) minX , cropRect . Top + ( int ) minY , ( int ) ( maxX - minX ) , ( int ) ( maxY - minY ) ) ;
1197- return true ;
11981203 }
11991204 url = "" ;
12001205 rect = new Rectangle ( ) ;
@@ -1203,32 +1208,33 @@ private bool ScanQRCode(Screen screen, Bitmap fullImage, Rectangle cropRect, out
12031208
12041209 private bool ScanQRCodeStretch ( Screen screen , Bitmap fullImage , Rectangle cropRect , double mul , out string url , out Rectangle rect )
12051210 {
1206- Bitmap target = new Bitmap ( ( int ) ( cropRect . Width * mul ) , ( int ) ( cropRect . Height * mul ) ) ;
1207-
1208- using ( Graphics g = Graphics . FromImage ( target ) )
1211+ using ( Bitmap target = new Bitmap ( ( int ) ( cropRect . Width * mul ) , ( int ) ( cropRect . Height * mul ) ) )
12091212 {
1210- g . DrawImage ( fullImage , new Rectangle ( 0 , 0 , target . Width , target . Height ) ,
1211- cropRect ,
1212- GraphicsUnit . Pixel ) ;
1213- }
1214- var source = new BitmapLuminanceSource ( target ) ;
1215- var bitmap = new BinaryBitmap ( new HybridBinarizer ( source ) ) ;
1216- QRCodeReader reader = new QRCodeReader ( ) ;
1217- var result = reader . decode ( bitmap ) ;
1218- if ( result != null )
1219- {
1220- url = result . Text ;
1221- double minX = Int32 . MaxValue , minY = Int32 . MaxValue , maxX = 0 , maxY = 0 ;
1222- foreach ( ResultPoint point in result . ResultPoints )
1213+ using ( Graphics g = Graphics . FromImage ( target ) )
1214+ {
1215+ g . DrawImage ( fullImage , new Rectangle ( 0 , 0 , target . Width , target . Height ) ,
1216+ cropRect ,
1217+ GraphicsUnit . Pixel ) ;
1218+ }
1219+ var source = new BitmapLuminanceSource ( target ) ;
1220+ var bitmap = new BinaryBitmap ( new HybridBinarizer ( source ) ) ;
1221+ QRCodeReader reader = new QRCodeReader ( ) ;
1222+ var result = reader . decode ( bitmap ) ;
1223+ if ( result != null )
12231224 {
1224- minX = Math . Min ( minX , point . X ) ;
1225- minY = Math . Min ( minY , point . Y ) ;
1226- maxX = Math . Max ( maxX , point . X ) ;
1227- maxY = Math . Max ( maxY , point . Y ) ;
1225+ url = result . Text ;
1226+ double minX = Int32 . MaxValue , minY = Int32 . MaxValue , maxX = 0 , maxY = 0 ;
1227+ foreach ( ResultPoint point in result . ResultPoints )
1228+ {
1229+ minX = Math . Min ( minX , point . X ) ;
1230+ minY = Math . Min ( minY , point . Y ) ;
1231+ maxX = Math . Max ( maxX , point . X ) ;
1232+ maxY = Math . Max ( maxY , point . Y ) ;
1233+ }
1234+ //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1235+ rect = new Rectangle ( cropRect . Left + ( int ) ( minX / mul ) , cropRect . Top + ( int ) ( minY / mul ) , ( int ) ( ( maxX - minX ) / mul ) , ( int ) ( ( maxY - minY ) / mul ) ) ;
1236+ return true ;
12281237 }
1229- //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY));
1230- rect = new Rectangle ( cropRect . Left + ( int ) ( minX / mul ) , cropRect . Top + ( int ) ( minY / mul ) , ( int ) ( ( maxX - minX ) / mul ) , ( int ) ( ( maxY - minY ) / mul ) ) ;
1231- return true ;
12321238 }
12331239 url = "" ;
12341240 rect = new Rectangle ( ) ;
0 commit comments