Hi,
I want to resize a gif image to fit into a picture box, and turn it upside down. How can i do this?
Printable View
Hi,
I want to resize a gif image to fit into a picture box, and turn it upside down. How can i do this?
from vbsquare...
Code:'// Make a New project. Add a module. To the form Add two picture boxes And a Command button.
'// Code:
'// Add this code To the module:
Declare Function SetPixel Lib "gdi32" Alias "SetPixelV" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
'// Add this code To the form's General Declarations procedure:
Public Sub rotateimage()
Dim x As Long
Dim y As Long
Picture2.Height = Picture1.Width
Picture2.Width = Picture1.Height
For x = 0 To Picture1.Width
For y = 0 To Picture1.Height
Call SetPixel(Picture2.hdc, y, x, GetPixel(Picture1.hdc, x, y))
Next
If x Mod 50 = 0 Then
DoEvents
Picture2.Refresh
End If
Next
DoEvents
Picture2.Refresh
End Sub
'// Add this code To the Command button:
Private Sub Command1_Click()
Call RotateImage
End Sub
There is another way - use the PaintPicture method.
Most controls with a graphic device context (hdc) has the PaintPicture method. That ioncludes Forms and PictureBoxes.
You simply pass it the picture and the coordinates and it renders the picture at that position on the control or form.
But it has some hidden powers: By specifying the width and height, you can stretch or shrink the picture to fit the box.Code:'Place the picture in the top left corner of the picturebox
Call picTest.PaintPicture(MyPicture, 0, 0)
By setting the x/y coordinates to the bottom right corner of the picture box, and the width and height as negative numbers, it flips the picture AND stretches it.Code:'fill the box with the picture
Call picTest.PaintPicture(MyPicture, 0, 0, picTest.Width, picTest.Height)
If you want to flip it only horizontally, make the width negative. For vertical, make the Height negative.
Hope this helps.Code:'stretched and upside down
With picTest
Call .PaintPicture(MyPicture, 0, .ScaleHeight, .ScaleWidth, -.ScaleHeight)
End With
Shrog
Thanx.
Shrog's method worked better for my purpose, because the setpixel thing was just too slow. I needed BitBlt speed.
Thanx guys!
OK. Another thing.
The transparent parts of the original gif, is now magenta in the copied pic. Any way around that?
Alas, no. The moment you "re-draw" the picture, it is not a gif any more, and you lose the transparency.
There are ways around this, but they are not simple and they are not fast. VB does not actually cater for transparency very well. Sorry.
Shrog
Never Mind...
I made a mask, and my Dest Pic's BGColor black.
Then I copied Src to Dest using vbSrcInvert (oh. Src's BGColor = White) and the copy mask to Dest using vbSrcAnd.
Works perfectly