Save Picturebox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pavan Arise

    Save Picturebox

    Dear all..
    I have a picturebox filled on a form. The picturebox has some
    graphics displayed on it.I was trying to save the picturebox, but
    continuesly failed to do so. I am clueless of why it is happening. I
    tried to save the image of the form & also the picturebox, but always,
    i see a blank file saved. Is there a way to save whatever is displayed
    on the picturebox, to a file??
    Thanks for any suggestions and help.

    Regards,
    Pavan
  • J French

    #2
    Re: Save Picturebox

    On 17 Jun 2004 21:45:25 -0700, pavankarise@yah oo.com (Pavan Arise)
    wrote:
    [color=blue]
    >Dear all..
    > I have a picturebox filled on a form. The picturebox has some
    >graphics displayed on it.I was trying to save the picturebox, but
    >continuesly failed to do so. I am clueless of why it is happening. I
    >tried to save the image of the form & also the picturebox, but always,
    >i see a blank file saved. Is there a way to save whatever is displayed
    >on the picturebox, to a file??
    > Thanks for any suggestions and help.[/color]

    Set the Picturebox's AutoRedraw = True

    SavePicture Picture1.Image, FileName

    Comment

    • Pavan Arise

      #3
      Re: Save Picturebox

      I am still not able to save the picturebox/ I am hereby attaching the
      code here.
      This is the code which I have in the sub:
      =============== ==
      In Declarations Section
      Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
      Long, ByVal hWndNewParent As Long) As Long
      Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long,
      ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight
      As Long, ByVal bRepaint As Long) As Long

      ' Following code in Sub/
      ------
      Dim Dum As New FrmRotSyncPlt

      Load FrmRotSyncPlt ' FrmRotSyncPlt is the form where the whole
      graphics is printed & also has the picturebox

      Call SetParent(FrmRo tSyncPlt.hwnd, Dum.hwnd)

      Call MoveWindow(FrmR otSyncPlt.hwnd, 0, 0, 800, 600, True)
      ' I alreday have the SetParent & MoveWindow declared in the
      declarations section (As previously given by Seth)
      Dum.Show
      SavePicture Dum.Image, "c:/trial.bmp"
      Debug.Print "done"

      End sub
      ============
      I also tried it with copying the whole form into a picturebox on Dum
      , as suggested by Seth above, but without any sucess. The saved
      trial.Bmp is always blank. Thanks for any suggestions & help.

      Comment

      • Randy Birch

        #4
        Re: Save Picturebox

        wait a second ...

        From what you post ... on one hand you Dim a variable 'Dum' as a form type,
        then proceed to not properly initialize it to a form. You then do APIs to
        FrmRotSyncPlt, setting parent to the non-existent form, then attempt to save
        a picture from this same non-existent (non-visible, non-referenced) form
        using the image property of a picturebox.

        What makes you think that following a SetParent call, that a picturebox
        contained on FrmRotSyncPlt will somehow become the picturebox on the other
        form?

        1) saving the Image property of a form only saves -- quite literally -- an
        image the form itself (no contained controls). If you were to draw onto the
        form canvas you would 'see' the saved form picture, but any controls on the
        form would not be visible.

        2) if you have a valid picture in a picturebox, execute:

        picture1.pictur e = picture1.image
        savepicture picture1.pictur e, "c:\temp.bm p"

        Once you 'setparent' a form all bets are off as to the success of any given
        activity. Using SetParent to reparent one form to another is at best a hack,
        and unsupported.

        Better explain things more clearly.

        --

        Randy Birch
        MVP Visual Basic

        Please respond only to the newsgroups so all can benefit.


        "Pavan Arise" <pavankarise@ya hoo.com> wrote in message
        news:ebda6084.0 406191835.3588b ea6@posting.goo gle.com...
        : I am still not able to save the picturebox/ I am hereby attaching the
        : code here.
        : This is the code which I have in the sub:
        : =============== ==
        : In Declarations Section
        : Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
        : Long, ByVal hWndNewParent As Long) As Long
        : Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long,
        : ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight
        : As Long, ByVal bRepaint As Long) As Long
        :
        : ' Following code in Sub/
        : ------
        : Dim Dum As New FrmRotSyncPlt
        :
        : Load FrmRotSyncPlt ' FrmRotSyncPlt is the form where the whole
        : graphics is printed & also has the picturebox
        :
        : Call SetParent(FrmRo tSyncPlt.hwnd, Dum.hwnd)
        :
        : Call MoveWindow(FrmR otSyncPlt.hwnd, 0, 0, 800, 600, True)
        : ' I alreday have the SetParent & MoveWindow declared in the
        : declarations section (As previously given by Seth)
        : Dum.Show
        : SavePicture Dum.Image, "c:/trial.bmp"
        : Debug.Print "done"
        :
        : End sub
        : ============
        : I also tried it with copying the whole form into a picturebox on Dum
        : , as suggested by Seth above, but without any sucess. The saved
        : trial.Bmp is always blank. Thanks for any suggestions & help.

        Comment

        • Pavan Arise

          #5
          Re: Save Picturebox

          Dear Randy Birch..thanks for the suggestion, I think I have messed up
          the code completely. What I was trying to do was to save a form(it has
          some plot drawn on it) along with a picturebox on it. As you said,
          when I tried with Picturesave, I am not able to save the controls. My
          try with the setparent is not yeilding any results either. I am now
          wondering(and trying) if Bitblt would serve the purpose of saving the
          form along with its control content.Can you please help me.
          Thanks in advance for any suggestions/

          Comment

          • Randy Birch

            #6
            Re: Save Picturebox

            If you use this ... http://vbnet.mvps.org/code/bitmap/printscreenole.htm ...
            and in the GetOLEScreenSna pshot routine substitute:

            'CaptureWindow
            WidthSrc = me.Width \ Screen.TwipsPer PixelX
            HeightSrc = me.Height \ Screen.TwipsPer PixelY

            'Get a handle to the desktop window and get the proper device context
            hWndSrc = Me.hwnd

            .... for ...

            'CaptureWindow
            WidthSrc = Screen.Width \ Screen.TwipsPer PixelX
            HeightSrc = Screen.Height \ Screen.TwipsPer PixelY

            'Get a handle to the desktop window and get the proper device context
            hWndSrc = GetDesktopWindo w()


            .... you'll get a pix of the form in Picture1 - including its controls and
            control contents - which you can then save using the code I provided
            earlier:

            picture1.pictur e = picture1.image
            savepicture picture1.pictur e, "c:\temp.bm p"

            --

            Randy Birch
            MVP Visual Basic

            Please respond only to the newsgroups so all can benefit.


            "Pavan Arise" <pavankarise@ya hoo.com> wrote in message
            news:ebda6084.0 406201607.3a7fc 8a4@posting.goo gle.com...
            : Dear Randy Birch..thanks for the suggestion, I think I have messed up
            : the code completely. What I was trying to do was to save a form(it has
            : some plot drawn on it) along with a picturebox on it. As you said,
            : when I tried with Picturesave, I am not able to save the controls. My
            : try with the setparent is not yeilding any results either. I am now
            : wondering(and trying) if Bitblt would serve the purpose of saving the
            : form along with its control content.Can you please help me.
            : Thanks in advance for any suggestions/

            Comment

            • Pavan Arise

              #7
              Re: Save Picturebox

              Thank you Randy Birch for the invaluable piece of code. I have the
              program working perfectly as I wanted. But there is a small problem
              here. I was trying to save just the form content only. By the way it
              is now, the title bar (Wher the caption is written) is also getting
              saved. I tried to change the form borderstyle setting to 0 (None) &
              refreshing the form before calling the GetOLEScreenSna pshot sub, but
              for some reason, the form border isn't changing and the form along
              with the titlebar is getting saved. There were some other buttons on
              the form which were not saved when I had them as visible =false during
              runtime. Is it that the borderstyle doesn't change during runtime, or
              are there any other changes to be made for having the titlebar not to
              be saved with the form. Thank you again and any suggestions are
              appreciated.

              Comment

              • Randy Birch

                #8
                Re: Save Picturebox

                Try this slightly tweaked routine. The values set for the voffset and
                hoffset values work on my system using small fonts. You may need to tweak
                the values to correctly clip for large font systems. Basically this uses the
                form's scaleheight and scalewidth rather than its width/height to size the
                bitmap, then offsets the bitblt'ed image to subtract the borders and
                titlebar.

                Private Function GetOLEScreenSna pshot() As Picture

                Dim hWndSrc As Long
                Dim hDCSrc As Long
                Dim hDCMemory As Long
                Dim hBmp As Long
                Dim hBmpPrev As Long
                Dim WidthSrc As Long
                Dim HeightSrc As Long
                Dim Pic As PicBmp
                Dim IPic As IPicture
                Dim IID_IDispatch As GUID

                Dim vOffset As Long
                Dim hOffset As Long

                'CaptureWindow
                WidthSrc = Me.ScaleWidth \ Screen.TwipsPer PixelX
                HeightSrc = Me.ScaleHeight \ Screen.TwipsPer PixelY

                vOffset = ((Me.Width - Me.ScaleWidth) / 1.75) \ Screen.TwipsPer PixelX
                hOffset = ((Me.Height - Me.ScaleHeight) / 1.15) \ Screen.TwipsPer PixelY

                'Get a handle to the desktop window and get the proper device context
                hWndSrc = Me.hWnd
                hDCSrc = GetWindowDC(hWn dSrc)

                'Create a memory device context for the copy process
                hDCMemory = CreateCompatibl eDC(hDCSrc)

                'Create a bitmap and place it in the memory DC
                hBmp = CreateCompatibl eBitmap(hDCSrc, WidthSrc, HeightSrc)
                hBmpPrev = SelectObject(hD CMemory, hBmp)

                Print vOffset, hOffset

                'Copy the on-screen image into the memory DC
                Call BitBlt(hDCMemor y, 0, 0, WidthSrc, HeightSrc, _
                hDCSrc, vOffset, hOffset, vbSrcCopy)

                'Remove the new copy of the the on-screen image
                hBmp = SelectObject(hD CMemory, hBmpPrev)

                'Release the device context resources back to the system
                Call DeleteDC(hDCMem ory)
                Call ReleaseDC(hWndS rc, hDCSrc)

                'Fill in OLE IDispatch Interface ID
                With IID_IDispatch
                .Data1 = &H20400
                .Data4(0) = &HC0
                .Data4(7) = &H46
                End With

                'Fill Pic with necessary parts
                With Pic
                .Size = Len(Pic) 'Length of structure
                .Type = vbPicTypeBitmap 'Type of Picture (bitmap)
                .hBmp = hBmp 'Handle to bitmap
                .hPal = 0& 'Handle to palette (may be null)
                End With

                'Create OLE Picture object
                Call OleCreatePictur eIndirect(Pic, IID_IDispatch, 1, IPic)

                'Return the new Picture object
                Set GetOLEScreenSna pshot = IPic

                End Function



                --

                Randy Birch
                MVP Visual Basic

                Please respond only to the newsgroups so all can benefit.


                "Pavan Arise" <pavankarise@ya hoo.com> wrote in message
                news:ebda6084.0 406211725.3755d 4a@posting.goog le.com...
                : Thank you Randy Birch for the invaluable piece of code. I have the
                : program working perfectly as I wanted. But there is a small problem
                : here. I was trying to save just the form content only. By the way it
                : is now, the title bar (Wher the caption is written) is also getting
                : saved. I tried to change the form borderstyle setting to 0 (None) &
                : refreshing the form before calling the GetOLEScreenSna pshot sub, but
                : for some reason, the form border isn't changing and the form along
                : with the titlebar is getting saved. There were some other buttons on
                : the form which were not saved when I had them as visible =false during
                : runtime. Is it that the borderstyle doesn't change during runtime, or
                : are there any other changes to be made for having the titlebar not to
                : be saved with the form. Thank you again and any suggestions are
                : appreciated.

                Comment

                • Pavan Arise

                  #9
                  Re: Save Picturebox

                  Dear Randy Birch,
                  The changed routine that you have provided has solved my purpose
                  exactly to the core. I am very much thankful to you for helping me
                  out. I was trying to crack it for more than a week continuously & you
                  have made it for me exactly as I wanted. I thank you very much again/

                  Regards,
                  Pavan.

                  Comment

                  Working...