void UploadProfileImage(EditUserInfo info, string path)
        {
            if (path != null)
            {
                // Crop and Resize image
                System.Drawing.Image img = ProcessImage(path);
                if (img != null)
                {
                    // Generate random file name for processed/temp image (to be saved in temp folder)
                    string newFilename = String_Functions.RandomString(20);

                    // Get file extension of original file
                    string ext = Path.GetExtension(path);

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

                    // Create new full path of temp file
                    string localPath = Path.Combine(FileLocations.TrakHoundTemp, newFilename);
                    //if (ext != null) localPath += "." + ext;
                    if (ext != null)
                    {
                        localPath = Path.ChangeExtension(localPath, ext);
                    }

                    // Save the processed image to the new temp path
                    img.Save(localPath);

                    // Create a temp UserConfiguration object to pass the current SessionToken to the Files.Upload() method
                    var userConfig = new UserConfiguration();
                    userConfig.SessionToken = info.SessionToken;

                    // Set the HTTP Content Type based on the type of image
                    string contentType = null;
                    if (ext == "jpg")
                    {
                        contentType = "image/jpeg";
                    }
                    else if (ext == "png")
                    {
                        contentType = "image/png";
                    }
                    else if (ext == "gif")
                    {
                        contentType = "image/gif";
                    }

                    var fileData = new HTTP.FileContentData("uploadimage", localPath, contentType);

                    // Upload File
                    var uploadInfos = TrakHound.API.Files.Upload(userConfig, fileData);
                    if (uploadInfos != null && uploadInfos.Length > 0)
                    {
                        string fileId = uploadInfos[0].Id;

                        info.ImageUrl = fileId;
                    }
                }
            }
        }
Exemplo n.º 2
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 });
            }
        }