PDA

View Full Version : سوال: چرخش عکس



pouya9111
جمعه 10 بهمن 1393, 11:50 صبح
چرخش عکس به دور خود؟

gilsoft
جمعه 10 بهمن 1393, 19:10 عصر
چرخش عکس به دور خود؟

سلام دوست عزیز

اینم کد چرخش عکس:
Imports System.Drawing.Drawing2D

Public Class Form1
Dim gr As Graphics
Dim mat As New Matrix
Dim im As Image


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
gr = Me.CreateGraphics
im = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg")
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mat.RotateAt(30, New Point(150, 150))
gr.Transform = mat
ReDrawImage()
End Sub


Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
ReDrawImage()
End Sub


Sub ReDrawImage()
gr.Clear(Color.Wheat)
gr.DrawImage(im, 100, 100, 100, 100)
End Sub
End Class

موفق باشیذ .....

gilsoft
جمعه 10 بهمن 1393, 19:31 عصر
اینم یه مدل دیگه:
Dim img As New Bitmap("C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg")
Dim iAngel As Integer = 0


Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.BackColor = Color.Black
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
PictureBox1.Image = RotateImg(img, Convert.ToSingle(iAngel))
Button2.Text = "<" : Button3.Text = ">"
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
iAngel -= 30 : If iAngel < 0 Then iAngel = 330
PictureBox1.Image = RotateImg(img, Convert.ToSingle(iAngel))
End Sub


Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
iAngel += 30 : If iAngel > 360 Then iAngel = 30
PictureBox1.Image = RotateImg(img, Convert.ToSingle(iAngel))
End Sub

Public Function RotateImg(ByVal bmpimage As Bitmap, ByVal angle As Single) As Bitmap
Dim w As Integer = bmpimage.Width
Dim h As Integer = bmpimage.Height
Dim pf As System.Drawing.Imaging.PixelFormat = Nothing
pf = bmpimage.PixelFormat
Dim tempImg As New Bitmap(w, h, pf)
Dim g As Graphics = Graphics.FromImage(tempImg)
g.DrawImageUnscaled(bmpimage, 1, 1)
g.Dispose()
Dim path As New GraphicsPath()
path.AddRectangle(New RectangleF(0.0F, 0.0F, w, h))
Dim mtrx As New Matrix()

mtrx.Rotate(angle)
Dim rct As RectangleF = path.GetBounds(mtrx)
Dim newImg As New Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height), pf)
g = Graphics.FromImage(newImg)
g.TranslateTransform(-rct.X, -rct.Y)
g.RotateTransform(angle)
g.InterpolationMode = InterpolationMode.HighQualityBilinear
g.DrawImageUnscaled(tempImg, 0, 0)
g.Dispose()
tempImg.Dispose()
Return newImg
End Function

موفق باشید .....