Bir görüntüyü bir picturebox'tan diğerine nasıl aktarabilirim? VB.NET & SQLNAME

0

Soru

Kimlik bilgilerini ve istenen profil resmini bir access veritabanında sakladığım bir oturum açma sistemi yapmaya çalışıyorum. istenen sonuç, oturum açma eşleştiğinde form 1'in bir köşeye yüklenen kaydedilmiş görüntü ile form 2'yi kapatıp açmasıdır.

ben bunu denedim

Form 1 kodu:

Imports System.Data.OleDb
Imports System.IO
Public Class Form1

    Private PictureFormat As String

    Private Sub FillPictureBoxFromFile()
        With OpenFileDialog1
            .Filter = "(*.jpg)|*.jpg|(*.png)|*.png"
            .RestoreDirectory = True
            .Title = "Select a file to open"
            If .ShowDialog = DialogResult.OK Then
                PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            End If
            PictureFormat = Path.GetExtension(OpenFileDialog1.FileName)
        End With
    End Sub

    Private cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\geral\source\repos\Login Fase 3 Final\Login Fase 3 Final\bin\Release\DBLoginPic.mdb"

    Private Sub saveimage()
        Dim arrimage() As Byte
        Using mstream As New System.IO.MemoryStream
            If PictureFormat.ToLower = ".png" Then
                PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Png)
            ElseIf PictureFormat.ToLower = ".jpg" Then
                PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
            arrimage = mstream.GetBuffer()
            Dim Filesize As Long
            Filesize = mstream.Length
        End Using
        Using con As New OleDbConnection(cnStr),
                cmd As New OleDbCommand("Insert into TBLoginPic (Imagen) Values (@Imagen)", con)
            With cmd
                .Parameters.Add("@Imagen", OleDbType.Binary).Value = arrimage
                '.Parameters.Add("@Nombre", OleDbType.VarChar).Value = TextBox1.Text
                con.Open()
                .ExecuteNonQuery()
            End With
        End Using
    End Sub

    Private Function GetDataByName(name As String) As DataTable
        Dim dt As New DataTable
        Using conn As New OleDb.OleDbConnection(cnStr),
                cmd As New OleDbCommand("Select * from TBLoginPic where Usuario= @Buscar", conn)
            cmd.Parameters.Add("@Buscar", OleDbType.VarChar).Value = TBusuario.Text
            conn.Open()
            Using reader = cmd.ExecuteReader
                dt.Load(reader)
            End Using
        End Using
        Return dt
    End Function


    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub

    Private Sub TBusuario_TextChanged(sender As Object, e As EventArgs) Handles TBusuario.TextChanged

    End Sub

    Private Sub TBcontraseña_TextChanged(sender As Object, e As EventArgs) Handles TBcontraseña.TextChanged

    End Sub

    Private Sub BtnLoguearse_Click(sender As Object, e As EventArgs) Handles BtnLoguearse.Click
        If Me.TBLoginPicTableAdapter.BuscarDatos(Me.DBLoginPicDataSet.TBLoginPic, TBusuario.Text, TBcontraseña.Text) Then

            Dim dt As DataTable
            Try
                dt = GetDataByName(TBusuario.Text)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Exit Sub
            End Try

            TBusuario.Text = dt(0)("Usuario").ToString
            Dim arrimage = DirectCast(dt(0)("Imagen"), Byte())
            Dim mstream As New System.IO.MemoryStream(arrimage)
            PictureBox1.Image = Image.FromStream(mstream)

            Form2.Show()
            _selectedImage = PictureBox1.Image
            Me.Close()
        Else
            MsgBox("Datos Erroneos")
        End If
    End Sub

    Private Sub BtnRegistrarse_Click(sender As Object, e As EventArgs) Handles BtnRegistrarse.Click


        Me.TBLoginPicBindingSource.AddNew()
        Me.TBLoginPicBindingSource.EndEdit()
        Me.TBLoginPicTableAdapter.Update(Me.DBLoginPicDataSet)


        Try
            saveimage()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try
        MsgBox("Image has been saved in the database")

    End Sub

    Private Sub BtnExaminar_Click(sender As Object, e As EventArgs) Handles BtnExaminar.Click
        FillPictureBoxFromFile()
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DBLoginPicDataSet.TBLoginPic' table. You can move, or remove it, as needed.
        Me.TBLoginPicTableAdapter.Fill(Me.DBLoginPicDataSet.TBLoginPic)



    End Sub

    Private Sub TBLoginPicBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TBLoginPicBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DBLoginPicDataSet)

    End Sub
End Class

Form 2 Kodu:

Module imagen
    Public _selectedImage As Image
    Public ReadOnly Property SelectedImage As Image
        Get
            Return _selectedImage
        End Get
    End Property
End Module

Public Class Form2



    Private Sub TBLoginPicBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TBLoginPicBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DBLoginPicDataSet)

    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DBLoginPicDataSet.TBLoginPic' table. You can move, or remove it, as needed.
        Me.TBLoginPicTableAdapter.Fill(Me.DBLoginPicDataSet.TBLoginPic)
        _selectedImage = PBPerfil.Image

    End Sub

    Public Sub PBPerfil_Click(sender As Object, e As EventArgs) Handles PBPerfil.Click

    End Sub
End Class

ve bu hatayı al

Sistem.InvalidCastException: 'Tür nesnesi atılamıyor' Sistemi.DBNull 'yazmak için' Sistem.Bayt[]'.'

Form 1 böyle görünüyor

Form 2 böyle görünüyor

Veritabanım böyle görünüyor (endişelenmeyin, burada herhangi bir kişisel bilgi yok))

Form 1'den 2'ye göndermeye çalıştığım yer burası.

Bu şekilde form 2'de almaya çalışıyorum

daha iyi ne yapabileceğime dair bir ipucu var mı?

database forms image picturebox
2021-11-24 05:29:42
1

En iyi cevabı

0

Boş değerlere bakmalısın. Görüntü sütununda veri yoksa, onu değiştirmeye çalıştığınızda hata verir. Veritabanında hiçbiri yoksa görüntülenecek varsayılan bir resim seçmenizi öneririm.

    If dt(0)("Usuario") IsNot Nothing OrElse Not IsDBNull(dt(0)("Usuario")) Then
        TBusuario.Text = dt(0)("Usuario").ToString
        Dim arrimage = DirectCast(dt(0)("Imagen"), Byte())
        Dim mstream As New System.IO.MemoryStream(arrimage)
        PictureBox1.Image = Image.FromStream(mstream)
    Else
        PictureBox1.Image = Image.FromFile("DefaultImage.png")
    End If
2021-11-24 08:18:30

böyle bir resmi nasıl ekleyebilirim? bunun gibi bir bağlantı dizesine benzer bir yol koymam gerekiyor mu: Provider=Microsoft.Jet.OLEDB.4.0; Veri Kaynağı=C: \ Users \ geral\source\repos \ Login Fase 3 Final\Login Fase 3 Final\bin\Release\DBLoginPic.mdb. yoksa oraya bir dosya yolu mu koyayım? veya başka bir yolu var mı?
THEwed123wet

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................