Results 1 to 16 of 16

Thread: picturebox problem [RESOLVED]

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    Question picturebox problem [RESOLVED]

    I have been developing a program and am now trying to save its contents. What i have is a picturebox with several things inside of it including textboxes, and other pictureboxes. All i want to do is save the main picturebox along with all of the other stuff inside of it to a .bmp or a .jpg. I have tried all the normal things but nothing inside the picture box is saved. Does anyone have any thoughts on that? The file is physically created, however it does not contain the contents of the picturebox, only the border of the picturebox.

    This is my current code:

    SavePicture frmImage3.Picture1.Image, CommonDialog1.filename

    I also tried

    SavePicture frmImage3.Picture1.Picture, CommonDialog1.filename

    and ive also tried changing the AutoRedraw function. However none of these work properly.. i get the same results each time.

    Thanks in advance.
    Last edited by ParadoxMember; Mar 5th, 2003 at 12:01 PM.
    Sometimes the simplest things are the hardest...

  2. #2
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Pay attention to comments:
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     '-----------------------------------------------------------------------------
    4.     'FROM MSDN:
    5.     'If a graphic was loaded from a file to the Picture property of an object,
    6.     'either atdesign time or atrun time, and it’s a bitmap, icon, metafile,
    7.     'or enhanced metafile, it's saved using the same format as the original file.
    8.     'If it is a GIF or JPEG file, it is saved as a bitmap file.
    9.     '-----------------------------------------------------------------------------
    10.     CommonDialog1.Filter = "Pictures (*.bmp;*.ico;*.wmf)|*.bmp;*.ico;*.wmf"
    11.     CommonDialog1.ShowSave
    12.     SavePicture Picture1.Picture, CommonDialog1.FileName
    13.  
    14. End Sub
    McGenius

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    picturebox...

    The file that i opened into the picturebox was a jpg, and it still did not save when i saved it as a bmp.

    'FROM MSDN:
    'If it is a GIF or JPEG file, it is saved as a bitmap file.

    Is it possible to save items that are stored inside a picturebox?

    Even my textboxes are not saving!

    Thanks for the reply though... I appreciate it.. I'm really throwing my head against a brick wall here!
    Sometimes the simplest things are the hardest...

  4. #4
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Is it possible to save items that are stored inside a picturebox?
    I already gave you the answer.
    Even my textboxes are not saving!
    Not sure I follow you ...
    McGenius

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    picturebox

    Im sorry if i was confusing... my problem is simply this: nothing i put inside the picturebox saves. I have an option where u can press a button and add textboxes and pictureboxes to the picturebox. But when i click save i get nothing. simply the frame of the original picturebox. Nothing inside of it. No text, no additional images.. i am rather confused as to why.
    Sometimes the simplest things are the hardest...

  6. #6
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    Because when your SavePicture its saving the image held in the PictureBox's picture object. Sounds like your using the PictureBox as a container for other things. Even those things such as your textboxes are contained in there, they havent been added to the picture box's image.

    However there are ways of doing it. You could CaptureDC of the picture boxes area and save it to the picturebox.picture object. Then you should be able to save it as a bmp. You cant save as a JPG unless you know the compression for jpg or have a third-party control which has been talked about in these forums.

  7. #7
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    Try this and set the PictureBox's AutoDraw = True

    VB Code:
    1. '______________________________________
    2. '
    3. ' Declarations
    4. '______________________________________
    5.  
    6. Private Declare Function BitBlt Lib "gdi32" _
    7.         (ByVal hDCDest As Long, _
    8.         ByVal XDest As Long, _
    9.         ByVal YDest As Long, _
    10.         ByVal nWidth As Long, _
    11.         ByVal nHeight As Long, _
    12.         ByVal hDCSrc As Long, _
    13.         ByVal xSrc As Long, _
    14.         ByVal ySrc As Long, _
    15.         ByVal dwRop As Long) As Long
    16.        
    17. Private Declare Function GetDC Lib "user32" _
    18.         (ByVal hwnd As Long) As Long
    19.  
    20. Private Declare Function ReleaseDC Lib "user32" _
    21.         (ByVal hwnd As Long, _
    22.         ByVal hdc As Long) As Long
    23.  
    24. Private Declare Function GetWindowRect Lib "user32" _
    25.         (ByVal hwnd As Long, _
    26.         lpRect As RECT) As Long
    27.        
    28. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    29.  
    30. Private Type RECT
    31.         Left As Long
    32.         Top As Long
    33.         Right As Long
    34.         Bottom As Long
    35. End Type
    36.  
    37. '______________________________________
    38. '
    39. ' Form Code To Flatten Image and Save
    40. '______________________________________
    41.  
    42. Private Sub Command1_Click()
    43.  
    44.  Dim Desktop As Long
    45.  Dim DesktopDC As Long
    46.  
    47.  'find the position of PictureBox on screen
    48.  Dim MainL As RECT
    49.  GetWindowRect Picture1.hwnd, MainL
    50.  
    51.  'capture desktop DC
    52.  Desktop = GetDesktopWindow
    53.  DesktopDC = GetDC(Desktop)
    54.  
    55.  'bitblt into PictureBox
    56.  BitBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, DesktopDC, MainL.Left, MainL.Top, vbSrcCopy
    57.  
    58.  'Release the device context
    59.  Call ReleaseDC(Desktop, DesktopDC)
    60.  
    61.  'save to bmp
    62.  SavePicture Picture1.Image, "C:\ss.bmp"
    63.  
    64. End Sub
    Last edited by Eyes.Only; Mar 4th, 2003 at 06:26 PM.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    cool

    I had to change this code:

    'Release the device context
    Call ReleaseDC(hWndDesk, hDCDesk)

    to this:
    'Release the device context
    Call ReleaseDC(hWnd, hDC)

    I am not sure exactly why.. but I did.. anyway.. thank you it worked perfectly...

    Sorry to bother.. but one last question... is there anyway i could save it so that it can be edited at a later date..? i cant modify a bmp at a later date.. just a question if its possible.. but thank you for the code... that was a lifesaver..
    Sometimes the simplest things are the hardest...

  9. #9
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Sorry, couldn't get back to you earlier.
    I see now where you're heading. No, that's not what savePicture was desined for: primerily saving loaded picture. What you needed was Screen capturing and that code snippet from Eyes.Only should do it. Second part of your question is has nothing to do with saving picture but the content of your Textboxes (or whatever) to say Text file so you can easily open it later and load its data back into a textbox.
    McGenius

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    so basically..

    So basically its going to be a real pain to get the files so that i can edit them later? bummer
    Sometimes the simplest things are the hardest...

  11. #11
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Nor really. Here is what you need to do:
    VB Code:
    1. 'To save to a file
    2. Open App.Path & "\tempdata.txt" For Output As #1
    3.     Print #1, Text1.Text
    4. Close #1
    5.  
    6. 'To get from a file
    7. Open App.Path & "\tempdata.txt" For Input As #1
    8.     Text1.Text = Input(LOF(1), #1)
    9. Close #1
    There will be some variations, of course, but it wouldn't be that complex.
    McGenius

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    thanks

    Thank you so much for the code... I have to get out of the office now but I will try this more tomorrow.. thank you all for all your help!...
    Sometimes the simplest things are the hardest...

  13. #13
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    whoops...my bad. I copied that line from my project and then forgot to change the variable names. I edited that post to reflect the right names now. I would take a look to make sure you get it right cuz you dont want any memory leaks

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    not a problem

    Thanks for the heads up... i did run through the code quickly and everything else appreaded to be correct, but I will double check that... still learning a lot of this stuff...

    As for the saving files I think i must have the syntax wrong... because the file is saving properly (with all of the options I have tried so far), but then i cannot re-load the file.

    This is my code to save the file:

    Private Sub Command8_Click()
    Open App.Path & "\tempdata.txt" For Output As #1
    Print #1, Picture5(0).Height
    Print #1, Picture5(0).Width
    Print #1, Text1.text
    Print #1, Text1.Height
    Print #1, Text1.Width

    Close #1

    End Sub

    This works, it saves in the textfile perfectly..

    However, my code to reopen the file does not work right at all, at the moment i am getting a type mismatch error, but i have gotten many others as well.

    Private Sub Command9_Click()
    Open App.Path & "\tempdata.txt" For Input As #1
    Picture5(0).Height = Input(LOF(1), #1)
    Picture5(0).Width = Input(LOF(2), #1)
    Text1.text = Input(LOF(3), #1)
    Text1.Height = Input(LOF(4), #1)
    Text1.Width = Input(LOF(5), #1)
    Close #1

    End Sub

    I assumed the LOF mean "Line of File" which is why it is so numbered, but I may be wrong since ive never done this before ... any thoghts? Thanks
    Sometimes the simplest things are the hardest...

  15. #15
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    Re: picturebox problem

    To simplify Eyes.Only's code, there is a PrintWindow API Call
    VB Code:
    1. Private Declare Function PrintWindow Lib "user32" _
    2. (ByVal hWnd As Long, _
    3. ByVal hdcBlt As Long, _
    4. ByVal nFlags As Long) As Long
    5.  
    6.  
    7. Private Sub Command1_Click()
    8. With Picture2
    9.     .Height = Picture1.Height
    10.     .Width = Picture1.Width
    11.     .AutoRedraw = True
    12. End With
    13. Call PrintWindow(Picture1.hWnd, Picture2.hdc, 0)
    14. SavePicture Picture2.Image, App.Path & "\bmpFromPicBox.bmp"
    15. Shell "Mspaint.exe " & App.Path & "\bmpFromPicBox.bmp"
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19. Picture2.Visible = False
    20. End Sub

    HTH

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    CT
    Posts
    62

    thanks

    Thanks for all the input... i finally found what i was looking for on how to save the files into a textbox so they are editable in another thread... its going to be a lot of code to save everything becuase users can add dynamic content.. but it is doable i am sure about that... so thanks everyone for all your help
    Sometimes the simplest things are the hardest...

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