[RESOLVED] Saving image from the picturebox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Saving image from the picturebox

  1. #1
    Join Date
    Jan 2006
    Location
    California, USA
    Posts
    69

    [RESOLVED] Saving image from the picturebox

    Hello all,
    i have a picturebox control where the user can draw something. There is a save button which should save the image drawn in the picturebox. This is the code for the save button. I am using common dialog box and using save dialog box available from it. Also, i have used the SavePicture method just like Loadpicture method. But i am getting runtime error...

    Private Sub save_Click()
    Dim FileName As String

    cdlg.DefaultExt = "jpg"
    cdlg.FileName = "untitled.jpg"
    cdlg.Filter = "JPEG files (*.jpg)|*.JPG"
    cdlg.InitDir = App.Path
    cdlg.DialogTitle = "File save"
    cdlg.ShowSave
    FileName = cdlg.FileName
    SavePicture(picCanvas.Picture, FileName) = picCanvas.Picture




    End Sub

  2. #2
    Join Date
    Aug 2007
    Posts
    445

    Re: Saving image from the picturebox

    change this line
    Code:
    SavePicture picCanvas.Picture, FileName

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  3. #3
    Join Date
    Aug 2007
    Location
    Illinois
    Posts
    164

    Re: Saving image from the picturebox

    Quote Originally Posted by chunks
    change this line
    Code:
    SavePicture picCanvas.Picture, FileName
    Actually, since users are drawing on it, you should save the .Image not the .Picture. The contents will be saved as BMP.
    Code:
    SavePicture picCanvas.Image, FileName
    rani_techie,
    VB does not save anything in JPG format. If you want JPG format, you will need to use GDI+ or a DLL that will convert the image to JPG.
    Insomnia is a simple byproduct of "it can't be done"

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Saving image from the picturebox

    This is somewhat advanced, but hopefully this will help you in saving a picture in JPEG format.
    http://www.tek-tips.com/viewthread.cfm?qid=760856

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Saving image from the picturebox

    I posted this in a thread a few days ago, thought it was the same one.

    Just save and convert!

    Code:
    Option Explicit
    ' NOTE:  You must save the file in .BMP format before converting it.
    Private Declare Function BmpToJpeg Lib "Bmp2Jpeg.dll" (ByVal BmpFilename As String, ByVal JpegFilename As String, ByVal CompressQuality As Integer) As Integer
    
    Private Sub cmdLoad_Click()
    Dim filename As String
    On Error Resume Next
    'Set the filter to bitmap pictures
    dlg.Filter = "Bitmap Files | *.bmp"
    dlg.filename = ""
    dlg.ShowOpen
    'Get the file name
    filename = dlg.filename
    'Load the picture
    picMain.Picture = LoadPicture(filename)
    End Sub
    
    'For saving into jpg format you must have
    'the Bmp2Jpeg.dll file in your windows folder
    
    Private Sub cmdSave_Click()
    Dim filename As String
    On Error Resume Next
    'Set the filter to JPEG files
    dlg.Filter = "JPEG files | *.jpg"
    dlg.filename = ""
    dlg.ShowOpen
    'Get the file name without the ".jpg"
    filename = Left(dlg.filename, Len(dlg.filename) - 4)
    'Saving the file as Bitmap
    SavePicture picMain.Picture, filename & ".bmp"
    'Change the bitmap file to the jpg file with the Bmp2Jpg.dll
    '100 is the Compress Quality
    BmpToJpeg filename & ".bmp", filename & ".jpg", 100
    'Deleting the bitmap file
    Kill filename & ".bmp"
    End Sub
    I've included the .dll so you don't have to hunt it down.
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center