Skip to content

Commit 0c1ecd4

Browse files
committed
Work on .NET 6 fixes
1 parent b8c177d commit 0c1ecd4

File tree

12 files changed

+394
-76
lines changed

12 files changed

+394
-76
lines changed

SmartImage.Lib/Engines/Impl/Ascii2DEngine.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using RestSharp;
1414
using SmartImage.Lib.Engines.Model;
1515

16+
#nullable disable
1617
// ReSharper disable CognitiveComplexity
1718

1819
// ReSharper disable IdentifierTypo
@@ -37,9 +38,16 @@ protected override Uri GetRawUri(ImageQuery query)
3738
{
3839
var uri = base.GetRawUri(query);
3940

40-
var request = WebRequest.Create(uri);
41+
// var request = WebRequest.Create(uri);
42+
// using var response = request.GetResponse();
43+
44+
var request = new HttpRequestMessage()
45+
{
46+
RequestUri = uri
47+
};
48+
49+
var response = new HttpClient().Send(request);
4150

42-
using var response = request.GetResponse();
4351

4452
/*
4553
* URL parameters
@@ -55,7 +63,13 @@ protected override Uri GetRawUri(ImageQuery query)
5563
*
5664
*/
5765

58-
string detailUrl = response.ResponseUri.ToString().Replace("/color/", "/bovw/");
66+
// var responseUri = response.ResponseUri;
67+
68+
Debug.Assert(response.RequestMessage?.RequestUri != null);
69+
70+
var responseUri = response.RequestMessage.RequestUri;
71+
72+
string detailUrl = responseUri.ToString().Replace("/color/", "/bovw/");
5973

6074
return new Uri(detailUrl);
6175

@@ -68,17 +82,17 @@ protected override SearchResultOrigin GetResultOrigin(ImageQuery query)
6882
var rawUri = GetRawUri(query);
6983

7084
var content = WebUtilities.GetString(rawUri.ToString());
71-
var diff = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - now);
85+
var diff = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - now);
7286

7387
var stub = new SearchResultOrigin()
7488
{
7589
InitialResponse = new RestResponse
7690
{
7791
Content = content
7892
},
79-
Retrieval = diff,
80-
InitialSuccess = true,
81-
RawUri = rawUri
93+
Retrieval = diff,
94+
InitialSuccess = true,
95+
RawUri = rawUri
8296
};
8397

8498

SmartImage.Lib/Engines/Model/BaseSearchEngine.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ protected virtual Uri GetRawUri(ImageQuery query)
8181

8282
protected virtual SearchResultOrigin GetResultOrigin(ImageQuery query)
8383
{
84-
// TODO: Refactor to use HttpClient
8584

8685
Uri rawUri = GetRawUri(query);
8786

8887

89-
var res = Network.GetResponse(rawUri.ToString(), (int) Timeout.TotalMilliseconds,
90-
Method.GET, FollowRedirects);
88+
var res = HttpUtilities.GetResponse(rawUri.ToString(),
89+
(int) Timeout.TotalMilliseconds, Method.GET, FollowRedirects);
9190

9291

9392
bool success;

SmartImage.Lib/Searching/ImageQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using SmartImage.Lib.Upload;
1212
using SmartImage.Lib.Utilities;
1313
using static Kantan.Diagnostics.LogCategories;
14-
14+
#pragma warning disable CA1416
1515
namespace SmartImage.Lib.Searching
1616
{
1717
/// <summary>

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using SmartImage.Lib.Utilities;
1111

1212
// ReSharper disable CognitiveComplexity
13-
#pragma warning disable 8629
13+
#pragma warning disable 8629,CA1416
1414
#nullable enable
1515

1616
namespace SmartImage.Lib.Searching

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Drawing;
5+
using System.Drawing.Drawing2D;
56
using System.Drawing.Imaging;
67
using System.IO;
78
using System.Linq;
@@ -22,6 +23,7 @@
2223
using RestSharp;
2324
using static Kantan.Diagnostics.LogCategories;
2425

26+
#pragma warning disable CA1416
2527
// ReSharper disable ConvertIfStatementToReturnStatement
2628
// ReSharper disable CognitiveComplexity
2729
// ReSharper disable PossibleNullReferenceException
@@ -95,6 +97,11 @@ public static string Download(Uri src, string path)
9597

9698
try {
9799
wc.DownloadFile(src.ToString(), combine);
100+
// WebUtilities.GetFile(src.ToString(), combine);
101+
// using var h = new HttpClient();
102+
// h.DownloadFile(src.ToString(), combine);
103+
104+
98105
return combine;
99106
}
100107
catch (Exception e) {
@@ -205,8 +212,8 @@ public static async Task<List<string>> FindDirectImages(string url, int count =
205212
return images;
206213
}
207214

208-
public static bool IsImage(string url, DirectImageCriterion directCriterion = DirectImageCriterion.Binary) =>
209-
IsImage(url, TimeoutMS, directCriterion);
215+
public static bool IsImage(string url, DirectImageCriterion directCriterion = DirectImageCriterion.Binary)
216+
=> IsImage(url, TimeoutMS, directCriterion);
210217

211218
public static bool IsImage(string url, long timeout, DirectImageCriterion directCriterion)
212219
{
@@ -221,7 +228,7 @@ public static bool IsImage(string url, long timeout, DirectImageCriterion direct
221228
return false;
222229
}
223230

224-
var response = Network.GetResponse(u.ToString(), (int) timeout, Method.HEAD);
231+
var response = HttpUtilities.GetResponse(u.ToString(), (int) timeout, Method.HEAD);
225232

226233
if (!response.IsSuccessful) {
227234
return false;
@@ -285,6 +292,44 @@ public static DisplayResolutionType GetDisplayResolution(int w, int h)
285292
};
286293

287294
}
295+
296+
public static Bitmap ResizeImage(Bitmap mg, Size newSize)
297+
{
298+
// todo
299+
double ratio = 0d;
300+
double myThumbWidth = 0d;
301+
double myThumbHeight = 0d;
302+
int x = 0;
303+
int y = 0;
304+
305+
Bitmap bp;
306+
307+
if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height / Convert.ToDouble(newSize.Height))) {
308+
ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
309+
}
310+
else {
311+
ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
312+
}
313+
314+
myThumbHeight = Math.Ceiling(mg.Height / ratio);
315+
myThumbWidth = Math.Ceiling(mg.Width / ratio);
316+
317+
//Size thumbSize = new Size((int)myThumbWidth, (int)myThumbHeight);
318+
Size thumbSize = new Size((int) newSize.Width, (int) newSize.Height);
319+
bp = new Bitmap(newSize.Width, newSize.Height);
320+
x = (newSize.Width - thumbSize.Width) / 2;
321+
y = (newSize.Height - thumbSize.Height);
322+
// Had to add System.Drawing class in front of Graphics ---
323+
System.Drawing.Graphics g = Graphics.FromImage(bp);
324+
g.SmoothingMode = SmoothingMode.HighQuality;
325+
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
326+
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
327+
Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
328+
g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);
329+
330+
return bp;
331+
332+
}
288333
}
289334

290335
public enum DirectImageCriterion

SmartImage/Core/AppInfo.cs

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ namespace SmartImage.Core
4040
/// </summary>
4141
public static class AppInfo
4242
{
43-
/// <summary>
44-
/// Name in ASCII art
45-
/// </summary>
46-
public const string NAME_BANNER =
47-
" ____ _ ___\n" +
48-
" / ___| _ __ ___ __ _ _ __| |_|_ _|_ __ ___ __ _ __ _ ___\n" +
49-
@" \___ \| '_ ` _ \ / _` | '__| __|| || '_ ` _ \ / _` |/ _` |/ _ \" + "\n" +
50-
" ___) | | | | | | (_| | | | |_ | || | | | | | (_| | (_| | __/\n" +
51-
@" |____/|_| |_| |_|\__,_|_| \__|___|_| |_| |_|\__,_|\__, |\___|" + "\n" +
52-
" |___/\n";
53-
54-
5543
/// <summary>
5644
/// Name
5745
/// </summary>
@@ -66,17 +54,7 @@ public static class AppInfo
6654
/// Config file name
6755
/// </summary>
6856
public const string NAME_CFG = "SmartImage.cfg";
69-
70-
public const string AUTHOR = "Read Stanton";
71-
72-
public const string URL_REPO = "https://github.com/Decimation/SmartImage";
73-
74-
public const string URL_README = "https://github.com/Decimation/SmartImage/blob/master/README.md";
75-
76-
public const string URL_ISSUE = "https://github.com/Decimation/SmartImage/issues/new";
77-
78-
public const string URL_WIKI = "https://github.com/Decimation/SmartImage/wiki";
79-
57+
8058
public static string AppFolder => Path.GetDirectoryName(ExeLocation);
8159

8260
public static Version Version => typeof(AppInfo).Assembly.GetName().Version!;
@@ -91,22 +69,15 @@ public static string ExeLocation
9169
get
9270
{
9371
var module = Process.GetCurrentProcess().MainModule;
72+
9473
Guard.AssertNotNull(module);
9574

9675
return module.FileName;
9776
}
9877
}
9978

10079

101-
public static bool IsAppFolderInPath
102-
{
103-
get
104-
{
105-
106-
107-
return FileSystem.IsFolderInPath(AppFolder);
108-
}
109-
}
80+
public static bool IsAppFolderInPath => FileSystem.IsFolderInPath(AppFolder);
11081

11182
/// <summary>
11283
/// Setup
@@ -118,32 +89,13 @@ public static void Setup()
11889
throw new NotSupportedException();
11990
}*/
12091

121-
12292
if (!IsAppFolderInPath) {
12393
AppIntegration.HandlePath(true);
12494
}
12595

12696
Debug.WriteLine($"Cli utilities: {ImageHelper.Utilities.QuickJoin()}", C_INFO);
12797

128-
/*var languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
129-
130-
bool zh = languages.Any(l => l.Contains("zh"));
131-
132-
bool ja = languages.Contains("ja");
133-
134-
if (ja || zh) {
135-
136-
/*Console.WriteLine("Non-Romance language detected!");
137-
Console.WriteLine("If English is not the main IME, things may not work properly!");
138-
139-
ConsoleManager.WaitForInput();#1#
140-
141-
Trace.WriteLine($"Languages: {languages.QuickJoin()}");
142-
}*/
143-
144-
//Windows.System.UserProfile.GlobalizationPreferences.Languages
145-
//Thread.CurrentThread.CurrentUICulture
146-
//CultureInfo.CurrentCulture
98+
14799
}
148100
}
149101
}

SmartImage/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public static class Program
148148
/// </summary>
149149
private static async Task Main(string[] args)
150150
{
151-
Debug.WriteLine($"{Process.GetCurrentProcess().MainModule}");
152151
/*
153152
* Setup
154153
* Check compatibility

0 commit comments

Comments
 (0)