从对象类型System.Drawing.Bitmap到已知的托管提供程序本机类型VB.NET不存在映射

问题描述 投票:2回答:2

我有问题从PictureBox1保存图像到我的SQL服务器数据库,我做了我的研究,发现我必须将我的图像转换为字节数组,以便这样做,但我不知道如何应用它这个代码我正在尝试编辑。当我点击保存按钮时,我收到此错误:No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type

我认为这与将图像插入数据库有关,因此我将向您展示与其相关的代码片段。

这是具有其属性的类:

Friend Class PersonFile
    Private _NewID As String

 Private _PersonID As String
    Friend Property PersonID() As String
        Get
            Return _PersonID
        End Get
        Set(ByVal Value As String)
            _PersonID = Value
        End Set
    End Property
 Private _Photo As Image
    Friend Property Photo() As Image
        Get
            Return _Photo
        End Get
        Set(ByVal Value As Image)
            _Photo = Value
        End Set
    End Property

这是插入的功能:

Friend Class PersonFileDB
 Friend Function DXInsertFile(ByVal cItem As PersonFile) As PersonFile
        Dim cReturn As New PersonFile

 Using oleCON As New SqlConnection(AppVariables.GConnectionString)
            oleCON.Open()

 Dim n1 As String = ""
            n1 = CreateNewID()
            cItem.PersonID = n1

            Dim xSQL As New StringBuilder
            xSQL.AppendLine(" INSERT INTO PersonData ")
            xSQL.AppendLine("( ")
            xSQL.AppendLine(" PersonID, ")
            xSQL.AppendLine(" Photo, ")
            ''Other code...
            xSQL.AppendLine("VALUES ( ")
            xSQL.AppendLine(" @PersonID, ")
            xSQL.AppendLine(" @Photo, ")
            ''Other code...

            Dim oleComm As New SqlCommand(xSQL.ToString, oleCON)
            With oleComm.Parameters
                .AddWithValue(" @PersonID, ", cItem.PersonID)
                .AddWithValue("@Photo", cItem.Photo)
            ''Other code...
            End With

            Dim n As Integer
            n = oleComm.ExecuteNonQuery()
            If n <> 0 Then
                cItem.Updated = True
                SaveNewID(CInt(cItem.PersonID))
                cReturn = cItem
            End If
 End Using
 Return cReturn
End Function

这是点击事件:

Private CurrPerson As New PersonFile

Private Sub cmdPersonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPersonSave.Click

        CurrPerson.Photo = PictureBox1.Image
        ''Other code...
  Dim cdb As New PersonFileDB

        Select Case cmdPersonSave.Text
            Case "Add"
                UIClear()
                UISetControls(False)
                cmdPersonSave.Text = "Save"
                cmdPersonUpdate.Text = "Cancel"
                cmdPersonDelete.Enabled = False
                cmdSearch.Enabled = False
            Case "Save"
                If EditMode Then
                    CurrPerson = cdb.DXUpdateFile(CurrPerson)
                    EditMode = False
                Else
                    CurrPerson = cdb.DXInsertFile(CurrPerson)
                End If
                If CurrPerson.Updated Then
                    BindGrid(CurrPerson.PersonID)
                    UISetControls(True)
                End If
                cmdPersonSave.Text = "Add"
                cmdPersonUpdate.Text = "Edit"
                cmdPersonDelete.Enabled = True
                cmdSearch.Enabled = True
                UISetControls(True)
        End Select
        End Sub
sql-server database vb.net bitmap picturebox
2个回答
1
投票

在SQL SERVER数据库中有数据类型IMAGE。将列数据类型保持为IMAGE。

您可以使用以下示例代码并将Image传递给下面的函数,该函数将图像转换为字节以存储在数据库中。

public static byte[] ImageToByte2(Image img)
        {
            byte[] byteArray = new byte[0];
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                stream.Close();

                byteArray = stream.ToArray();
            }
            return byteArray;
        }

0
投票

您只需要定义名为“imgpath”的图像路径

//upload image

private void button1_Click_1(object sender, EventArgs e)
{
    OpenFileDialog opf = new OpenFileDialog();

    opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
    if (opf.ShowDialog() == DialogResult.OK)
    {
        imgpath.Text = opf.FileName;
        pictureBox3.Image = Image.FromFile(opf.FileName);
    }
}
// ADD NEW/INSERT IMAGE TO DB
private void button5_Click(object sender, EventArgs e)
{
    string filepath = imgpath.Text;
    string filename = Path.GetFileName(imgpath.Text);
    FileStream fs = new FileStream(imgpath.Text, FileMode.Open, 
     FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    br.Close();
    fs.Close();

    if (imgpath.Text != "")
    {
        cmd = new SqlCommand("insert into tbl_Record(@StudentImage)", con);
        con.Open();
        MemoryStream ms = new MemoryStream();

       // must use toolbox to add picturebox
        pictureBox3.Image.Save(ms, pictureBox3.Image.RawFormat);
        byte[] img = ms.ToArray();
        cmd.Parameters.Add("@StudentImage", SqlDbType.Binary).Value = 
       bytes;
        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Succeed");
        DisplayData();
        ClearData();
    }
    else
    {
        MessageBox.Show("Fill Required Informations!");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.