Results 1 to 24 of 24

Thread: Saving images [Resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Resolved Saving images [Resolved]

    How would I save an image that is in a picturebox by pressing a commandbutton?
    Last edited by abstrait; Feb 9th, 2006 at 05:56 PM.

  2. #2
    New Member
    Join Date
    Feb 2006
    Location
    Alabama
    Posts
    15

    Re: Saving images

    VB Code:
    1. SavePicture Picture1.Image, "C:\yourimagename.bmp"

    Found by searching these forums.

  3. #3
    Addicted Member
    Join Date
    Jan 2006
    Posts
    246

    Re: Saving images

    How would you save ANY file? without common dialog

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Saving images

    Quote Originally Posted by dwthomas05
    VB Code:
    1. SavePicture Picture1.Image, "C:\yourimagename.bmp"

    Found by searching these forums.
    If by "image" he meant something that was drawing on the picturebox (using PaintPicture, Print, Line methods) then yes that would work. Use Picture1.Picture otherwise.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    If I've moved images on top of images in a picutrebox, how would I save the new image that is formed in the picturebox?

  6. #6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    A code that allows me to drag the images

    VB Code:
    1. Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2. If Draging = False Then
    3.     Draging = True
    4.     XPos = X
    5.     Ypos = Y
    6.     Image1.MousePointer = 1
    7.     Image1.ZOrder 1
    8.     Exit Sub
    9. End If
    10. End Sub
    11.  
    12. Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    13. If Draging = True Then
    14.     Image1.Move Image1.Left + (X - XPos), Image1.Top + (Y - Ypos)
    15. End If
    16. End Sub
    17.  
    18. Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19. Draging = False
    20. Image1.MousePointer = 0
    21. End Sub

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    I'm dragging images on top of other images and I want to save the new image that is formed

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Saving images

    I'm afraid you're not listening: in order to "form" new picture you need to paint the one that just dragged directly onto the picturebox usein PaintPicture() method. Whet you are ready to save you'd save Pciture1.Image as suggested in one of the first replies. But so for you're only dragging Image controls. Picturebox has no Image nor Picture properties assigned yet so there nothing to save.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    How would I do that then?

  12. #12
    New Member bflsantos's Avatar
    Join Date
    Feb 2006
    Posts
    3

    Re: Saving images

    I'm interested on this thread...

    I've on program that runs several times some random problems and I need to store a picture of each of these problems. Thus, I use a picture box to create the pictures during the run-time, and I save then using the SavePicture process. At the end, I use the same picture box to go through all the pictures and see them nice on a form

    Everything seems nice until now... but life is not that bright, and I have one problem. The pictures are saved always as gif picture (even if a give them a jpg extension). This makes my pictures file to have more the 1MB!! When I have to run 100 samples several times, you can image the amount of space that I'm using to store all these pictures.

    Do you know other way to save pictures at the run-time?

    I'll appreciate all your help. Thanx!

  13. #13
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Saving images

    Quote Originally Posted by abstrait
    How would I do that then?
    Here's a very basic sample so try to work with it to make it better:
    VB Code:
    1. Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     Draging = False
    3.     Image1.MousePointer = 0
    4.     Picture1.PaintPicture Image1.Picture, Image1.Left, Image1.Top
    5.     SavePicture Picture1.Image, "c:\test1.bmp"
    6. End Sub

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    It's still just saving as a white square.

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Saving images

    First of all, you'll find it impossible to drag an image on to the top of a picturebox, so I would suggest going to all pictureboxes.

    Make sure you have Autoredraw properties set to true and your savepicture method should be slightly different that what the Rhino sugested
    VB Code:
    1. SavePicture Picture1.Image, "C:\test.bmp"
    Also if your main picturebox is not located at 0,0 then you'll have to enter an offset in the Paintpicture call.
    VB Code:
    1. Picture1.PaintPicture Picture2.Picture, Picture2.Left - Picture1.Left, Picture2.Top - Picture1.Top
    Finally, if your dragging picture has a transparent background that you want to keep then we'll have to do it in a different manner.

    Here is code I just tested
    VB Code:
    1. Option Explicit
    2. Dim Draging As Boolean
    3. Dim Xpos As Single
    4. Dim Ypos As Single
    5.  
    6. Private Sub Command1_Click()
    7.     SavePicture Picture1.Image, "C:\test2.bmp"
    8.     Picture1.Picture = LoadPicture("C:\test2.bmp")
    9.    
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13.     Picture1.Picture = LoadPicture("C:\test.bmp")
    14.     Picture2.Picture = LoadPicture("\\7-up.gif")
    15.     Picture1.AutoRedraw = True
    16. End Sub
    17.  
    18. Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19.     If Draging = False Then
    20.         Draging = True
    21.         Xpos = X
    22.         Ypos = Y
    23.         Picture2.MousePointer = 1
    24.         Picture2.ZOrder
    25.         Exit Sub
    26.     End If
    27. End Sub
    28.  
    29. Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    30.     If Draging = True Then
    31.         Picture2.Move Picture2.Left + (X - Xpos), Picture2.Top + (Y - Ypos)
    32.     End If
    33. End Sub
    34.  
    35. Private Sub Picture2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    36.     Draging = False
    37.     Picture2.MousePointer = 0
    38.     Picture2.ZOrder 1
    39.     Picture1.PaintPicture Picture2.Picture, Picture2.Left - Picture1.Left, Picture2.Top - Picture1.Top
    40. End Sub

  16. #16
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Saving images

    Quote Originally Posted by moeur
    First of all, you'll find it impossible to drag an image on to the top of a picturebox, ...
    All Image controls "belong" to Picturebox container as far as I understand ... if not then he confused everybody.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    moeur - Thanks. It works great.
    However, once I've placed a picture over the picture box I am not able to move it anymore. How can I still move it after I've placed it i nthe picture box incase I've placed it in the wrong spot?
    Also, it keeps its transparancy until it placed inside the picturebox. How can I make it so it also has transparancy?
    Last edited by abstrait; Feb 8th, 2006 at 04:02 AM.

  18. #18
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Saving images

    OK, here is a solution for transparent images. I handle it by using a Usercontrol rather than an Image control or picturebox.

    Add A Usercontrol to your project and add this code to it's code module
    VB Code:
    1. Option Explicit
    2.  
    3. Private Draging As Boolean
    4. Private Xpos As Single
    5. Private Ypos As Single
    6.  
    7. 'this is where we put the picture into the control
    8. 'transpColor is the background color of the picture that you
    9. 'don't want to show
    10. Public Sub LoadPicture(pic As Picture, transpColor As Long)
    11.     Set UserControl.Picture = pic
    12.     Set UserControl.MaskPicture = pic
    13.     UserControl.MaskColor = transpColor
    14.     'AutoSize to match new picture
    15.     UserControl.Width = ScaleX(pic.Width, vbHimetric, vbTwips)
    16.     UserControl.Height = ScaleY(pic.Height, vbHimetric, vbTwips)
    17.     'turn off border
    18.     UserControl.BorderStyle = 0
    19. End Sub
    20.  
    21. Public Property Get hdc() As Long
    22.     hdc = UserControl.hdc
    23. End Property
    24.  
    25. Public Property Get MaskColor() As Long
    26.     MaskColor = UserControl.MaskColor
    27. End Property
    28.  
    29. Private Sub UserControl_Initialize()
    30.     With UserControl
    31.         .AutoRedraw = True
    32.         .BackStyle = 0 'transparent
    33.         .BorderStyle = 1 'we'll change this once there is a picure loaded
    34.         .Appearance = 0
    35.     End With
    36. End Sub
    37.  
    38. '--- allows dragging of the picture ---
    39. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    40.     If Draging = False Then
    41.         Draging = True
    42.         Xpos = x
    43.         Ypos = y
    44.         UserControl.MousePointer = 1
    45.         Extender.ZOrder
    46.         Exit Sub
    47.     End If
    48. End Sub
    49.  
    50. Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    51.     If Draging = True Then
    52.         Extender.Left = Extender.Left + (x - Xpos) \ Screen.TwipsPerPixelX
    53.         Extender.Top = Extender.Top + (y - Ypos) \ Screen.TwipsPerPixelY
    54.     End If
    55. End Sub
    56.  
    57. Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    58.     Draging = False
    59.     UserControl.MousePointer = 0
    60. End Sub
    Now place one on your form and rename it ClearPic. Here is the code for your form
    VB Code:
    1. Option Explicit
    2.  
    3. Dim Draging As Boolean
    4. Dim Xpos As Single
    5. Dim Ypos As Single
    6.  
    7. Private Declare Function TransparentBlt Lib "msimg32.dll" ( _
    8.     ByVal hdc As Long, _
    9.     ByVal x As Long, _
    10.     ByVal y As Long, _
    11.     ByVal nWidth As Long, _
    12.     ByVal nHeight As Long, _
    13.     ByVal hSrcDC As Long, _
    14.     ByVal xSrc As Long, _
    15.     ByVal ySrc As Long, _
    16.     ByVal nSrcWidth As Long, _
    17.     ByVal nSrcHeight As Long, _
    18.     ByVal crTransparent As Long _
    19. ) As Boolean
    20.  
    21. Private Sub Command1_Click()
    22.     Dim xBW As Long
    23.     Dim yBW As Long
    24.     'get picture border widths
    25.     xBW = (Picture1.Width - Picture1.ScaleWidth) \ 2
    26.     yBW = (Picture1.Height - Picture1.ScaleHeight) \ 2
    27.     'afix moveable picture
    28.     With ClearPic
    29.         TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
    30.             .Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
    31.             0, 0, .Width, .Height, .MaskColor
    32.         Picture1.Refresh
    33.             .Visible = False
    34.     End With
    35.     SavePicture Picture1.Image, "C:\test2.bmp"
    36. 'reload picture from disk just for testing purposes
    37.     Picture1.Picture = LoadPicture("C:\test2.bmp")
    38. End Sub
    39.  
    40. Private Sub Form_Load()
    41.     With Picture1
    42.         .ScaleMode = vbPixels
    43.         .AutoRedraw = True
    44.         .AutoSize = True
    45. 'load the background picture
    46.         .Picture = LoadPicture("C:\bliss.bmp")
    47.     End With
    48.     Me.ScaleMode = vbPixels
    49. 'put your transparent picture into the control
    50. 'be sure to choose the correct background color
    51. 'if it is not white
    52.     ClearPic.LoadPicture LoadPicture("C:\fisch.gif"), vbWhite
    53. End Sub
    Notice that now you can move the control as much as you want. Once it is in place, press the button and it is written to the picture.

    Attached is the project
    Attached Files Attached Files
    Last edited by moeur; Feb 8th, 2006 at 11:17 AM.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    Perfect, thanks!!!

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images

    I ran into a problem. This allows all images to be dragged if I add like

    VB Code:
    1. ClearPic.LoadPicture LoadPicture("C:\1.gif"), vbWhite
    2. ClearPic2.LoadPicture LoadPicture("C:\2.gif"), vbWhite
    3. ClearPic3.LoadPicture LoadPicture("C:\3.gif"), vbWhite

    Which is how I want it, but when I click the button only the first image is placed and the rest are still movable and they don't save. How do I fix this?

  21. #21
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Saving images

    for each control you add, you have to call TransparentBlt to copy it into the picturebox
    VB Code:
    1. With ClearPic
    2.         TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
    3.             .Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
    4.             0, 0, .Width, .Height, .MaskColor
    5.         .Visible=False
    6. End With
    7.  
    8. With ClearPic2
    9.         TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
    10.             .Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
    11.             0, 0, .Width, .Height, .MaskColor
    12.         .Visible=False
    13. End With
    14.  
    15. With ClearPic3
    16.         TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
    17.             .Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
    18.             0, 0, .Width, .Height, .MaskColor
    19.         .Visible=False
    20. End With

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images [One last question]

    Thanks.
    How can I hide certain pictures until I click on a button to display them?

  23. #23
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Saving images [One last question]

    to hide a picture
    VB Code:
    1. ClearPic.Visible=False
    To show it
    VB Code:
    1. ClearPic.Visible=True

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Saving images [One last question]

    Thanks!

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