none
Saving image file to a specific folder (VB.NET) RRS feed

  • Question

  • Hi, I want to save the selected image by the user from the openfiledialog and save it in the resources file of the vb.net project with a different image file name. But, for some reason, I am getting an error whenever I run my program even though the code is fine. These are the errors that show up when a picture is added: 

    1) 

    An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in Microsoft.VisualBasic.dll

    2) In the error list:

    Invalid Resx file. Could not find file 'E:\imagepath\imagepath\Resources\b2496308711688f7e44ed31a9eaa8ac2.jpg'. 

    This is my code: 

    Public Class Form1
        Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
            OpenFileDialog1.ShowDialog()
            Dim imageFile As String = OpenFileDialog1.FileName
            PictureBox1.ImageLocation = OpenFileDialog1.FileName
            Dim ImagePath = GetImagePath()
            Dim pic As Image
            Dim picfile As String
            pic = PictureBox1.Image
            If Not pic Is Nothing Then ' if there is an image then
                picfile = pic.Save(ImagePath & "A.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 'coverts image to jpeg
                MsgBox(picfile)
                BtnAdd.Focus()
            End If
        End Sub
        Function GetImagePath() 'gets filepath of the image so that it can be stored
            Dim ImagePath As String = Application.ExecutablePath 'Gets the filepath of the executable file 
            ImagePath = ImagePath.Substring(0, ImagePath.LastIndexOf("\")) & "\Resources\"
            Return ImagePath
        End Function
    
        Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles BtnLoad.Click
    
        End Sub
    End Class
    
    Please help me, I really need this for my project. 

    Saturday, March 31, 2018 8:39 PM

All replies

  • Invalid Resx file. Could not find file 'E:\imagepath\imagepath\Resources\b2496308711688f7e44ed31a9eaa8ac2.jpg'. 

    Your resource file is invalid.  Perhaps it got corrupted somehow, or maybe you moved your application location and neglected to copy the resource file to the new location.  You should delete your resource file if it exists and re-create your resources.

    Saturday, March 31, 2018 8:56 PM
  • Thanks, that worked. I didn't notice why that was happening. But I got it now. By any chance, do you know why I am getting the other error? 
    • Edited by Sumi Tasnim Saturday, March 31, 2018 9:03 PM
    Saturday, March 31, 2018 9:01 PM
  • Thanks, that worked. I didn't notice why that was happening. But I got it now. By any chance, do you know why I am getting the other error? 

    If the first error is still occurring after you  fixed the second error, then the problem is probably not in the area of the code you have posted.   The description of the functions provided by Interop Services is here:
    https://msdn.microsoft.com/en-us/library/system.runtime.interopservices(v=vs.110).aspx

    It relates to some form of interaction with a different process.  That might be part of your code or it might be occurring at some lower level inside the system.  You need to identify the portion of your code where it is occurring.

    Saturday, March 31, 2018 9:46 PM
  • Thanks, that worked. I didn't notice why that was happening. But I got it now. By any chance, do you know why I am getting the other error? 

    If the first error is still occurring after you  fixed the second error, then the problem is probably not in the area of the code you have posted.   The description of the functions provided by Interop Services is here:

    It relates to some form of interaction with a different process.  That might be part of your code or it might be occurring at some lower level inside the system.  You need to identify the portion of your code where it is occurring.

    But the Interop Service error happens on this line of code: 

       picfile = pic.Save(ImagePath & "A.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

    Everything before this line of code is fine, I constantly get an error on this line whenever I run the program and press the add button. It's really hard for me to figure out since I  am totally new to programming. It would be really useful if you could help me out a little. Thanks for supporting until now though. 


    Sunday, April 1, 2018 12:00 PM
  • Hi

    The Image.Save method doesn't produce a Return value, so presumably, the 'file =' is causing an issue.


    Regards Les, Livingston, Scotland

    Sunday, April 1, 2018 1:43 PM
  • Hi

    The Image.Save method doesn't produce a Return value, so presumably, the 'file =' is causing an issue.


    Regards Les, Livingston, Scotland

    Hi, thanks for replying, that's not the problem. I tried taking the "file =" out but I am still getting the same error.
    Sunday, April 1, 2018 3:30 PM
  • Place a MessageBox after this line "Dim ImagePath = GetImagePath()" and use it to display the ImagePath returned from the function to verify the integrity of the path as possibly you have multiple slant bars before Resources.

    La vida loca

    Sunday, April 1, 2018 4:21 PM
  • Hi

    The Image.Save method doesn't produce a Return value, so presumably, the 'file =' is causing an issue.


    Regards Les, Livingston, Scotland

    Hi, thanks for replying, that's not the problem. I tried taking the "file =" out but I am still getting the same error.

    Hi

    Try this :

     pic.Save(IO.Path.Combine(ImagePath, "A.jpg"), Imaging.ImageFormat.Jpeg)


    Regards Les, Livingston, Scotland

    Sunday, April 1, 2018 4:23 PM
  • Place a MessageBox after this line "Dim ImagePath = GetImagePath()" and use it to display the ImagePath returned from the function to verify the integrity of the path as possibly you have multiple slant bars before Resources.

    La vida loca

    Hi, I tried that and it seems the image path is fine since this is what I get: E:\imagepath\imagepath\Resources\. 
    Do you know by any chance what this e value is in the locals output box? 
    e {X = 22 Y = 18 Button = Left {1048576}} System.EventArgs {System.Windows.Forms.MouseEventArgs}
    This is always marked red, I think this is what's causing the problem. 
    Sunday, April 1, 2018 4:44 PM
  • Hi

    The Image.Save method doesn't produce a Return value, so presumably, the 'file =' is causing an issue.


    Regards Les, Livingston, Scotland

    Hi, thanks for replying, that's not the problem. I tried taking the "file =" out but I am still getting the same error.

    Hi

    Try this :

     pic.Save(IO.Path.Combine(ImagePath, "A.jpg"), Imaging.ImageFormat.Jpeg)


    Regards Les, Livingston, Scotland

    Hi, 
    I have tried that but I am still getting the same error. :( 
    Thank you for replying everyone. 
    Sunday, April 1, 2018 4:45 PM
  • Sumi,

    The name resources is in VB development used to embed for instance images in the assembly. 

    You use that in another way not standard VB. Any reason you do it that way or do you misunderstand resources. 

    You can find that by doing Project -> Project Properties -> Resources and paste the image in the pane which occurs. 

    To get it at runtime it is My.Resources.Yourimagename


    Success
    Cor

    Sunday, April 1, 2018 5:04 PM
  • Hi, I tried that and it seems the image path is fine since this is what I get: E:\imagepath\imagepath\Resources\. 

    Does the location 'E:\imagepath\imagepath\Resources\' exist in the file system? Do you have correct permissions for saving a file to that location?  I assume the full stop at the end is a typo.  If the file 'A.jpg' already exists at that location you will not be able to save there - choose a different path or a different file name.

    You should use Path.Combine for creating a file path, rather than string concatenation. See:
    https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx

    The locals expression that you see is the string representation of the e object. It is probably in red because the error does not allow you to continue

    Sunday, April 1, 2018 9:22 PM
  • Hello,

    >>The Image.Save method doesn't produce a Return value, so presumably, the 'file =' is causing an issue.


    Regards Les, Livingston, Scotland

    Hi, thanks for replying, that's not the problem. I tried taking the "file =" out but I am still getting the same error.

    You said that you have some issue at converting image code line, here is the image.save method:

    https://msdn.microsoft.com/en-us/library/9t4syfhh(v=vs.110).aspx

    Please first check the filename is correct format, secondly, as you see the image.save method don't return value.

    here is the example that converting image to other format.

     Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Image = Image.FromFile("D:\Image\switch-off-icon.png")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim image1 As Image
            image1 = PictureBox1.Image
            image1.Save("D:\Image\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, April 4, 2018 8:49 AM
    Moderator
  • Hello,

    You said that you have some issue at converting image code line, here is the image.save method:

    https://msdn.microsoft.com/en-us/library/9t4syfhh(v=vs.110).aspx

    Please first check the filename is correct format, secondly, as you see the image.save method don't return value.

    here is the example that converting image to other format.

     Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Image = Image.FromFile("D:\Image\switch-off-icon.png")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim image1 As Image
            image1 = PictureBox1.Image
            image1.Save("D:\Image\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Cherry and Stanly,

    Are you both trying to work together and with that getting a high score. 

    The code of you both does not solve this. 

    "Invalid Resx file. Could not find file 'E:\imagepath\imagepath\Resources\b2496308711688f7e44ed31a9eaa8ac2.jpg'."

    Don't show so much that you're both  just beginners in programming. 


    Success
    Cor



    Wednesday, April 4, 2018 8:56 AM