-
Notifications
You must be signed in to change notification settings - Fork 507
/
ImageAnalysisQuickstart.cs
599 lines (535 loc) · 27.1 KB
/
ImageAnalysisQuickstart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Computer Vision SDK QuickStart
*
* Examples included:
* - Authenticate
* - Analyze Image with an image url
* - Analyze Image with a local file
* - Detect Objects with an image URL
* - Detect Objects with a local file
* - Generate Thumbnail from a URL and local image
*
* Prerequisites:
* - Visual Studio 2019 (or 2017, but note this is a .Net Core console app, not .Net Framework)
* - NuGet library: Microsoft.Azure.CognitiveServices.Vision.ComputerVision
* - Azure Computer Vision resource from https://ms.portal.azure.com
* - Create a .Net Core console app, then copy/paste this Program.cs file into it. Be sure to update the namespace if it's different.
* - Download local images (celebrities.jpg, objects.jpg, handwritten_text.jpg, and printed_text.jpg)
* from the link below then add to your bin/Debug/netcoreapp2.2 folder.
* https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/ComputerVision/Images
*
* How to run:
* - Once your prerequisites are complete, press the Start button in Visual Studio.
* - Each example displays a printout of its results.
*
* References:
* - .NET SDK: https://docs.microsoft.com/en-us/dotnet/api/overview/azure/cognitiveservices/client/computervision?view=azure-dotnet
* - API (testing console): https://westus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-2/operations/5d986960601faab4bf452005
* - Computer Vision documentation: https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/
*/
// <snippet_using_and_vars>
// <snippet_using>
using System;
using System.Collections.Generic;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Linq;
// </snippet_using>
namespace ComputerVisionQuickstart
{
class Program
{
// <snippet_vars>
// Add your Computer Vision key and endpoint
static string key = "PASTE_YOUR_COMPUTER_VISION_KEY_HERE";
static string endpoint = "PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE";
// </snippet_vars>
// </snippet_using_and_vars>
// Download these images (link in prerequisites), or you can use any appropriate image on your local machine.
private const string ANALYZE_LOCAL_IMAGE = "celebrities.jpg";
private const string DETECT_LOCAL_IMAGE = "objects.jpg";
private const string DETECT_DOMAIN_SPECIFIC_LOCAL = "celebrities.jpg";
// <snippet_analyze_url>
// URL image used for analyzing an image (image of puppy)
private const string ANALYZE_URL_IMAGE = "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png";
// </snippet_analyze_url>
// URL image for detecting objects (image of man on skateboard)
private const string DETECT_URL_IMAGE = "https://moderatorsampleimages.blob.core.windows.net/samples/sample9.png";
// URL image for detecting domain-specific content (image of ancient ruins)
private const string DETECT_DOMAIN_SPECIFIC_URL = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg";
static void Main(string[] args)
{
Console.WriteLine("Azure Cognitive Services Computer Vision - .NET quickstart example");
Console.WriteLine();
// <snippet_main_calls>
// Create a client
ComputerVisionClient client = Authenticate(endpoint, key);
// Analyze an image to get features and other properties.
AnalyzeImageUrl(client, ANALYZE_URL_IMAGE).Wait();
// </snippet_main_calls>
AnalyzeImageLocal(client, ANALYZE_LOCAL_IMAGE).Wait();
// Detect objects in an image.
DetectObjectsUrl(client, DETECT_URL_IMAGE).Wait();
DetectObjectsLocal(client, DETECT_LOCAL_IMAGE).Wait();
// Detect domain-specific content in both a URL image and a local image.
DetectDomainSpecific(client, DETECT_DOMAIN_SPECIFIC_URL, DETECT_DOMAIN_SPECIFIC_LOCAL).Wait();
// Generate a thumbnail image from a URL and local image
GenerateThumbnail(client, ANALYZE_URL_IMAGE, DETECT_LOCAL_IMAGE).Wait();
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine();
Console.WriteLine("Computer Vision quickstart is complete.");
Console.WriteLine();
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
// <snippet_auth>
/*
* AUTHENTICATE
* Creates a Computer Vision client used by each example.
*/
public static ComputerVisionClient Authenticate(string endpoint, string key)
{
ComputerVisionClient client =
new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{ Endpoint = endpoint };
return client;
}
// </snippet_auth>
/*
* END - Authenticate
*/
// <snippet_visualfeatures>
/*
* ANALYZE IMAGE - URL IMAGE
* Analyze URL image. Extracts captions, categories, tags, objects, faces, racy/adult/gory content,
* brands, celebrities, landmarks, color scheme, and image types.
*/
public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("ANALYZE IMAGE - URL");
Console.WriteLine();
// Creating a list that defines the features to be extracted from the image.
List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
{
VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
VisualFeatureTypes.Objects
};
// </snippet_visualfeatures>
Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
Console.WriteLine();
// <snippet_analyze>
// Analyze the URL image
ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, visualFeatures: features);
// <snippet_describe>
// Summarizes the image content.
Console.WriteLine("Summary:");
foreach (var caption in results.Description.Captions)
{
Console.WriteLine($"{caption.Text} with confidence {caption.Confidence}");
}
Console.WriteLine();
// </snippet_describe>
// <snippet_categorize>
// Display categories the image is divided into.
Console.WriteLine("Categories:");
foreach (var category in results.Categories)
{
Console.WriteLine($"{category.Name} with confidence {category.Score}");
}
Console.WriteLine();
// </snippet_categorize>
// <snippet_tags>
// Image tags and their confidence score
Console.WriteLine("Tags:");
foreach (var tag in results.Tags)
{
Console.WriteLine($"{tag.Name} {tag.Confidence}");
}
Console.WriteLine();
// </snippet_tags>
// <snippet_objects>
// Objects
Console.WriteLine("Objects:");
foreach (var obj in results.Objects)
{
Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " +
$"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}");
}
Console.WriteLine();
// </snippet_objects>
// <snippet_faces>
// Faces
Console.WriteLine("Faces:");
foreach (var face in results.Faces)
{
Console.WriteLine($"A {face.Gender} of age {face.Age} at location {face.FaceRectangle.Left}, " +
$"{face.FaceRectangle.Left}, {face.FaceRectangle.Top + face.FaceRectangle.Width}, " +
$"{face.FaceRectangle.Top + face.FaceRectangle.Height}");
}
Console.WriteLine();
// </snippet_faces>
// <snippet_adult>
// Adult or racy content, if any.
Console.WriteLine("Adult:");
Console.WriteLine($"Has adult content: {results.Adult.IsAdultContent} with confidence {results.Adult.AdultScore}");
Console.WriteLine($"Has racy content: {results.Adult.IsRacyContent} with confidence {results.Adult.RacyScore}");
Console.WriteLine($"Has gory content: {results.Adult.IsGoryContent} with confidence {results.Adult.GoreScore}");
Console.WriteLine();
// </snippet_adult>
// <snippet_brands>
// Well-known (or custom, if set) brands.
Console.WriteLine("Brands:");
foreach (var brand in results.Brands)
{
Console.WriteLine($"Logo of {brand.Name} with confidence {brand.Confidence} at location {brand.Rectangle.X}, " +
$"{brand.Rectangle.X + brand.Rectangle.W}, {brand.Rectangle.Y}, {brand.Rectangle.Y + brand.Rectangle.H}");
}
Console.WriteLine();
// </snippet_brands>
// <snippet_celebs>
// Celebrities in image, if any.
Console.WriteLine("Celebrities:");
foreach (var category in results.Categories)
{
if (category.Detail?.Celebrities != null)
{
foreach (var celeb in category.Detail.Celebrities)
{
Console.WriteLine($"{celeb.Name} with confidence {celeb.Confidence} at location {celeb.FaceRectangle.Left}, " +
$"{celeb.FaceRectangle.Top}, {celeb.FaceRectangle.Height}, {celeb.FaceRectangle.Width}");
}
}
}
Console.WriteLine();
// </snippet_celebs>
// <snippet_landmarks>
// Popular landmarks in image, if any.
Console.WriteLine("Landmarks:");
foreach (var category in results.Categories)
{
if (category.Detail?.Landmarks != null)
{
foreach (var landmark in category.Detail.Landmarks)
{
Console.WriteLine($"{landmark.Name} with confidence {landmark.Confidence}");
}
}
}
Console.WriteLine();
// </snippet_landmarks>
// <snippet_color>
// Identifies the color scheme.
Console.WriteLine("Color Scheme:");
Console.WriteLine("Is black and white?: " + results.Color.IsBWImg);
Console.WriteLine("Accent color: " + results.Color.AccentColor);
Console.WriteLine("Dominant background color: " + results.Color.DominantColorBackground);
Console.WriteLine("Dominant foreground color: " + results.Color.DominantColorForeground);
Console.WriteLine("Dominant colors: " + string.Join(",", results.Color.DominantColors));
Console.WriteLine();
// </snippet_color>
// <snippet_type>
// Detects the image types.
Console.WriteLine("Image Type:");
Console.WriteLine("Clip Art Type: " + results.ImageType.ClipArtType);
Console.WriteLine("Line Drawing Type: " + results.ImageType.LineDrawingType);
Console.WriteLine();
// </snippet_type>
// </snippet_analyze>
}
/*
* END - ANALYZE IMAGE - URL IMAGE
*/
/*
* ANALYZE IMAGE - LOCAL IMAGE
* Analyze local image. Extracts captions, categories, tags, objects, faces, racy/adult/gory content,
* brands, celebrities, landmarks, color scheme, and image types.
*/
public static async Task AnalyzeImageLocal(ComputerVisionClient client, string localImage)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("ANALYZE IMAGE - LOCAL IMAGE");
Console.WriteLine();
// Creating a list that defines the features to be extracted from the image.
List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
{
VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
VisualFeatureTypes.Objects
};
Console.WriteLine($"Analyzing the local image {Path.GetFileName(localImage)}...");
Console.WriteLine();
using (Stream analyzeImageStream = File.OpenRead(localImage))
{
// Analyze the local image.
ImageAnalysis results = await client.AnalyzeImageInStreamAsync(analyzeImageStream, visualFeatures: features);
// Summarizes the image content.
if (null != results.Description && null != results.Description.Captions)
{
Console.WriteLine("Summary:");
foreach (var caption in results.Description.Captions)
{
Console.WriteLine($"{caption.Text} with confidence {caption.Confidence}");
}
Console.WriteLine();
}
// Display categories the image is divided into.
Console.WriteLine("Categories:");
foreach (var category in results.Categories)
{
Console.WriteLine($"{category.Name} with confidence {category.Score}");
}
Console.WriteLine();
// Image tags and their confidence score
if (null != results.Tags)
{
Console.WriteLine("Tags:");
foreach (var tag in results.Tags)
{
Console.WriteLine($"{tag.Name} {tag.Confidence}");
}
Console.WriteLine();
}
// Objects
if (null != results.Objects)
{
Console.WriteLine("Objects:");
foreach (var obj in results.Objects)
{
Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " +
$"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}");
}
Console.WriteLine();
}
// Detected faces, if any.
if (null != results.Faces)
{
Console.WriteLine("Faces:");
foreach (var face in results.Faces)
{
Console.WriteLine($"A {face.Gender} of age {face.Age} at location {face.FaceRectangle.Left}, {face.FaceRectangle.Top}, " +
$"{face.FaceRectangle.Left + face.FaceRectangle.Width}, {face.FaceRectangle.Top + face.FaceRectangle.Height}");
}
Console.WriteLine();
}
// Adult or racy content, if any.
if (null != results.Adult)
{
Console.WriteLine("Adult:");
Console.WriteLine($"Has adult content: {results.Adult.IsAdultContent} with confidence {results.Adult.AdultScore}");
Console.WriteLine($"Has racy content: {results.Adult.IsRacyContent} with confidence {results.Adult.RacyScore}");
Console.WriteLine($"Has gory content: {results.Adult.IsGoryContent} with confidence {results.Adult.GoreScore}");
Console.WriteLine();
}
// Well-known brands, if any.
if (null != results.Brands)
{
Console.WriteLine("Brands:");
foreach (var brand in results.Brands)
{
Console.WriteLine($"Logo of {brand.Name} with confidence {brand.Confidence} at location {brand.Rectangle.X}, " +
$"{brand.Rectangle.X + brand.Rectangle.W}, {brand.Rectangle.Y}, {brand.Rectangle.Y + brand.Rectangle.H}");
}
Console.WriteLine();
}
// Celebrities in image, if any.
if (null != results.Categories)
{
Console.WriteLine("Celebrities:");
foreach (var category in results.Categories)
{
if (category.Detail?.Celebrities != null)
{
foreach (var celeb in category.Detail.Celebrities)
{
Console.WriteLine($"{celeb.Name} with confidence {celeb.Confidence} at location {celeb.FaceRectangle.Left}, " +
$"{celeb.FaceRectangle.Top},{celeb.FaceRectangle.Height},{celeb.FaceRectangle.Width}");
}
}
}
Console.WriteLine();
}
// Popular landmarks in image, if any.
if (null != results.Categories)
{
Console.WriteLine("Landmarks:");
foreach (var category in results.Categories)
{
if (category.Detail?.Landmarks != null)
{
foreach (var landmark in category.Detail.Landmarks)
{
Console.WriteLine($"{landmark.Name} with confidence {landmark.Confidence}");
}
}
}
Console.WriteLine();
}
// Identifies the color scheme.
if (null != results.Color)
{
Console.WriteLine("Color Scheme:");
Console.WriteLine("Is black and white?: " + results.Color.IsBWImg);
Console.WriteLine("Accent color: " + results.Color.AccentColor);
Console.WriteLine("Dominant background color: " + results.Color.DominantColorBackground);
Console.WriteLine("Dominant foreground color: " + results.Color.DominantColorForeground);
Console.WriteLine("Dominant colors: " + string.Join(",", results.Color.DominantColors));
Console.WriteLine();
}
// Detects the image types.
if (null != results.ImageType)
{
Console.WriteLine("Image Type:");
Console.WriteLine("Clip Art Type: " + results.ImageType.ClipArtType);
Console.WriteLine("Line Drawing Type: " + results.ImageType.LineDrawingType);
Console.WriteLine();
}
}
}
/*
* END - ANALYZE IMAGE - LOCAL IMAGE
*/
/*
* DETECT OBJECTS - URL IMAGE
*/
public static async Task DetectObjectsUrl(ComputerVisionClient client, string urlImage)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("DETECT OBJECTS - URL IMAGE");
Console.WriteLine();
Console.WriteLine($"Detecting objects in URL image {Path.GetFileName(urlImage)}...");
Console.WriteLine();
// Detect the objects
DetectResult detectObjectAnalysis = await client.DetectObjectsAsync(urlImage);
// For each detected object in the picture, print out the bounding object detected, confidence of that detection and bounding box within the image
Console.WriteLine("Detected objects:");
foreach (var obj in detectObjectAnalysis.Objects)
{
Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " +
$"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}");
}
Console.WriteLine();
}
/*
* END - DETECT OBJECTS - URL IMAGE
*/
/*
* DETECT OBJECTS - LOCAL IMAGE
* This is an alternative way to detect objects, instead of doing so through AnalyzeImage.
*/
public static async Task DetectObjectsLocal(ComputerVisionClient client, string localImage)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("DETECT OBJECTS - LOCAL IMAGE");
Console.WriteLine();
using (Stream stream = File.OpenRead(localImage))
{
// Make a call to the Computer Vision service using the local file
DetectResult results = await client.DetectObjectsInStreamAsync(stream);
Console.WriteLine($"Detecting objects in local image {Path.GetFileName(localImage)}...");
Console.WriteLine();
// For each detected object in the picture, print out the bounding object detected, confidence of that detection and bounding box within the image
Console.WriteLine("Detected objects:");
foreach (var obj in results.Objects)
{
Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " +
$"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}");
}
Console.WriteLine();
}
}
/*
* END - DETECT OBJECTS - LOCAL IMAGE
*/
/*
* DETECT DOMAIN-SPECIFIC CONTENT
* Recognizes landmarks or celebrities in an image.
*/
public static async Task DetectDomainSpecific(ComputerVisionClient client, string urlImage, string localImage)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("DETECT DOMAIN-SPECIFIC CONTENT - URL & LOCAL IMAGE");
Console.WriteLine();
// Detect the domain-specific content in a URL image.
DomainModelResults resultsUrl = await client.AnalyzeImageByDomainAsync("landmarks", urlImage);
// Display results.
Console.WriteLine($"Detecting landmarks in the URL image {Path.GetFileName(urlImage)}...");
var jsonUrl = JsonConvert.SerializeObject(resultsUrl.Result);
JObject resultJsonUrl = JObject.Parse(jsonUrl);
if (resultJsonUrl["landmarks"].Any())
{
Console.WriteLine($"Landmark detected: {resultJsonUrl["landmarks"][0]["name"]} " +
$"with confidence {resultJsonUrl["landmarks"][0]["confidence"]}.");
}
Console.WriteLine();
// Detect the domain-specific content in a local image.
using (Stream imageStream = File.OpenRead(localImage))
{
// Change "celebrities" to "landmarks" if that is the domain you are interested in.
DomainModelResults resultsLocal = await client.AnalyzeImageByDomainInStreamAsync("celebrities", imageStream);
Console.WriteLine($"Detecting celebrities in the local image {Path.GetFileName(localImage)}...");
// Display results.
var jsonLocal = JsonConvert.SerializeObject(resultsLocal.Result);
JObject resultJsonLocal = JObject.Parse(jsonLocal);
if (resultJsonLocal["celebrities"].Any())
{
Console.WriteLine($"Celebrity detected: {resultJsonLocal["celebrities"][0]["name"]} " +
$"with confidence {resultJsonLocal["celebrities"][0]["confidence"]}");
}
}
Console.WriteLine();
}
/*
* END - DETECT DOMAIN-SPECIFIC CONTENT
*/
/*
* GENERATE THUMBNAIL
* Taking in a URL and local image, this example will generate a thumbnail image with specified width/height (pixels).
* The thumbnail will be saved locally.
*/
public static async Task GenerateThumbnail(ComputerVisionClient client, string urlImage, string localImage)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("GENERATE THUMBNAIL - URL & LOCAL IMAGE");
Console.WriteLine();
// Thumbnails will be saved locally in your bin\Debug\netcoreappx.x\ folder of this project.
string localSavePath = @".";
// URL
Console.WriteLine("Generating thumbnail with URL image...");
// Setting smartCropping to true enables the image to adjust its aspect ratio
// to center on the area of interest in the image. Change the width/height, if desired.
Stream thumbnailUrl = await client.GenerateThumbnailAsync(60, 60, urlImage, true);
string imageNameUrl = Path.GetFileName(urlImage);
string thumbnailFilePathUrl = Path.Combine(localSavePath, imageNameUrl.Insert(imageNameUrl.Length - 4, "_thumb"));
Console.WriteLine("Saving thumbnail from URL image to " + thumbnailFilePathUrl);
using (Stream file = File.Create(thumbnailFilePathUrl)) { thumbnailUrl.CopyTo(file); }
Console.WriteLine();
// LOCAL
Console.WriteLine("Generating thumbnail with local image...");
using (Stream imageStream = File.OpenRead(localImage))
{
Stream thumbnailLocal = await client.GenerateThumbnailInStreamAsync(100, 100, imageStream, smartCropping: true);
string imageNameLocal = Path.GetFileName(localImage);
string thumbnailFilePathLocal = Path.Combine(localSavePath,
imageNameLocal.Insert(imageNameLocal.Length - 4, "_thumb"));
// Save to file
Console.WriteLine("Saving thumbnail from local image to " + thumbnailFilePathLocal);
using (Stream file = File.Create(thumbnailFilePathLocal)) { thumbnailLocal.CopyTo(file); }
}
Console.WriteLine();
}
/*
* END - GENERATE THUMBNAIL
*/
}
}