Datei überschreiben

  • VB.NET

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von raist10.

    Datei überschreiben

    Hallo,

    Ich lasse ein Bild in einer PictureBox anzeigen jetzt soll das Programm aber die geladene Imgae überschreiben also die jpg das Problem ist wenn ich das Image auf Nothing setzte gibt VB die jpg Datei nicht frei.
    mach die ne temp datei in die du das bild copierst und anzeigen lässt
    löschen tust du dann das richtige danach machst du die temp datei wieder leer


    pseudocode :

    file.copy("altedate.endung","temp.datei")
    und löschst die "altedate.endung"
    Wie jetzt ich machs momentan so du hast so komisch geschrieben

    VB.NET-Quellcode

    1. PictureBox1.Image = Nothing
    2. Kill(Application.StartupPath & "\tmp.jpeg")
    3. WebClient1.DownloadFile(rss(ListBox1.SelectedIndex)(3), Application.StartupPath & "\tmp.jpeg")
    4. PictureBox1.Image = Image.FromFile(Application.StartupPath & "\tmp.jpeg")
    aber so gehts nicht
    also bei mir gehts

    hab es so gemacht


    8 bilder die 1.png... bis 8.png heißen im ordner der exe dann starten

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim i As Integer = 1
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. If PictureBox1.Image.Equals(PictureBox1.ErrorImage) = True Then
    5. MsgBox("fehlerbild ( starte nach ok wieder von vorne )")
    6. i = 1
    7. PictureBox1.ImageLocation = Application.StartupPath & "\" & i & ".png"
    8. Else
    9. i = i + 1
    10. PictureBox1.ImageLocation = Application.StartupPath & "\" & i & ".png"
    11. End If
    12. Label1.Text = PictureBox1.ImageLocation
    13. End Sub
    14. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    15. System.IO.File.Delete(Application.StartupPath & "\" & i & ".png")
    16. Label1.Text = PictureBox1.ImageLocation
    17. End Sub
    18. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19. Label1.Text = PictureBox1.ImageLocation
    20. Button1.Text = "nächste bild"
    21. Button2.Text = "löschen"
    22. PictureBox1.ImageLocation = Application.StartupPath & "\" & i & ".png"
    23. End Sub
    24. End Class



    edit : ein problem gibt es, das bild1 wird gelöscht und er bleibt in ner dauerschleife
    ich bastel da mal ein array das die bildpfade der "lebenden" bilder speichert und
    beim löschen sich neu baut

    meine form :
    Bilder
    • bild löschen.png

      10,47 kB, 528×542, 247 mal angesehen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Triple-Axe“ ()

    VB.NET-Quellcode

    1. If rss(ListBox1.SelectedIndex)(3) = "" Then
    2. PictureBox1.Image = Nothing
    3. Else
    4. PictureBox1.Image = Nothing
    5. If i > 0 Then
    6. Kill(Application.StartupPath & "\tmp" & i & ".jpeg")
    7. End If
    8. i = i + 1
    9. WebClient1.DownloadFile(rss(ListBox1.SelectedIndex)(3), Application.StartupPath & "\tmp" & i & ".jpeg")
    10. PictureBox1.Image = Image.FromFile(Application.StartupPath & "\tmp" & i & ".jpeg")
    11. End If

    Der kann einfach nicht die Datei löschen weil die von VB "beschlagnahmt" wird

    coolerj schrieb:


    Ich lasse ein Bild in einer PictureBox anzeigen jetzt soll das Programm aber die geladene Imgae überschreiben also die jpg das Problem ist wenn ich das Image auf Nothing setzte gibt VB die jpg Datei nicht frei.


    Juup, ist klar.

    Du musst den Inhalt der PictureBox erst mit Dispose() freigeben und danach die PictureBox auf Nothing setzen. Dann ist auch das Image auf der Festplatte freigegeben.

    Also so:

    VB.NET-Quellcode

    1. Me.PictureBox1.Dispose()
    2. Me.PictureBox1 = Nothing


    Gruß

    Rainer

    VB.NET-Quellcode

    1. Public Function CreatePictureBox()
    2. Dim pictureBox As PictureBox = New PictureBox()
    3. pictureBox.Name = "PictureBox1"
    4. pictureBox.Left = 600
    5. pictureBox.Top = 0
    6. pictureBox.Height = 192
    7. pictureBox.Width = 254
    8. Return pictureBox
    9. End Function

    VB.NET-Quellcode

    1. If rss(ListBox1.SelectedIndex)(3) = "" Then
    2. pictureBox.Image = Nothing
    3. Else
    4. pictureBox.Dispose()
    5. pictureBox = Nothing
    6. If i > 0 Then
    7. Kill(Application.StartupPath & "\tmp" & i & ".jpeg")
    8. End If
    9. i = i + 1
    10. WebClient1.DownloadFile(rss(ListBox1.SelectedIndex)(3), Application.StartupPath & "\tmp" & i & ".jpeg")
    11. pictureBox = CreatePictureBox()
    12. pictureBox.Image = Image.FromFile(Application.StartupPath & "\tmp" & i & ".jpeg")
    13. End If

    Auch so wird auf die Datei zugegriffen
    Sorry, hab gepennt.

    Dispose() war schon die absolut richtige Antwort, bloss auf welches Objekt sich das bezieht hatte ich jetzt auf die Schnelle verwechselt.

    Du musst nicht die PictureBox Disposen und auf Nothing setzen, sondern natürlich das Image in der PictureBox.

    Und das geht so:

    VB.NET-Quellcode

    1. Me.PictureBox1.Image.Dispose()
    2. Me.PictureBox1.Image = Nothing


    Damit fällt natürlich der Aufwand die PictureBox neu zu erstellen komplett weg.

    Gruß

    Rainer

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „raist10“ ()