How would I save an image that is in a picturebox by pressing a commandbutton?
Printable View
How would I save an image that is in a picturebox by pressing a commandbutton?
VB Code:
SavePicture Picture1.Image, "C:\yourimagename.bmp"
Found by searching these forums.
How would you save ANY file? without common dialog
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.Quote:
Originally Posted by dwthomas05
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?
How do you move it?
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
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 ... :confused:
I'm dragging images on top of other images and I want to save the new image that is formed
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.
How would I do that then?
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!
Here's a very basic sample so try to work with it to make it better:Quote:
Originally Posted by abstrait
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
It's still just saving as a white square.
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
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:
SavePicture Picture1.Image, "C:\test.bmp"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.VB Code:
Picture1.PaintPicture Picture2.Picture, Picture2.Left - Picture1.Left, Picture2.Top - Picture1.Top
Here is code I just testedVB 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
All Image controls "belong" to Picturebox container as far as I understand ... if not then he confused everybody. :)Quote:
Originally Posted by moeur
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?
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 moduleNow place one on your form and rename it ClearPic. Here is the code for your formVB 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 SubNotice 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.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
Attached is the project
Perfect, thanks!!!
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?
for each control you add, you have to call TransparentBlt to copy it into the pictureboxVB 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
Thanks. :)
How can I hide certain pictures until I click on a button to display them?
to hide a pictureTo show itVB Code:
ClearPic.Visible=FalseVB Code:
ClearPic.Visible=True
Thanks! :)