共用方式為


Computer Vision – Generate Thumbnail

We will start by Creating a new project just like we did with the Setting up the Project Post this time we will name the project Generate Thumbnail.

Once your code looks like the above image add the using statement “using System.Net.Http.Headers;”

Enter in your Computer Vision API Key in between the quotes for the variable “skey”. For the const string apiMethod  type within the quotes generateThumbnail

In the const string fileSource within the quotes add a file path to an image you will be generating the Thumbnail from.

At this point your code should look like

In the static void Main(string[] args) section between the braces add the following

GenerateThumbnail(fileSource, 80, 80, true);
Console.ReadLine();

At this point the code should look like

Note: you will see a red squiggly line under GenerateThumbnail,

Now lets add the next block of code under the static void Main block of code

public static async void GenerateThumbnail(string fileSource, int width, int height, bool smart)
{
byte[] thumbnail = await GetThumbnail(fileSource, width, height, smart);

string thumbnailFullPath = string.Format("{0}\\thumbnail_{1:yyyy-MMM-dd_hh-mm-ss}.jpg",
Path.GetDirectoryName(fileSource), DateTime.Now);

using (BinaryWriter bw = new BinaryWriter(new FileStream(thumbnailFullPath, FileMode.OpenOrCreate, FileAccess.Write)))
bw.Write(thumbnail);
}

Your code should now look like this

Now under that the block of code you just added give your self some more space and add the following block of code.

public static async Task<byte[]> GetThumbnail(string fileSource, int width, int height, bool smart)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", skey);

string requestParameters = $"width={width.ToString()}&height={height.ToString()}&smartCropping={smart.ToString().ToLower()}";
string uri = uriBase + apiMethod + "?" + requestParameters;

HttpResponseMessage response = null;
byte[] byteData = GetImageAsByteArray(fileSource);

using (ByteArrayContent content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);

return await response.Content.ReadAsByteArrayAsync();

}
}

Your code should now look like this

Now we just need to add one more small block of code under that last one you added

public static byte[] GetImageAsByteArray(string fileSource)
{
FileStream fileStream = new FileStream(fileSource, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}

Your code should now look like this

Now just make sure you entered the key in correctly and that have an image file path set for the const string fileSource

Build the code make sure there are no errors and click on Start, Check the location of the file that was set in the fileSource and verify there is a much smaller image there a (“thumbnail”)