JayBeeOH

Image Save Demo

Oct 29th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.70 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Image Save Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   29 OCT 2016
  16. '
  17. ' Description:    Quick demo of how to use the SaveFileDialog to save an image.
  18. '
  19. '                 Documentation at PasteBin.com at: http://pastebin.com/6R45Ea9b
  20. '
  21. '-------------------------------------------------------------------------------------------
  22.  
  23. Public Class MainForm
  24.  
  25.     Private sfd As New SaveFileDialog
  26.  
  27.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  28.         Handles MyBase.Load
  29.  
  30.         'Initialize SaveFileDialog properties
  31.         With sfd
  32.             .Filter = "PNG (*.png)|*.png|JPG (*.jpg)|*.jpg|BMP (*.bmp)|*.BMP"
  33.             .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
  34.         End With
  35.  
  36.         MyPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  37.     End Sub
  38.  
  39.     Private Sub SaveButton_Click(sender As Object, e As EventArgs) _
  40.         Handles SaveButton.Click
  41.  
  42.         If String.IsNullOrWhiteSpace(FilenameTextBox.Text) Then
  43.             sfd.FileName = "Image"
  44.         Else
  45.             sfd.FileName = FilenameTextBox.Text
  46.         End If
  47.  
  48.         If sfd.ShowDialog = DialogResult.OK Then
  49.             SaveImageToFile(sfd.FileName)
  50.             sfd.InitialDirectory = System.IO.Path.GetDirectoryName(sfd.FileName)
  51.         End If
  52.     End Sub
  53.  
  54.     Private Sub SaveImageToFile(pathFile As String)
  55.  
  56.         Try
  57.             MyPictureBox.Image.Save(pathFile)
  58.             MessageBox.Show("Image saved successfully.",
  59.                             My.Application.Info.Title,
  60.                             MessageBoxButtons.OK,
  61.                             MessageBoxIcon.Information)
  62.         Catch ex As Exception
  63.             MessageBox.Show(ex.GetType.ToString & Environment.NewLine & Environment.NewLine & ex.Message,
  64.                             "Error",
  65.                             MessageBoxButtons.OK,
  66.                             MessageBoxIcon.Error)
  67.         End Try
  68.     End Sub
  69. End Class
Advertisement
Add Comment
Please, Sign In to add comment