5 Replies - 484 Views - Last Post: 29 November 2012 - 11:42 AM Rate Topic: -----

#1 VBbeginner01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-November 12

[VB.NET 9]release backgroundimage for deleting

Posted 29 November 2012 - 09:18 AM

hi, i am new to the forum and pretty new at visual basic .net.

This is how the project goes:

At startup the form loads a image from a predefined source:
Me.BackgroundImage = Image.FromFile("C:\img1.bmp")


I've made a button that replaces the image with another image selected by the user:
        Dim newsbmp As New OpenFileDialog
        newsbmp.InitialDirectory = Environment.GetFolderPath(SpecialFolder.DesktopDirectory)
        If newsbmp.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                Me.BackgroundImage = Image.FromFile(newsbmp.FileName)
            Catch ex As Exception
                Me.BackgroundImage = Image.FromFile("C:\img1.bmp")
                MsgBox(ex, MsgBoxStyle.Critical)
            End Try
        End If


Now what i want to do is if the image loads correctly the old/predefined image "img1" to be replaced in the same directory with the same name by the picture selected by the user.
I can't delete the image since it say that the image is being used by the application.

Any help?
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: [VB.NET 9]release backgroundimage for deleting

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: [VB.NET 9]release backgroundimage for deleting

Posted 29 November 2012 - 10:32 AM

I'm having trouble understanding you. Please explain step by step what would you like to do with img1, if user's selected picture loads correctly.
Was This Post Helpful? 0
  • +
  • -

#3 VBbeginner01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-November 12

Re: [VB.NET 9]release backgroundimage for deleting

Posted 29 November 2012 - 10:42 AM

View Postlucky3, on 29 November 2012 - 10:32 AM, said:

I'm having trouble understanding you. Please explain step by step what would you like to do with img1, if user's selected picture loads correctly.


Basically i would want the new picture (the one that the user selected) to take place of the old one in "C:\" with the same name "img1.bmp".

And i thought of doing that by deleting the old picture and copying and renaming the new one.But the old one can't be deleted while the application is running.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: [VB.NET 9]release backgroundimage for deleting

Posted 29 November 2012 - 11:01 AM

You need to Dispose the background inmage

      Me.BackgroundImage.Dispose()


There is something to keep in mind though:

When you Dispose it and delete the image from disk, it is no longer available for reloading to the background, so you might want to consider copying it to an Image variable. Then if something goes wrong when copying the new file, you'll be able to save the old image from the image variable.

This post has been edited by lar3ry: 29 November 2012 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 VBbeginner01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-November 12

Re: [VB.NET 9]release backgroundimage for deleting

Posted 29 November 2012 - 11:20 AM

View Postlar3ry, on 29 November 2012 - 11:01 AM, said:

You need to Dispose the background inmage

      Me.BackgroundImage.Dispose()



I've tried that, i get the error:

Quote

System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"

Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: [VB.NET 9]release backgroundimage for deleting

Posted 29 November 2012 - 11:42 AM

View PostVBbeginner01, on 29 November 2012 - 12:20 PM, said:

I've tried that, i get the error:

Quote

System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"

That's odd. I pasted that line directly from the test program I used, which in turn was directly from the code you posted.

Did you Import System.Drawing?

Here, I'll give you the code I used to test. I have changed the location of the images, the type of image (to .jpg), but it should give you the idea.

Make a form with one button, default names for Form and Button. Place two images in the Debug directory of the project, and copy one of them to "img1.jpg"

Option Strict On
Option Explicit On
Imports System.IO
Imports System.Drawing

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim newsbmp As New OpenFileDialog
        newsbmp.InitialDirectory = CurDir()
        Dim tempimage As Image = Me.BackgroundImage
        If newsbmp.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                Me.BackgroundImage.Dispose()
                Me.BackgroundImage = Image.FromFile(newsbmp.FileName)
                File.Delete("img1.jpg")
                File.Copy(newsbmp.FileName, "img1.jpg")
            Catch ex As Exception
                tempimage.Save("img.jpg", Imaging.ImageFormat.Jpeg)
                Me.BackgroundImage = Image.FromFile("img1.jpg")
                MsgBox(ex, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.BackgroundImage = Image.FromFile("img1.jpg")
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1