Esempio n. 1
0
        private async Task <byte[]> UploadAndThumbnailImage(string imageFilePath, int width, int height, bool smartCropping)
        {
            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Upload an image and generate a thumbnail
                //
                Log("Calling VisionServiceClient.GetThumbnailAsync()...");
                return(await VisionServiceClient.GetThumbnailAsync(imageFileStream, width, height, smartCropping));
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Esempio n. 2
0
        private static async Task <byte[]> UploadAndThumbnailImage(string imageFilePath, int width, int height, bool smartCropping)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(ComKey);

            Log("VisionServiceClient is created");

            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Upload an image and generate a thumbnail
                //
                Log("Calling VisionServiceClient.GetThumbnailAsync()...");
                return(await VisionServiceClient.GetThumbnailAsync(imageFileStream, width, height, smartCropping));
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a url to Project Oxford and generates a thumbnail
        /// </summary>
        /// <param name="imageUrl">The url of the image to generate a thumbnail for</param>
        /// <param name="width">Width of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="height">Height of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="smartCropping">Boolean flag for enabling smart cropping.</param>
        /// <returns></returns>
        private static async Task <byte[]> ThumbnailUrl(string imageUrl, int width, int height, bool smartCropping)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(ComKey);

            Log("VisionServiceClient is created");

            //
            // Generate a thumbnail for the given url
            //
            Log("Calling VisionServiceClient.GetThumbnailAsync()...");
            byte[] thumbnail = await VisionServiceClient.GetThumbnailAsync(imageUrl, width, height, smartCropping);

            return(thumbnail);

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Esempio n. 4
0
        public async Task<ActionResult> Thumbnail()
        {
            var result = await visionClient.GetThumbnailAsync(
                "https://oxfordportal.blob.core.windows.net/vision/Thumbnail/6-1.jpg",
                50,
                50
                );

            var result1 = await visionClient.GetThumbnailAsync(
                "https://oxfordportal.blob.core.windows.net/vision/Thumbnail/6-1.jpg",
                100,
                100
                );

            return View();
        }
Esempio n. 5
0
        public async Task <string> GetThumbnail(IRandomAccessStream stream, string filename, int width = 250, int height = 250)
        {
            byte[] thumbnailData = null;
            var    success       = false;

            while (!success)
            {
                try
                {
                    thumbnailData = await _client.GetThumbnailAsync(stream.AsStream(), width, height);

                    success = true;
                }
                catch (Exception ex)
                {
                    // wait out the limitation in the API
                    await Task.Delay(TimeSpan.FromSeconds(60));
                }
            }
            var file = await(await AdventureObjectStorageHelper.GetDataSaveFolder()).CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);

            using (var writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                await writeStream.WriteAsync(thumbnailData.AsBuffer());
            }

            return(file.Path);
        }
        /// <summary>
        /// Uploads the image to Project Oxford and generates a thumbnail
        /// </summary>
        /// <param name="imageFilePath">The image file path.</param>
        /// <param name="width">Width of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="height">Height of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="smartCropping">Boolean flag for enabling smart cropping.</param>
        /// <returns></returns>
        private async Task <byte[]> UploadAndThumbnailImage(string imageFilePath, int width, int height, bool smartCropping)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey, "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0");

            Log("VisionServiceClient is created");

            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Upload an image and generate a thumbnail
                //
                Log("Calling VisionServiceClient.GetThumbnailAsync()...");
                return(await VisionServiceClient.GetThumbnailAsync(imageFileStream, width, height, smartCropping));
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
Esempio n. 7
0
        static Bitmap SmartCropImage(Bitmap sourceImage)
        {
            VisionServiceClient VisionServiceClient = ComputerVisionService.GetClient();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                sourceImage.SaveAsPng(memoryStream);
                memoryStream.Position = 0;

                int width  = sourceImage.Width;
                int height = (int)(sourceImage.Width / 16.0f * 9.0f * 1.2f); // Set it to a 16:9 with an extra 20% to increase the overall size
                Console.WriteLine("Original Width: " + sourceImage.Width + " Original Height: " + sourceImage.Height + " Cropped Height: " + height);

                if (sourceImage.Height > height)
                {
                    Console.WriteLine("Calling VisionServiceClient.GetThumbnailAsync()...");
                    byte[] bytes = VisionServiceClient.GetThumbnailAsync(memoryStream, width, height).GetAwaiter().GetResult();

                    Bitmap croppedImage = Image.Load(bytes);

                    return(croppedImage);
                }
                else
                {
                    Console.WriteLine("Image was already small. No reason to crop");
                    return(sourceImage);
                }
            }
        }
Esempio n. 8
0
        public async Task <string> GetThumbnailAsync(string selectedFile, int width, int height, bool smartCropping)
        {
            IVisionServiceClient visionClient = new VisionServiceClient(_subscriptionKeyVision);
            string fileThumbnail = Path.GetTempFileName() + ".jpg";

            ErrorMesssage = string.Empty;
            try
            {
                if (File.Exists(selectedFile))
                {
                    using (var fileStreamVision = File.OpenRead(selectedFile))
                    {
                        var imgBytes = await visionClient.
                                       GetThumbnailAsync(fileStreamVision, width, height, smartCropping);

                        File.WriteAllBytes(fileThumbnail, imgBytes);
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorMesssage = exception.ToString();
            }

            return(fileThumbnail);
        }
Esempio n. 9
0
        public static async Task <byte[]> SmartThumbnailGen(string fname, int width, int height, bool smartCropping)
        {
            byte[] thumbnail           = null;
            VisionServiceClient client = new VisionServiceClient(API_key, API_location);

            if (File.Exists(fname))
            {
                using (Stream stream = File.OpenRead(fname))
                    thumbnail = await client.GetThumbnailAsync(stream, width, height, smartCropping);
            }
            return(thumbnail);
        }
Esempio n. 10
0
        private async Task <byte[]> ThumbnailUrl(string imageUrl, int width, int height, bool smartCropping)
        {
            byte[] thumbnail = await visionServiceClient.GetThumbnailAsync(imageUrl, width, height, smartCropping);

            return(thumbnail);
        }
        /// <summary>
        /// Uploads the image to Project Oxford and generates a thumbnail
        /// </summary>
        /// <param name="imageFilePath">The image file path.</param>
        /// <param name="width">Width of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="height">Height of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="smartCropping">Boolean flag for enabling smart cropping.</param>
        /// <returns></returns>
        private async Task<byte[]> UploadAndThumbnailImage(string imageFilePath, int width, int height, bool smartCropping)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
            Log("VisionServiceClient is created");

            using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
                //
                // Upload an image and generate a thumbnail
                //
                Log("Calling VisionServiceClient.GetThumbnailAsync()...");
                return await VisionServiceClient.GetThumbnailAsync(imageFileStream, width, height, smartCropping);
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
        /// <summary>
        /// Sends a url to Project Oxford and generates a thumbnail
        /// </summary>
        /// <param name="imageUrl">The url of the image to generate a thumbnail for</param>
        /// <param name="width">Width of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="height">Height of the thumbnail. It must be between 1 and 1024. Recommended minimum of 50.</param>
        /// <param name="smartCropping">Boolean flag for enabling smart cropping.</param>
        /// <returns></returns>
        private async Task<byte[]> ThumbnailUrl(string imageUrl, int width, int height, bool smartCropping)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Project Oxford Vision API Service client
            //
            VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
            Log("VisionServiceClient is created");

            //
            // Generate a thumbnail for the given url
            //
            Log("Calling VisionServiceClient.GetThumbnailAsync()...");
            byte[] thumbnail = await VisionServiceClient.GetThumbnailAsync(imageUrl, width, height, smartCropping);
            return thumbnail;

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }