Ejemplo n.º 1
0
        public async Task DownloadQueryImage(string url, bool resizeIfBigger = true)
        {
            try
            {
                Uri uri = new Uri(url);
                if (uri.IsFile)
                {
                    string correctedImageLocalTempFile = url;
                    bool   imageModified = false;

                    if (resizeIfBigger)
                    {
                        var r = ImageEditor.ResizeIfBiggerAndFixOrientation(url);

                        correctedImageLocalTempFile = r.Item1;
                        imageModified = r.Item2;
                    }

                    this.DownloadedImageUri = new Uri(correctedImageLocalTempFile);

                    this.IsQueryImageModified = true; // local files are always "modified", so that we send them with the query
                }
                else
                {
                    using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
                    {
                        var response = await client.GetAsync(uri);

                        var tempFileName = System.IO.Path.GetTempFileName();

                        byte[] bytes = await response.Content.ReadAsByteArrayAsync();

                        System.IO.File.WriteAllBytes(tempFileName, bytes);

                        string correctedImageLocalTempFile = tempFileName;
                        bool   imageModified = false;

                        if (resizeIfBigger)
                        {
                            var r = ImageEditor.ResizeIfBiggerAndFixOrientation(tempFileName);

                            correctedImageLocalTempFile = r.Item1;
                            imageModified = r.Item2;
                        }

                        this.DownloadedImageUri   = new Uri(correctedImageLocalTempFile);
                        this.IsQueryImageModified = imageModified; // if image was not modified (resized or orientation fixed, we will mark it so. This will control sending image vs image url)
                    }
                }

                this.TransformedImageUri = this.DownloadedImageUri;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        public static Tuple <string, bool> ResizeIfBiggerAndFixOrientation(string srcImageFile, int targetWidthHeight = 1000) // This method checks if an image needs to be resized or rotated and does so
        {
            using (var srcBitmap = new Bitmap(srcImageFile))
            {
                int srcWidth  = srcBitmap.Width;
                int srcHeight = srcBitmap.Height;

                bool imageNeedsResize = false;
                int  n = srcWidth > srcHeight ? srcWidth : srcHeight;
                if (n > targetWidthHeight)
                {
                    imageNeedsResize = true;
                }                                                       // for images smaller than target set, target is updated to the larger dimension

                bool imageRotated = ImageEditor.FixImageOrientation(srcBitmap);

                if (imageNeedsResize || imageRotated) // If the image needs to be resized or rotated, do so
                {
                    var modifiedBitmap = srcBitmap;   // modifiedBitmap is either already rotated and/or needs resizing

                    if (imageNeedsResize)             // Resize the image if needed
                    {
                        // re-read dimensions
                        srcWidth  = modifiedBitmap.Width;
                        srcHeight = modifiedBitmap.Height;

                        n = srcWidth > srcHeight ? srcWidth : srcWidth;

                        int dstWidth  = srcWidth * targetWidthHeight / n;
                        int dstHeight = srcHeight * targetWidthHeight / n;

                        modifiedBitmap = ImageEditor.ResizeImage(modifiedBitmap, dstWidth, dstHeight);
                    }

                    var retValString = Path.GetTempFileName();
                    modifiedBitmap.Save(retValString, ImageFormat.Jpeg);
                    modifiedBitmap.Dispose();

                    return(new Tuple <string, bool>(retValString, true));
                }

                // no rotation or resize required
                return(new Tuple <string, bool>(srcImageFile, false));
            }
        }
Ejemplo n.º 3
0
        public static string RotateImage(string srcImageFile, float theta) // This method handles the location of an image loaded from the internet
        {
            if (theta == 0.0)                                              // If there's no rotation needed, just output the original image
            {
                return(srcImageFile);
            }

            using (var srcBitmap = new Bitmap(srcImageFile)) // Create a new rotated version of the image
            {
                var rotatedBitmap = ImageEditor.RotateImage(srcBitmap, theta);
                var retValString  = Path.GetTempFileName();

                rotatedBitmap.Save(retValString, ImageFormat.Jpeg);
                rotatedBitmap.Dispose();

                return(retValString);
            }
        }