首页 > 解决方案 > C# .net windows 窗体;通过picturebox上传带有cloudinary的图片

问题描述

首先,我是 .net/C# 的新手

我希望用户通过图片框选择图像,图像应直接上传到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);
}

但是当涉及到cloudinary.Upload(UploadParams)它时,它会抛出

这个错误

所以这可能是因为它cloudinary是空的。所以如果我搬家

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

进入uploadImage(string imagePath)函数,我收到以下错误:

System.IO.FileNotFoundException:'无法加载文件或程序集'Newtonsoft.Json,Version=12.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed'或其依赖项之一。该系统找不到指定的文件。'

我按照此处的步骤进行了修复,现在可以正常工作了。

标签: c#.netwinformspictureboxcloudinary

解决方案


确保在调用之前运行以下行upload

cloudinary = new Cloudinary(account);

推荐阅读