I'm create simple paint program in vb.net, when I'm trying to save the file, the program is freeze, and I can't do anything.
This is my code that I'm used
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.CreatePrompt = True
SaveFileDialog1.DefaultExt = "jpg"
SaveFileDialog1.Filter = "File Images (*.jpg;*.jpeg;) | *.jpg;*.jpeg; |PNG Images | *.png |GIF Images | *.GIF"
SaveFileDialog1.InitialDirectory = "F:"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
FrmCanvas.PictureBox1.Image.Save(SaveFileDialog1.FileName)
End If
End Sub
Did I'm missed something on my code? I'm sorry, I'm new at vb.net
You have missed the file format of your Image
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Jpeg)
To save image on users selected format based on filter is something like this
If SaveFileDialog1.FileName <> "" Then
' Saves the Image in the appropriate ImageFormat based upon the
' file type selected in the dialog box.
' NOTE that the FilterIndex property is one-based.
Select Case SaveFileDialog1.FilterIndex
Case 1
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Jpeg)
Case 2
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Bmp)
Case 3
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Gif)
End Select
End If
Hope that helps you get the idea.
Please visit this for more info.
Try This.
Dim SaveImage As New Bitmap(PictureBox1.Image)
SaveImage.Save(SaveImagePath + SaveImageName, Imaging.ImageFormat.Jpeg)
SaveImage.Dispose()
Related
Jan 8. 2023:
The AppendAllText can append into existing file and can create a file if no file exist.
the WriteAllText can Write into new created file and overwrite the existing file.
I'm trying to find another alltext for what I want to happen.
What I want to do is to save my textboxcontent.text into txt file.
I want to save 3 different content that will be displayed into my textboxcontent.text
And I only have one button.
That one button will open savefiledialog but with the code I have, I can only do 2 things, Write and Append.
Now, This is what suppose to happen.
*If I save the textcontent.text to an existing txt file, it will prompt message box "Do you want to overwrite this file?" And even if I click Yes, it will not allow to.
I must be able to create new txt file since I was not able to overwrite the file.
The reason is because I don't want to delete or overwrite the existing file with important information saved in it.
I hope somebody can help me.
This is the code I have.
```Imports System.io
Private lastSaveFileName As String = String.Empty
Private Function GetSaveFileName3(ByVal suggestedName As String) As String
Using sfd3 As New SaveFileDialog()
sfd3.Filter = "Text Files (*.txt) |*.txt"
sfd3.FileName = suggestedName
sfd3.OverwritePrompt = True
If DialogResult.OK Then
End If
If sfd3.ShowDialog() = DialogResult.OK Then
MessageBox.Show(
Me, "Your activity is not saved! This file have records from your last session, you cannot overwrite this file. Please create new file to save new records.",
"Save error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
)
Else
End If
Return String.Empty
End Using
End Function
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
lastSaveFileName = GetSaveFileName3(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.AppendAllText(lastSaveFileName, TextContent.Text)
End If
End Sub ' This code above includes IMPORTS.IO
Jan 9, 2023: Update
This is what I've done so far.
I tried to use the File.Exist but I don't know where to place it to make it run in the way I wanted.
Please see this code and help me fix it.
This code is running well in almost the way I want. I'm missing something.
Imports System.IO
Private lastSaveFileName As String = String.Empty
Private Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click
If Not File.Exists(lastSaveFileName) Then
lastSaveFileName = GetSaveFileName(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.WriteAllText(lastSaveFileName, txtdisplay1.Text)
End If
ElseIf File.Exists(lastSaveFileName) Then
lastSaveFileName = GetSaveFileName2(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.WriteAllText(lastSaveFileName, txtdisplay1.Text)
End If
End If
End Sub
Private Function GetSaveFileName2(ByVal suggestedName As String) As String
Using sfd As New SaveFileDialog()
sfd.Filter = "Text Files (*.txt) |*.txt"
sfd.FileName = suggestedName
sfd.OverwritePrompt = True
If sfd.ShowDialog() = DialogResult.OK Then
'If File.Exists(lastSaveFileName) Then
MessageBox.Show(
Me, "Your activity is not saved! This file have records from your last session, you cannot overwrite this file. Please create new file to save new records.",
"Save error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
)
End If
Return String.Empty
End Using
End Function
Private Function GetSaveFileName(ByVal suggestedName As String) As String
Using sfd As New SaveFileDialog()
sfd.Filter = "Text Files (*.txt) |*.txt"
sfd.FileName = suggestedName
sfd.OverwritePrompt = True
If sfd.ShowDialog() = DialogResult.OK Then
Return sfd.FileName
End If
Return String.Empty
End Using
End Function
With this code, I was able to save the textdisplay to a txtfile but it's like, it's bypassing the Elseif function.
Sometimes, poeple forgot to avoid important files and accidentally deleted it. This is what I'm preventing to happen.
I let the overwriteprompt true to let it ask the user if they want to replace. It accidentally click the yes, this will show message "This file have records from your last session, Please create new file to save new records." means that even the user want to replace it, the program will not allow it. I don't want to remove that scenario.
(Scenario 1)
What happen in this code is this, when I click the button, savefiledialog pop up and giving me choice how I want to save the textdisplay.
I can create new file or replace existing file.
First, I choose to replace, and a messagebox shows and saying, I can't replace the file.
Then I create new file, it lets me save the txt display normally.
(scenario 2)
That's what I want. The code runs that way at first, but if you click the button again, and try to create new file first, the message box will show saying I can't replace the file. then when I choose to replace, no message box shows and the file was replace. I lost the file.
That's where I need help. I only want the Scenario 1.
Please try on your own I you don't get what I mean.
I tried this and this code works the way I want.
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
If File.Exists(saveFileDialog1.FileName) Then
MessageBox.Show("A file with that name already exists. Please select a different file name or choose a different location to save the file.")
Else
File.WriteAllText(saveFileDialog1.FileName, Txtdisplay1.Text)
End If
End If
End Sub
Answered by: schoemr
I'm learning Visual Basic and I need to know how when I click "Save" on my GUI and the Save Dialogue box pops up, the current open file name is already being displayed in "File Name: xxxxxx"
This is what I have so far:
Imports System.IO
Public Class Form1
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim dr As DialogResult
dr = dlgSave.ShowDialog()
If dr = Windows.Forms.DialogResult.OK Then
Dim writerVar As StreamWriter
writerVar = New StreamWriter(dlgSave.FileName, False)
writerVar.Write(txtEdit.Text)
writerVar.Close()
End If
End Sub
Cheers,
Alex
This should help you out:
' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button2.Click
' Set the properties on SaveFileDialog1 so the user is
' prompted to create the file if it doesn't exist
' or overwrite the file if it does exist.
SaveFileDialog1.CreatePrompt = True
SaveFileDialog1.OverwritePrompt = True
' Set the file name to myText.txt, set the type filter
' to text files, and set the initial directory to the
' MyDocuments folder.
SaveFileDialog1.FileName = "myText"
' DefaultExt is only used when "All files" is selected from
' the filter box and no extension is specified by the user.
SaveFileDialog1.DefaultExt = "txt"
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
SaveFileDialog1.InitialDirectory = _
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Source: https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filename(v=vs.110).aspx
I recommend you read more into the FileName property there in order to find what you're looking for :-) hopefully that solves your problem!
Edit: Changed code to VB. Sorry, should have read the question more clearly. Been doing too much C# today..
I have a drag and drop event to get images into a picture box. I need to get the image location to string to store on my database. I can get the location if I use a OpenFileDialog, but if I use a drag and drop, I can't seem to get it. Here is my code:
Private Sub picCategoryImage_DragEnter(sender As Object, e As DragEventArgs) Handles picCategoryImage.DragEnter
'Procedure to copy the dragged picture from the
'open window
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
'If the file explorer is open, copy the picture to the box
e.Effect = DragDropEffects.Copy
picCategoryImage.BorderStyle = BorderStyle.FixedSingle
TextBox1.Text = picCategoryImage.ImageLocation
Else
'otherwise, don't take action
e.Effect = DragDropEffects.None
btnDeleteImage.Visible = False
End If
End Sub
Private Sub picCategoryImage_DragDrop(sender As Object, e As DragEventArgs) Handles picCategoryImage.DragDrop
'Procedure to select the pictue and drag to picturebox
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
btnDeleteImage.Visible = True
picbox.BorderStyle = BorderStyle.None
picCategoryImage.BringToFront()
btnDeleteImage.BringToFront()
Catch ex As Exception
MessageBox.Show("Image did not load")
End Try
End If
End Sub
As suggested by Plutonix, you're already using the file path so you've already got it. That's how Image.FromFile is able to create an Image from a file. If you mean that you need to be able to get the path later then you have two main choices:
Do as you're doing and store the path in a member variable for later use.
Instead of calling Image.FromFile and setting the Image of the PictureBox, just call the Load method of the PictureBox. You can then get the path from the ImageLocation property.
I would actually suggest using option 2 regardless, because it has the added advantage of not locking the file the way your current code does.
I am very new to Visual Basic so please be gentle. :P
I am creating a small application, for basic learning purposes, which will allow a user of the application to update a profile of sorts. This includes, uploading a profile picture, which is then stored in /bin/Debug/Resource and then displayed on their profile in a PictureBox.
I am using the following code, which seems to do just that. However, when I close the application and run it again, the image is not displayed on either PictureBox but is still stored in the designated folder.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If (Not System.IO.Directory.Exists("Resource")) Then
System.IO.Directory.CreateDirectory("Resource")
End If
Dim OpenFileDialog1 As New OpenFileDialog
With OpenFileDialog1
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
.FilterIndex = 2
'
If .ShowDialog = DialogResult.OK Then
Dim FName() As String = OpenFileDialog1.FileName.Split("\\")
System.IO.File.Copy(OpenFileDialog1.FileName, "Resource\\" + FName(FName.Length - 1))
PictureBox1.Image = Image.FromFile(.FileName)
Profile.PictureBox1.Image = Image.FromFile(.FileName)
End If
End With
End Sub
Any help that you can provide is greatly appreciated.
Thank you.
This is what you need to do in order to display the picture.
This code will have to be added to the Load form method.
Dim StoredPath As String = "PathToImage"
IF File.Exists(StoredPath) Then
PictureBox1.Image = Image.FromFile(#StoredPath)
PictureBox1.Refresh()
End if
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\KVRequest.txt"
Dim aryText(4) As String
aryText(0) = "TextBox4.Text"
aryText(1) = "TextBox5.Text"
aryText(2) = "TextBox6.Text"
aryText(3) = "TextBox7.Text"
aryText(4) = "TextBox8.Text"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Close()
MsgBox("Text file created in your C drive, attach this file in an email to someone#gmail.com Please check that all of the details are correct before sending.")
End Sub
What I am trying to do is get the text from the text boxes (4 5 6 7 8) to write into a text file. The code I have creates the file, but does not write text into it, can anyone give me a tip on how to get this working?
Thanks!
Edit: Also while I am here, I was trying to get it so button_1.enabled was only true if all of the text boxes had been edited, but I could not think of a practical way to do this, if you could help me with this I would also be very grateful!
Given the code posted above, the reason nothing is being written to the file is because you're not telling it to write anything to the file. You would need to add something like this between the creation of your StreamWriter and where you close the close method on it:
objWriter.WriteLine(TextBox4.Text)
objWriter.WtiteLine(TextBox5.Text)
etc...
Also, the simplest option for only enabling the save button is to create a Control.TextChanged handler for each of your text boxes (or use the one Sub to do it for all of them by adding all their events to the one handler method) and have it do something similar to:
If TextBox4.Text <> "" And TextBox5.Text <> "" And TextBox6.Text <> "" Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If