Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2012, 10:31 PM
itrs itrs is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 47 itrs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 12 m 39 sec
Reputation Power: 4
Load image, rename, save

This may seem like a simple question but I am new at VB so be gentle.

I am trying to do a simple open image file, display in picturebox, save as but when I do this using the code below, it produces errors

Object reference not set to an instance of an object. on my picturebox1.image.dispose()

What is the best way to read an image from a file, display it in a picture box and then save it with a different name?

Thanks

Code:
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Set the Filter.
        OpenFileDialog1.Filter = "Bitmap |*.bmp| JPG | *.jpg | GIF | *.gif | All Files|*.*"

        'Clear the file name
        OpenFileDialog1.FileName = ""

        'Show it
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            'Get the image name
            Dim img As String = OpenFileDialog1.FileName

            'Create a new Bitmap and display it
            Dim Image As Image
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)
            Image = PictureBox1.Image
            'PictureBox1.Image = Nothing
            'Image.Dispose()

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
        ' Displays a SaveFileDialog so the user can save the Image
        ' assigned to Button2.
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
        saveFileDialog1.Title = "Save an Image File"
        saveFileDialog1.ShowDialog()

        ' If the file name is not an empty string open it for saving.
        If saveFileDialog1.FileName <> "" Then
            PictureBox1.Image = Nothing
            PictureBox1.Image.Dispose()
            ' Saves the Image via a FileStream created by the OpenFile method.
            Dim fs As System.IO.FileStream = CType _
               (saveFileDialog1.OpenFile(), System.IO.FileStream)
            ' 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
                    Me.Button2.Image.Save(fs, _
                       System.Drawing.Imaging.ImageFormat.Jpeg)

                Case 2
                    Me.Button2.Image.Save(fs, _
                       System.Drawing.Imaging.ImageFormat.Bmp)

                Case 3
                    Me.Button2.Image.Save(fs, _
                       System.Drawing.Imaging.ImageFormat.Gif)
            End Select

            fs.Close()
        End If
    End Sub
End Class

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Load image, rename, save

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap