Question cant save a . jpg file

Artsound

Member
Joined
Feb 1, 2011
Messages
10
Programming Experience
Beginner
First of all I am TOTAL newbie-please excuse my profound ignorance- I am attempting to set up a simple app to overwrite a jpg file with the user's choice of jpg.
Afile does get saved but it is saved as but1Imagejpg instead of but1Image.jpg
here is my code
VB.NET:
Public Class SavIm
    Dim image1 As Bitmap
    Private Sub SavIm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Retrieve the image.
        image1 = New Bitmap( _
            "C:\Users\pb\Desktop\vb.net experiments\but1Image.jpg", _
            True)
        PictureBox1.Image = image1
    End Sub

    Private Sub ChangeImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeImageToolStripMenuItem.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            image1 = New Bitmap(OpenFileDialog1.FileName)

            PictureBox1.Image = image1
            'image1.Save("C:\Users\pb\Desktop\vb.net experiments\but1Image" & "jpg", _
            'System.Drawing.Imaging.ImageFormat.Jpeg)


        End If
    End Sub

    
    Private Sub SaveCurrentImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCurrentImageToolStripMenuItem.Click
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then

            image1.Save("C:\Users\pb\Desktop\vb.net experiments\but1Image" & "jpg", _
            System.Drawing.Imaging.ImageFormat.Jpeg)
        End If
    End Sub
End Class
 
VB.NET:
"\but1Image" & "jpg"

It's naming the file exactly as you told it to. Why are you concatenating strings there anyways?
 
Take a good look at the code I highlighted. Take a look at the file name you're getting compared to the result you expect.
 
First of all thanx for your response and your patience---
so as you stated I may be concatinating for no good reason
and I am getting exactly the results the code is asking for in:
VB.NET:
"\but1Image" & "jpg"
your hints lead me to:
VB.NET:
"\but1Image.jpg"
yes ? or perhaps just:
VB.NET:
"\but1Image"
and then use the savefile dialog to add the extension by default extension or filter?
 
trying the preceding I get same result or this exception
{"A generic error occurred in GDI+."}
Any help on what I am doing wrong
 
You can't save to an image file you currently have loaded in memory while locked, you have to Dispose that first.
If you need to display the image while overwriting the file source it was loaded from you can for example use the constructor that takes an Image instance and immediately Dispose the instance that holds the file lock.
Bitmap Constructor (Image) (System.Drawing)
eg:
VB.NET:
Using fileLocked As New Bitmap(filepath)
    Me.PBox.Image = New Bitmap(fileLocked)
End Using
 
yes I see-I tried saving file with diff name and it saved so my exception comes from trying to write over a file in use--I will try you suggestion
 
it works thanx now i will try and do this with a button rather than a picturebox--any ideas?
VB.NET:
Public Class SavIm
    Dim image1 As Bitmap
    Private Sub SavIm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Retrieve the image.
        Using fileLocked As New Bitmap("C:\Users\pb\Desktop\vb.net experiments\but1Image.jpg")
            image1 = New Bitmap(fileLocked)
        End Using

        PictureBox1.Image = image1
    End Sub

    Private Sub ChangeImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeImageToolStripMenuItem.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            image1 = New Bitmap(OpenFileDialog1.FileName)

            PictureBox1.Image = image1

        End If
    End Sub


    Private Sub SaveCurrentImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCurrentImageToolStripMenuItem.Click
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then

            image1.Save("C:\Users\pb\Desktop\vb.net experiments\but1Image.jpg",
            System.Drawing.Imaging.ImageFormat.Jpeg)
        End If
    End Sub
End Class
 
Back
Top