PDA

View Full Version : مبتدی: rotate عکس در ویژوال بیسیک



SHAZAM
دوشنبه 15 مهر 1392, 15:03 عصر
سلام
سلام اگه میشه اینو بهم یاد بدین:

مثلا کاربر میاد و یه عکس میذاره تو پیکچر باکس و میخواد اونو rotate کنه
اگه لطف کنین ممنون میشم

rahnema1
دوشنبه 15 مهر 1392, 20:02 عصر
کدی که توی آدرس زیر هست تصویر رو می چرخونه اما سرعتش شاید کم باشه
http://support.microsoft.com/kb/80406

کدی که توی کادر زیر می بینی از سایت زیر گرفتم سرعتش هم خوبه
https://groups.google.com/forum/#!msg/microsoft.public.vb.winapi/jwLoUbeFwVU/-uRgzZ7-0R4J
همون جوری که توضیح داده دو تا picturebox به نام Picture1 و Picture2 و یک scroll bar بنام HScroll1 توی فرم بگذار یک تصویر را توی picture1 بگذار بعد کد زیر رو توی vb کپی و پیست کن و برنامه رو اجرا کن
وقتی scroll bar رو تغییر می دهی زاویه چرخش پیدا می کنه



Private Declare Function PlgBlt Lib "GDI32.dll" (ByVal hDCDest As Long, _
ByRef lpPoint As PointAPI, ByVal hdcSrc As Long, ByVal nXSrc As Long, _
ByVal nYSrc As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hbmMask As Long, ByVal xMask As Long, ByVal yMask As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32.dll" (ByVal hDC As Long) As Long
Private Declare Function SelectObject Lib "GDI32.dll" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "GDI32.dll" (ByVal hDC As Long) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Private Function DrawStdPictureRot(ByVal inDC As Long, ByVal inX As Long, _
ByVal inY As Long, ByVal inAngle As Single, ByRef inPicture As StdPicture) As Long
Dim hDC As Long
Dim hOldBMP As Long
Dim PlgPts(0 To 4) As PointAPI
Dim PicWidth As Long, PicHeight As Long
Dim HalfWidth As Single, HalfHeight As Single
Dim AngleRad As Single
Const Pi As Single = 3.14159
Const HalfPi As Single = Pi * 0.5
' Validate input picture
If (inPicture Is Nothing) Then Exit Function
If (inPicture.Type <> vbPicTypeBitmap) Then Exit Function
' Get picture size
PicWidth = ScaleX(inPicture.Width, vbHimetric, vbPixels)
PicHeight = ScaleY(inPicture.Height, vbHimetric, vbPixels)
' Get half picture size and angle in radians
HalfWidth = PicWidth / 2
HalfHeight = PicHeight / 2
AngleRad = (inAngle / 180) * Pi
' Create temporary DC and select input picture into it
hDC = CreateCompatibleDC(0&)
hOldBMP = SelectObject(hDC, inPicture.Handle)
If (hOldBMP) Then ' Get angle vectors for width and height
PlgPts(0).X = Cos(AngleRad) * HalfWidth
PlgPts(0).Y = Sin(AngleRad) * HalfWidth
PlgPts(1).X = Cos(AngleRad + HalfPi) * HalfHeight
PlgPts(1).Y = Sin(AngleRad + HalfPi) * HalfHeight
' Project parallelogram points for rotated area
PlgPts(2).X = HalfWidth + inX - PlgPts(0).X - PlgPts(1).X
PlgPts(2).Y = HalfHeight + inY - PlgPts(0).Y - PlgPts(1).Y
PlgPts(3).X = HalfWidth + inX - PlgPts(1).X + PlgPts(0).X
PlgPts(3).Y = HalfHeight + inY - PlgPts(1).Y + PlgPts(0).Y
PlgPts(4).X = HalfWidth + inX - PlgPts(0).X + PlgPts(1).X
PlgPts(4).Y = HalfHeight + inY - PlgPts(0).Y + PlgPts(1).Y
' Draw rotated image
DrawStdPictureRot = PlgBlt(inDC, PlgPts(2), _
hDC, 0, 0, PicWidth, PicHeight, 0&, 0, 0)
' De-select Bitmap from DC
Call SelectObject(hDC, hOldBMP)
End If
' Destroy temporary DC
Call DeleteDC(hDC)
End Function
Private Sub HScroll1_Change()
Call ReDraw
End Sub
Private Sub HScroll1_Scroll()
Call ReDraw
End Sub
Private Sub ReDraw()
Picture2.AutoRedraw = True
Call Picture2.Cls
Call DrawStdPictureRot(Picture2.hDC, 0, 0, HScroll1.Value, Picture1.Picture)
Call Picture2.Refresh
End Sub