Hejsan! <code>Rotera en bild
Jag har en picture box i en VB app som jag vill kunna rotera i grader.
Är det någon som vet hur man kan göra detta?Sv: Rotera en bild
Option Explicit 'Form1
Private Sub Command1_Click()
Call RotateBitmap(Picture1, Picture2, 3.14 / 4)
End Sub
'Modul
Option Explicit
Private Const Pi As Single = 3.14159265358979
Private Declare Function SetPixel Lib "GDI32" (ByVal hDC As Long, ByVal X As Long, _
ByVal Y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal X As Long,
ByVal Y As Long) As Long
' Rotate the image in a picture box.
' pic1 is the picture box with the bitmap to rotate
' pic2 is the picture box to receive the rotated bitmap
' theta is the angle of rotation
Sub RotateBitmap(pic1 As PictureBox, pic2 As PictureBox, ByVal theta As Single)
Dim c1x As Long, c1y As Long
Dim c2x As Long, c2y As Long
Dim a As Single
Dim p1x As Long, p1y As Long
Dim p2x As Long, p2y As Long
Dim pic1hDC As Long, pic2hDC As Long
Dim n As Long, r As Long
Dim c0 As Long, c1 As Long, c2 As Long, c3 As Long
Dim xret As Long
c1x = pic1.ScaleX(pic1.ScaleWidth \ 2, pic1.ScaleMode, vbPixels)
c1y = pic1.ScaleX(pic1.ScaleHeight \ 2, pic1.ScaleMode, vbPixels)
c2x = pic2.ScaleX(pic2.ScaleWidth \ 2, pic2.ScaleMode, vbPixels)
c2y = pic2.ScaleX(pic2.ScaleHeight \ 2, pic2.ScaleMode, vbPixels)
If c2x < c2y Then
n = c2y - 1
Else
n = c2x - 1
End If
pic1hDC = pic1.hDC
pic2hDC = pic2.hDC
For p2x = 0 To n
For p2y = 0 To n
If p2x = 0 Then
a = Pi / 2
Else
a = Atn(p2y / p2x)
End If
r = Sqr(p2x * p2x + p2y * p2y)
p1x = r * Cos(a + theta)
p1y = r * Sin(a + theta)
c0 = GetPixel(pic1hDC, c1x + p1x, c1y + p1y)
c1 = GetPixel(pic1hDC, c1x - p1x, c1y - p1y)
c2 = GetPixel(pic1hDC, c1x + p1y, c1y - p1x)
c3 = GetPixel(pic1hDC, c1x - p1y, c1y + p1x)
If c0 <> -1 Then xret = SetPixel(pic2hDC, c2x + p2x, c2y + p2y, c0)
If c1 <> -1 Then xret = SetPixel(pic2hDC, c2x - p2x, c2y - p2y, c1)
If c2 <> -1 Then xret = SetPixel(pic2hDC, c2x + p2y, c2y - p2x, c2)
If c3 <> -1 Then xret = SetPixel(pic2hDC, c2x - p2y, c2y + p2x, c3)
Next
pic2.Refresh
Next
End Sub
</code>