0

First of all, I'm new to .net/C#

I want the user to pick an image through a picturebox and the image should upload directly to cloudinary.

public static Cloudinary cloudinary;

public const string CLOUD_NAME = "XXXXXX";
public const string API_KEY = "XXXXXXX";
public const string API_SECRET = "XXXXXX";

string imagePath = "";
static void cloudinaryAccount(string[] args)
{
    Account account = new Account(CLOUD_NAME, API_KEY, API_SECRET);
    cloudinary = new Cloudinary(account);
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "All files(*.*)|*.*";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        imagePath = dialog.FileName.ToString();
        pictureBox1.ImageLocation = imagePath;
    }
    uploadImage(imagePath); // pass the path of the image to the uploadImage() function.
}

public static void uploadImage(string imagePath)
{

    var UploadParams = new ImageUploadParams()
    {
        File = new FileDescription(imagePath)
    };


    var uploadResult = cloudinary.Upload(UploadParams);
}

But when it comes to the cloudinary.Upload(UploadParams) it throws

this error

So it's probably because it cloudinary is null. So if I move

Account account = new Account(CLOUD_NAME, API_KEY, API_SECRET);
cloudinary = new Cloudinary(account);

into the uploadImage(string imagePath) function, I get the following error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.'

I followed the steps here and fixed it, it's working now.

Jelmer Overeem
  • 359
  • 1
  • 10

1 Answers1

0

Ensure that the following line is run before the call to upload:

cloudinary = new Cloudinary(account);
Andrew Arthur
  • 1,563
  • 2
  • 11
  • 17
  • I did, after that I get this error: _System.IO.FileNotFoundException: 'Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.'_ – Jelmer Overeem Aug 19 '20 at 12:22