No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type, VB.NET

 ̄綄美尐妖づ 提交于 2019-12-19 10:25:50

问题


I have a problem saving images from PictureBox1 to my SQL server database, I've done my research and found out that I have to convert my image to a byte array in order to do that but I don't know how to apply it in this code that I'm trying to edit. When I click on the save button I get this error: No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type.

I think it has something to do with inserting the image to the database so I'll show you code snippets that are related to it.

This is the class with its properties:

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

This is the function for insert:

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

This is the click event:

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

回答1:


In SQL SERVER database have datatype IMAGE. Keep the column datatype as IMAGE.

You can use the following sample code and pass Image to the below function which converts the image to byte to store in database.

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;
        }



回答2:


You just need to define an image path named "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!");
    }
}


来源:https://stackoverflow.com/questions/27310874/no-mapping-exists-from-object-type-system-drawing-bitmap-to-a-known-managed-prov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!