Results 1 to 11 of 11

Thread: Save picturebox as png

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2014
    Posts
    87

    Save picturebox as png

    example of saving picturebox as png no dll's please modules and classes only....

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: Save picturebox as png

    What have you done yourself, beside begging for complete solutions?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2014
    Posts
    87

    Re: Save picturebox as png

    ouch that felt a little hostile why such hostility my bad bro i meant no disrespect by the ? i do apolagize for whatever i said ......... ill post my code im using tho i dont take credit for code i didnt write but all my debuging has done no good all i can think of is the stdpicture is my error being as that when debugged on the savepictur line it say in the function declarations the picture = nothing hence why im sure its saving as nothing ......... but what i find strange is the filename = "" but on down it does use the specified called filename from call for the function weird i thought but the type of fine is there in the function declartions as .png or whatever type im trying to save and yes my wording may not be proper i am self tought i have tryed to adapt and learn but im ok with code just some stuff ive never done i dont know how and as a self tought trial error and help is the only way i learn kinda like through repetition the more times i do the functions calls declaration the more i remeber them and understand how they work ... but ne ways to my code im trying to use........this is my module
    Code:
    Option Explicit
    
    Private Type GUID
        Data1 As Long
        Data2 As Integer
        Data3 As Integer
        Data4(0 To 7) As Byte
    End Type
    Private Type GdiplusStartupInput
        GdiplusVersion As Long
        DebugEventCallback As Long
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
    Private Type EncoderParameter
        GUID As GUID
        NumberOfValues As Long
        type As Long
        Value As Long
    End Type
    Private Type EncoderParameters
        count As Long
        Parameter As EncoderParameter
    End Type
    
    Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
    Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
    Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hPal As Long, BITMAP As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
    Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal FileName As Long, clsidEncoder As GUID, encoderParams As Any) As Long
    Private Declare Function CLSIDFromString Lib "ole32" (ByVal Str As Long, id As GUID) As Long
    Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb As Long) As Long
    
    Public Sub SavePic(ByVal pict As StdPicture, ByVal FileName As String, PicType As String, _
        Optional ByVal Quality As Byte = 80, _
        Optional ByVal TIFF_ColorDepth As Long = 24, _
        Optional ByVal TIFF_Compression As Long = 6)
        Screen.MousePointer = vbHourglass
        Dim tSI As GdiplusStartupInput
        Dim lRes As Long
        Dim lGDIP As Long
        Dim lBitmap As Long
        Dim aEncParams() As Byte
        On Error GoTo ErrHandle:
        tSI.GdiplusVersion = 1
        lRes = GdiplusStartup(lGDIP, tSI)
        If lRes = 0 Then
            lRes = GdipCreateBitmapFromHBITMAP(pict.Handle, 0, lBitmap)
            If lRes = 0 Then
                Dim tJpgEncoder As GUID
                Dim tParams As EncoderParameters
                Select Case PicType
                Case ".jpg"
                    CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
                    tParams.count = 1
                    With tParams.Parameter
                        CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
                        .NumberOfValues = 1
                        .type = 4
                        .Value = VarPtr(Quality)
                    End With
                    ReDim aEncParams(1 To Len(tParams))
                    Call CopyMemory(aEncParams(1), tParams, Len(tParams))
                Case ".png"
                    CLSIDFromString StrPtr("{557CF406-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
                    ReDim aEncParams(1 To Len(tParams))
                Case ".gif"
                    CLSIDFromString StrPtr("{557CF402-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
                    ReDim aEncParams(1 To Len(tParams))
                Case ".tiff"
                    CLSIDFromString StrPtr("{557CF405-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
                    tParams.count = 2
                    ReDim aEncParams(1 To Len(tParams) + Len(tParams.Parameter))
                    With tParams.Parameter
                        .NumberOfValues = 1
                        .type = 4
                        CLSIDFromString StrPtr("{E09D739D-CCD4-44EE-8EBA-3FBF8BE4FC58}"), .GUID
                        .Value = VarPtr(TIFF_Compression)
                    End With
                    Call CopyMemory(aEncParams(1), tParams, Len(tParams))
                    With tParams.Parameter
                        .NumberOfValues = 1
                        .type = 4
                        CLSIDFromString StrPtr("{66087055-AD66-4C7C-9A18-38A2310B8337}"), .GUID
                        .Value = VarPtr(TIFF_ColorDepth)
                    End With
                    Call CopyMemory(aEncParams(Len(tParams) + 1), tParams.Parameter, Len(tParams.Parameter))
                Case ".bmp"
                    SavePicture pict, FileName
                    Screen.MousePointer = vbDefault
                    Exit Sub
                End Select
                lRes = GdipSaveImageToFile(lBitmap, StrPtr(FileName), tJpgEncoder, aEncParams(1))
                GdipDisposeImage lBitmap
            End If
            GdiplusShutdown lGDIP
        End If
        Screen.MousePointer = vbDefault
        Erase aEncParams
        Exit Sub
    ErrHandle:
        Screen.MousePointer = vbDefault
        MsgBox "Error" & vbCrLf & vbCrLf & "Error No. " & Err.Number & vbCrLf & " Error .Description:  " & Err.Description, vbInformation Or vbOKOnly
    End Sub
    this is how i call it which im sure is right .... cuz in the call the picture = 912808929 and the filname is ther and type is there just when i debug the function the filename doesnt show in its declarations it shows "" and the pict = nothing i use picture1 which is a picturebox perhaps i should have ref a wia.dll or somthing im not sure but seems like the picture1.image should be also 19808980 in this part as shown below and seems the filename would show but only the type shows pictype = ".png" there below
    Code:
    SavePic(ByVal pict As StdPicture, ByVal FileName As String, PicType As String, _
    this is how i call it
    Code:
    SavePic Picture1.Image, "C:\Test.png", ".png"

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save picturebox as png

    but what i find strange is the filename = "" but on down it does use the specified called filename from call for the function weird i thought but the type of fine is there in the function declartions as .png or whatever type im trying to save
    If you break on your function, on the "Public Sub ... " line, then hover over the parameter names, you should expect to see Nothing, "", etc for ByVal parameters. Once you press F8 to get to the next executable line, then the Nothing, "", etc should no longer be seen.

    Does that help for the next step in your troubleshooting?

    As far as what is or is not being saved, does c:\test.png contain data? I'm guessing it does and your problem may simply be that Picture1.AutoRedraw = False?

    And not intended to be hostile -- jeez Josh, sprinkle a bit more punctuation into those long posts
    Last edited by LaVolpe; Oct 18th, 2015 at 09:57 AM. Reason: typo
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Save picturebox as png

    Given your recent questions I feel bound to ask, what is in the picture that you are trying to save? What is it a picture of?
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  6. #6
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: Save picturebox as png

    Quote Originally Posted by josh230526 View Post
    ouch that felt a little hostile why such hostility my bad bro i meant no disrespect by the ? i do apolagize for whatever i said ......... ill post my code im using tho i dont take credit for code i didnt write but all my debuging has done no good all i can think of is the stdpicture is my error being as that when debugged on the savepictur line it say in the function declarations the picture = nothing hence why im sure its saving as nothing ......... but what i find strange is the filename = "" but on down it does use the specified called filename from call for the function weird i thought but the type of fine is there in the function declartions as .png or whatever type im trying to save and yes my wording may not be proper i am self tought i have tryed to adapt and learn but im ok with code just some stuff ive never done i dont know how and as a self tought trial error and help is the only way i learn kinda like through repetition the more times i do the functions calls declaration the more i remeber them and understand how they work ... but ne ways to my code im trying to use........this is my module
    Whilst reading this aloud to myself I passed out through lack of oxygen.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save picturebox as png

    Quote Originally Posted by ColinE66 View Post
    Whilst reading this aloud to myself I passed out through lack of oxygen.
    ROTFL - seriously. I must have opened this thread a dozen+ times before I think I figured out what was being asked.

    Sorry Josh, try to see it from our point of view.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Save picturebox as png

    "A picture is worth a thousand words"
    And we got both

  9. #9
    New Member
    Join Date
    Jul 2017
    Posts
    7

    Re: Save picturebox as png

    how to save picture in picturebox? I input pic1 on picturebox.1, and pic1 is processed to change its pixel and change to pic2 in picturebox2. Then i use commondialog to save pic2. i'd use this code, but id didn't work.
    Code:
    Private Sub save_Click()
    CommonDialog1.ShowSave
    If CommonDialog1.FileName <> "" Then
    Open CommonDialog1.FileName For Output As #1
    Write #1, outputgambar.Picture
    Close #1
    End If
    End Sub

  10. #10
    Lively Member
    Join Date
    Dec 2015
    Posts
    103

    Re: Save picturebox as png

    Quote Originally Posted by fdasmansyah View Post
    how to save picture in picturebox? I input pic1 on picturebox.1, and pic1 is processed to change its pixel and change to pic2 in picturebox2. Then i use commondialog to save pic2. i'd use this code, but id didn't work.
    Code:
    Private Sub save_Click()
    CommonDialog1.ShowSave
    If CommonDialog1.FileName <> "" Then
    Open CommonDialog1.FileName For Output As #1
    Write #1, outputgambar.Picture
    Close #1
    End If
    End Sub
    ....

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Save picturebox as png

    Kind of confused about the previous 2 posts. If there is a question, start your own thread as it appears those last two posts do not refer to the original thread owner's question.

    P.S. You can't save a picture that way. You may want to start your own thread on how to use VB's SavePicture function.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Click Here to Expand Forum to Full Width