0% found this document useful (0 votes)
130 views

Saving Image File To Specific Folder Within VB - Net Project..

Saving image file to specific folder within VB.net project.._

Uploaded by

wijatmikopuya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Saving Image File To Specific Folder Within VB - Net Project..

Saving image file to specific folder within VB.net project.._

Uploaded by

wijatmikopuya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

This site uses cookies for analytics, personalized content and ads.

By continuing to browse this site, you agree to this use. Learn more

Developer Network Sign in Subscriber portal Get tools

Downloads Programs Community Documentation

Ask a question Search related threads Search forum questions

Quick access

Answered by: Saving image file to specific folder within VB.net project...

4,015 Visual Studio Languages , .NET Framework > Visual Basic


Points
Top 5%
Question

siva.k     I am a newbie to development and am stuck on a project I am working on. I have a picturebox that the
Joined Jun 2008 user populates via an OpenFileDialog. I've done this successfully. The next step is for the user to choose to
siva.k's threads add the picture to a collection. When the user clicks the add button, I would like for the picture to be saved in
1 3 10 Show activity a folder called "photos" located at the same location of the application executable file. I am able to save the
0 picture, but do not know how to point the picture to the folder I want it saved in. I want the picture to be
Sign saved there automatically, so I am not using a SaveFileDialog. 
in to
vote     I tried using the following line, but do not know how to make this save the file to the location I want to
save it in.
        picSelectedPic.Image.Save(strFileName & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Any suggestions? The block of code is as follows:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click


Dim strFilePath As String = Application.ExecutablePath & "\photos\"   'Contains the path to save the picture...
Dim strFileName As String

    'Manipulates OpenFileDialog.filename so that variable 'strFileName' contains only the name of the picture...
t Fil N fdPi Fil N
strFileName = ofdPic.FileName
strFileName = strFileName.Substring(strFileName.LastIndexOf("\"))
strFileName = strFileName.Substring(1)
strFileName = strFileName.Remove(strFileName.IndexOf("."), 4)

    'Saves file to the same location as the application executable ---- Should go into 'photos' folder at that
location...
picSelectedPic.Image.Save(strFileName & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

    'Calls function to populate listbox with all the image files in 'photos' folder...
popImageList()

End Sub

Thanks, in advanced, for any assistance...

jason

Edited by MU_Jason Wednesday, July 2, 2008 4:01 AM Code entered...

Wednesday, July 2, 2008 4:00 AM

Reply | Quote
MU_Jason 40 Points

Answers
 Try this code. The executablePath will also contain the executable file name.

Dim strFilePath As String = Application.ExecutablePath


strFilePathstrFilePath = strFilePath.Substring(0, strFilePath.LastIndexOf("\")) & "\photos\"

1 PictureBox1.Image.Save(strFilePath & "A.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Sign
in to
vote

Marked as answer by Martin Xie - MSFT Monday, July 7, 2008 10:56 AM

Wednesday, July 2, 2008 4:38 AM

Reply | Quote
siva.k 4,015 Points

In fact you can use Application.StartupPath to avoid using Substring, this goes something like this:

Dim strBasePath As String


Dim strFilePath As String
Dim strFileName As String
0 strFileName = "A.jpg"
strBasePath = Application.StartupPath & "\Photos"
Sign ' >> Check if Folder Exists
in to If Directory.Exists(strBasePath) = False Then
vote Call Directory.CreateDirectory(strBasePath)
End If
' >> Save Picture
Call PictureBox1.Image.Save(strBasePath & "\" & strFileName, System.Drawing.Imaging.ImageFormat.Jpeg

Regards,

Shobhit Deep
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Proposed as answer by Shobhit Deep Wednesday, July 2, 2008 6:46 AM


Marked as answer by Martin Xie - MSFT Monday, July 7, 2008 10:55 AM

Wednesday, July 2, 2008 6:43 AM

Reply | Quote
Shobhit Deep 170 Points
py | Q
Shobhit Deep 170 Points

All replies
 Try this code. The executablePath will also contain the executable file name.

Dim strFilePath As String = Application.ExecutablePath


strFilePathstrFilePath = strFilePath.Substring(0, strFilePath.LastIndexOf("\")) & "\photos\"

1 PictureBox1.Image.Save(strFilePath & "A.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Sign
in to
vote

Marked as answer by Martin Xie - MSFT Monday, July 7, 2008 10:56 AM

Wednesday, July 2, 2008 4:38 AM

Reply | Quote
siva.k 4,015 Points

In fact you can use Application.StartupPath to avoid using Substring, this goes something like this:

Dim strBasePath As String


Dim strFilePath As String
Dim strFileName As String
0 strFileName = "A.jpg"
strBasePath = Application.StartupPath & "\Photos"
Sign ' >> Check if Folder Exists
in to If Directory.Exists(strBasePath) = False Then
vote Call Directory.CreateDirectory(strBasePath)
End If
' >> Save Picture
Call PictureBox1.Image.Save(strBasePath & "\" & strFileName, System.Drawing.Imaging.ImageFormat.Jpeg

Regards,

Shobhit Deep
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Proposed as answer by Shobhit Deep Wednesday, July 2, 2008 6:46 AM


Marked as answer by Martin Xie - MSFT Monday, July 7, 2008 10:55 AM

Wednesday, July 2, 2008 6:43 AM

Reply | Quote
Shobhit Deep 170 Points
py | Q
Shobhit Deep 170 Points

Sorry for the delayed response. Both of the suggestions were extremely helpful. I am able to use them
both in different parts of the app I'm working on. The 'application.executablepath' helped me find the files
and folders in the project and the 'application.startup' helped me to save them to where I needed to. My
extreme thanks to the both of you.
0
Sign jason
in to
vote
Tuesday, July 22, 2008 4:31 AM

Reply | Quote
MU_Jason 40 Points

Help us improve MSDN. Make a suggestion

Dev centers Learning resources Community Support


Microsoft Virtual Academy Forums Self support
Windows
Channel 9 Blogs

Office MSDN Magazine Codeplex

Visual Studio
Programs
Microsoft Azure BizSpark (for startups)

Microsoft Imagine (for students)


More...

United States (English) Newsletter Privacy & cookies Terms of use Trademarks © 2019 Microsoft

You might also like