本文介绍了参数 5:无法从“System.Drawing.Image"转换为“string" - 从主类调用 class1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我以前的线程 这里.我收到此错误.我不知道如何解决这个问题.我正在尝试在 Main 类中调用 class1.cs.如果有人能在这里帮助我,我将不胜感激.提前致谢...

I am working from my previous thread here. I am getting this error. I don't know how to solve this problem. I am trying to call class1.cs in Main class. Most appreciate if anyone can help me here. thanks in advance...

这是 class1.cs

this is class1.cs

public static OleDbConnection GetConnection()
{
    var myCon = new OleDbConnection();
    myCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
    Source=C:\...Database1.mdb";
    return myCon;
}

public static void Insert(string id, string agegroup, string gender, string photoa)
{

    var con = GetConnection();
    OleDbCommand cmd = new OleDbCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "INSERT INTO Table1 (ID, AgeGroup, Gender, photo )";
    cmd.Parameters.AddWithValue("@ID", id);
    cmd.Parameters.AddWithValue("@AgeGroup", agegroup);
    cmd.Parameters.AddWithValue("@Gender", gender);
    cmd.Parameters.AddWithValue("@photo", photoa);

    cmd.Connection = con;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

这是我的主类,我想在其中调用它.

this is my Main class where I would like to call it.

 private void btnInsert_Click(object sender, EventArgs e)
    {

        Class1.Insert(textBox1.Text, comboBox1.Text, comboBox2.Text, pictureBox1.Image); // error is pointing at picture1.Image
        if (pictureBox1.Image != null)
        {
            //using MemoryStream:
            ms = new MemoryStream();
            pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            byte[] photo_aray = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo_aray, 0, photo_aray.Length);
            cmd.Parameters.AddWithValue("@photo", photo_aray);
        }

推荐答案

问题是你发送 Image 但你的方法接受字符串类型

problem is you sending Image but your method accepting string type

public static void Insert(string id, string agegroup, string gender, System.Drawing.Image photoa)
{
   // code
    cmd.Parameters.AddWithValue("@photo", imageToByteArray(photoa));
   // code
}

public static byte[] imageToByteArray(System.Drawing.Image iImage)
{
    MemoryStream mMemoryStream = new MemoryStream();
    iImage.Save(mMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
    return mMemoryStream.ToArray();
}

这篇关于参数 5:无法从“System.Drawing.Image"转换为“string" - 从主类调用 class1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:02