public static System.Drawing.Image ProcessImage(string path)
        {
            System.Drawing.Image result = null;

            if (File.Exists(path))
            {
                System.Drawing.Image img = Image_Functions.CropImageToCenter(System.Drawing.Image.FromFile(path));

                result = Image_Functions.SetImageSize(img, 200, 200);
            }

            return(result);
        }
Esempio n. 2
0
            public static Image Get(string filename)
            {
                Image result = null;

                if (!string.IsNullOrEmpty(filename))
                {
                    string localPath = Path.Combine(FileLocations.TrakHoundTemp, filename);

                    // Look for file in local cache
                    if (File.Exists(localPath))
                    {
                        result = Image_Functions.GetImageFromFile(localPath);
                    }
                    // If not in local cache (already downloaded) then download it
                    else
                    {
                        const int MAX_ATTEMPTS = 3;
                        int       attempts     = 0;
                        bool      success      = false;

                        while (attempts < MAX_ATTEMPTS && !success)
                        {
                            attempts += 1;

                            using (var webClient = new WebClient())
                            {
                                try
                                {
                                    byte[] data = webClient.DownloadData("https://www.feenux.com/trakhound/files/profile_images/" + filename);

                                    using (MemoryStream mem = new MemoryStream(data))
                                    {
                                        result = Image.FromStream(mem);

                                        string localSavePath = Path.Combine(FileLocations.TrakHoundTemp, filename);

                                        result.Save(localSavePath);
                                    }

                                    success = true;
                                }
                                catch (Exception ex) { Logger.Log("Exception : " + ex.Message); }
                            }
                        }
                    }
                }

                return(result);
            }
Esempio n. 3
0
        private void UploadDeviceImage_Worker(object o)
        {
            if (o != null)
            {
                var info = (ImageInfo)o;

                string fileId = null;

                if (info.UserConfig != null)
                {
                    string contentType = null;

                    try
                    {
                        string ext = Path.GetExtension(info.FileId);

                        if (ext == "jpg" || ext == ".jpg")
                        {
                            contentType = "image/jpeg";
                        }
                        else if (ext == "png" || ext == ".png")
                        {
                            contentType = "image/png";
                        }
                        else if (ext == "gif" || ext == ".gif")
                        {
                            contentType = "image/gif";
                        }

                        var img = System.Drawing.Image.FromFile(info.FileId);
                        if (img != null)
                        {
                            if (img.Width > img.Height)
                            {
                                img = Image_Functions.SetImageSize(img, 300, 300);
                            }
                            else
                            {
                                img = Image_Functions.SetImageSize(img, 0, 150);
                            }

                            string tempPath = Path.ChangeExtension(String_Functions.RandomString(20), ext);
                            tempPath = Path.Combine(FileLocations.TrakHoundTemp, tempPath);

                            // Make sure Temp directory exists
                            FileLocations.CreateTempDirectory();

                            img.Save(tempPath);
                            img.Dispose();

                            var fileData = new HTTP.FileContentData("uploadImage", tempPath, contentType);

                            var fileInfos = Files.Upload(currentUser, fileData);
                            if (fileInfos != null && fileInfos.Length > 0)
                            {
                                fileId = fileInfos[0].Id;
                            }
                        }
                    }
                    catch (Exception ex) { logger.Error(ex); }
                }
                else
                {
                    string filename = Path.ChangeExtension(Guid.NewGuid().ToString(), ".image");

                    string destinationPath = Path.Combine(FileLocations.Storage, filename);

                    FileLocations.CreateStorageDirectory();

                    File.Copy(info.FileId, destinationPath);

                    fileId = filename;
                }

                Dispatcher.BeginInvoke(new Action <string>(UploadDeviceImage_GUI), System.Windows.Threading.DispatcherPriority.Background, new object[] { fileId });
            }
        }