Feb 6th, 2006, 03:25 PM
#1
Thread Starter
Addicted Member
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 .
Feb 6th, 2006, 03:32 PM
#2
New Member
Re: Saving images
VB Code:
SavePicture Picture1.Image, "C:\yourimagename.bmp"
Found by searching these forums.
Feb 6th, 2006, 04:12 PM
#3
Addicted Member
Re: Saving images
How would you save ANY file? without common dialog
Feb 6th, 2006, 04:39 PM
#4
Re: Saving images
Originally Posted by
dwthomas05
VB Code:
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.
Feb 6th, 2006, 07:01 PM
#5
Thread Starter
Addicted Member
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?
Feb 6th, 2006, 07:14 PM
#6
Feb 6th, 2006, 07:16 PM
#7
Thread Starter
Addicted Member
Re: Saving images
A code that allows me to drag the images
VB Code:
Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Draging = False Then
Draging = True
XPos = X
Ypos = Y
Image1.MousePointer = 1
Image1.ZOrder 1
Exit Sub
End If
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Draging = True Then
Image1.Move Image1.Left + (X - XPos), Image1.Top + (Y - Ypos)
End If
End Sub
Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Draging = False
Image1.MousePointer = 0
End Sub
Feb 6th, 2006, 07:28 PM
#8
Re: Saving images
As I understand you're moving Image control within Picturebox container but you are not painting or setting anything so what are going to save - there is nothing so far... Unless you have another piece of code ...
Feb 6th, 2006, 07:30 PM
#9
Thread Starter
Addicted Member
Re: Saving images
I'm dragging images on top of other images and I want to save the new image that is formed
Feb 6th, 2006, 07:43 PM
#10
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.
Feb 6th, 2006, 07:56 PM
#11
Thread Starter
Addicted Member
Re: Saving images
How would I do that then?
Feb 6th, 2006, 08:59 PM
#12
Feb 6th, 2006, 09:13 PM
#13
Re: Saving images
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:
Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Draging = False
Image1.MousePointer = 0
Picture1.PaintPicture Image1.Picture, Image1.Left, Image1.Top
SavePicture Picture1.Image, "c:\test1.bmp"
End Sub
Feb 7th, 2006, 02:03 PM
#14
Thread Starter
Addicted Member
Re: Saving images
It's still just saving as a white square.
Feb 7th, 2006, 06:57 PM
#15
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:
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:
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:
Option Explicit
Dim Draging As Boolean
Dim Xpos As Single
Dim Ypos As Single
Private Sub Command1_Click()
SavePicture Picture1.Image, "C:\test2.bmp"
Picture1.Picture = LoadPicture("C:\test2.bmp")
End Sub
Private Sub Form_Load()
Picture1.Picture = LoadPicture("C:\test.bmp")
Picture2.Picture = LoadPicture("\\7-up.gif")
Picture1.AutoRedraw = True
End Sub
Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Draging = False Then
Draging = True
Xpos = X
Ypos = Y
Picture2.MousePointer = 1
Picture2.ZOrder
Exit Sub
End If
End Sub
Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Draging = True Then
Picture2.Move Picture2.Left + (X - Xpos), Picture2.Top + (Y - Ypos)
End If
End Sub
Private Sub Picture2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Draging = False
Picture2.MousePointer = 0
Picture2.ZOrder 1
Picture1.PaintPicture Picture2.Picture, Picture2.Left - Picture1.Left, Picture2.Top - Picture1.Top
End Sub
Feb 7th, 2006, 08:29 PM
#16
Re: Saving images
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.
Feb 8th, 2006, 03:59 AM
#17
Thread Starter
Addicted Member
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 .
Feb 8th, 2006, 11:09 AM
#18
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:
Option Explicit
Private Draging As Boolean
Private Xpos As Single
Private Ypos As Single
'this is where we put the picture into the control
'transpColor is the background color of the picture that you
'don't want to show
Public Sub LoadPicture(pic As Picture, transpColor As Long)
Set UserControl.Picture = pic
Set UserControl.MaskPicture = pic
UserControl.MaskColor = transpColor
'AutoSize to match new picture
UserControl.Width = ScaleX(pic.Width, vbHimetric, vbTwips)
UserControl.Height = ScaleY(pic.Height, vbHimetric, vbTwips)
'turn off border
UserControl.BorderStyle = 0
End Sub
Public Property Get hdc() As Long
hdc = UserControl.hdc
End Property
Public Property Get MaskColor() As Long
MaskColor = UserControl.MaskColor
End Property
Private Sub UserControl_Initialize()
With UserControl
.AutoRedraw = True
.BackStyle = 0 'transparent
.BorderStyle = 1 'we'll change this once there is a picure loaded
.Appearance = 0
End With
End Sub
'--- allows dragging of the picture ---
Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Draging = False Then
Draging = True
Xpos = x
Ypos = y
UserControl.MousePointer = 1
Extender.ZOrder
Exit Sub
End If
End Sub
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Draging = True Then
Extender.Left = Extender.Left + (x - Xpos) \ Screen.TwipsPerPixelX
Extender.Top = Extender.Top + (y - Ypos) \ Screen.TwipsPerPixelY
End If
End Sub
Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Draging = False
UserControl.MousePointer = 0
End Sub
Now place one on your form and rename it ClearPic. Here is the code for your form
VB Code:
Option Explicit
Dim Draging As Boolean
Dim Xpos As Single
Dim Ypos As Single
Private Declare Function TransparentBlt Lib "msimg32.dll" ( _
ByVal hdc As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hSrcDC As Long, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal nSrcWidth As Long, _
ByVal nSrcHeight As Long, _
ByVal crTransparent As Long _
) As Boolean
Private Sub Command1_Click()
Dim xBW As Long
Dim yBW As Long
'get picture border widths
xBW = (Picture1.Width - Picture1.ScaleWidth) \ 2
yBW = (Picture1.Height - Picture1.ScaleHeight) \ 2
'afix moveable picture
With ClearPic
TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
.Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
0, 0, .Width, .Height, .MaskColor
Picture1.Refresh
.Visible = False
End With
SavePicture Picture1.Image, "C:\test2.bmp"
'reload picture from disk just for testing purposes
Picture1.Picture = LoadPicture("C:\test2.bmp")
End Sub
Private Sub Form_Load()
With Picture1
.ScaleMode = vbPixels
.AutoRedraw = True
.AutoSize = True
'load the background picture
.Picture = LoadPicture("C:\bliss.bmp")
End With
Me.ScaleMode = vbPixels
'put your transparent picture into the control
'be sure to choose the correct background color
'if it is not white
ClearPic.LoadPicture LoadPicture("C:\fisch.gif"), vbWhite
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
Last edited by moeur; Feb 8th, 2006 at 11:17 AM .
Feb 8th, 2006, 05:59 PM
#19
Thread Starter
Addicted Member
Feb 8th, 2006, 09:33 PM
#20
Thread Starter
Addicted Member
Re: Saving images
I ran into a problem. This allows all images to be dragged if I add like
VB Code:
ClearPic.LoadPicture LoadPicture("C:\1.gif"), vbWhite
ClearPic2.LoadPicture LoadPicture("C:\2.gif"), vbWhite
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?
Feb 8th, 2006, 10:47 PM
#21
Re: Saving images
for each control you add, you have to call TransparentBlt to copy it into the picturebox
VB Code:
With ClearPic
TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
.Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
0, 0, .Width, .Height, .MaskColor
.Visible=False
End With
With ClearPic2
TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
.Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
0, 0, .Width, .Height, .MaskColor
.Visible=False
End With
With ClearPic3
TransparentBlt Picture1.hdc, .Left - Picture1.Left - xBW, _
.Top - Picture1.Top - yBW, .Width, .Height, .hdc, _
0, 0, .Width, .Height, .MaskColor
.Visible=False
End With
Feb 9th, 2006, 03:49 PM
#22
Thread Starter
Addicted Member
Re: Saving images [One last question]
Thanks.
How can I hide certain pictures until I click on a button to display them?
Feb 9th, 2006, 05:51 PM
#23
Re: Saving images [One last question]
to hide a pictureTo show it
Feb 9th, 2006, 05:55 PM
#24
Thread Starter
Addicted Member
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
Forum Rules
Click Here to Expand Forum to Full Width