在Picturebox中旋转图像 [英] Rotating Image in Picturebox
本文介绍了在Picturebox中旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我是编程的新手和新手
现在,我正在尝试创建有关船舶导航的简单模拟.
我在创建指南针时遇到问题.
我找到了此代码(我不是创建此代码的人)
Hello
i am new and beginner in programming
right now, i am trying to create a simple simulation about ship navigation.
i have a problem on creating a compass.
i found this code (i am not the one that create this code)
Imports System.Math
Public Class Form1
Dim A, B As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnRotate_Click(sender As Object, e As EventArgs) Handles btnRotate.Click
Dim bm_in As New Bitmap(picSource.Image)
Dim wid As Single = bm_in.Width
Dim hgt As Single = bm_in.Height
Dim corners As Point() = {New Point(0, 0), New Point(wid, 0), New Point(0, hgt), New Point(wid, hgt)}
Dim cx As Single = wid / 2
Dim cy As Single = hgt / 2
Dim i As Long
For i = 0 To 3
corners(i).X -= cx
corners(i).Y -= cy
Next i
Dim theta As Single = Single.Parse(txtAngle.Text) * PI / 180.0
Dim sin_theta As Single = Sin(theta)
Dim cos_theta As Single = Cos(theta)
Dim X As Single
Dim Y As Single
For i = 0 To 3
X = corners(i).X
Y = corners(i).Y
corners(i).X = X * cos_theta + Y * sin_theta
corners(i).Y = -X * sin_theta + Y * cos_theta
Next i
Dim xmin As Single = corners(0).X
Dim ymin As Single = corners(0).Y
For i = 1 To 3
If xmin > corners(i).X Then xmin = corners(i).X
If ymin > corners(i).Y Then ymin = corners(i).Y
Next i
For i = 0 To 3
corners(i).X -= xmin
corners(i).Y -= ymin
Next i
Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
Dim gr_out As Graphics = Graphics.FromImage(bm_out)
ReDim Preserve corners(2)
gr_out.DrawImage(bm_in, corners)
picDest.Image = bm_out
'picSource.Image = bm_out
'A = TB1.Value
'txtAngle.Text = TB1.Value
B = txtAngle.Text
TB1.Value = txtAngle.Text
End Sub
Private Sub TB1_Scroll(sender As Object, e As EventArgs) Handles TB1.Scroll
Dim bm_in As New Bitmap(picSource.Image)
Dim wid As Single = bm_in.Width
Dim hgt As Single = bm_in.Height
Dim corners As Point() = {New Point(0, 0), New Point(wid, 0), New Point(0, hgt), New Point(wid, hgt)}
Dim cx As Single = wid / 2
Dim cy As Single = hgt / 2
Dim i As Long
For i = 0 To 3
corners(i).X -= cx
corners(i).Y -= cy
Next i
Dim theta As Single = Single.Parse(TB1.Value) * PI / 180.0
Dim sin_theta As Single = Sin(theta)
Dim cos_theta As Single = Cos(theta)
Dim X As Single
Dim Y As Single
For i = 0 To 3
X = corners(i).X
Y = corners(i).Y
corners(i).X = X * cos_theta + Y * sin_theta
corners(i).Y = -X * sin_theta + Y * cos_theta
Next i
Dim xmin As Single = corners(0).X
Dim ymin As Single = corners(0).Y
For i = 1 To 3
If xmin > corners(i).X Then xmin = corners(i).X
If ymin > corners(i).Y Then ymin = corners(i).Y
Next i
For i = 0 To 3
corners(i).X -= xmin
corners(i).Y -= ymin
Next i
Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
Dim gr_out As Graphics = Graphics.FromImage(bm_out)
ReDim Preserve corners(2)
gr_out.DrawImage(bm_in, corners)
picDest.Image = bm_out
'picSource.Image = bm_out
A = TB1.Value
txtAngle.Text = TB1.Value
End Sub
Private Sub txtAngle_TextChanged(sender As Object, e As EventArgs) Handles txtAngle.TextChanged
End Sub
End Class
我做到了,因此可以使用轨迹栏和按钮单击来旋转
问题是图片框中的图片没有在中心旋转.
谁能帮我? (对不起的语法不好)
i make it so it can be rotated with trackbar and button click
The problem is the picture in the picture box does not rotate at the center.
Can anyone help me? (sorry for bad grammar)
推荐答案
首先,旋转PictureBox
中的任何内容是一个非常糟糕的主意.此控件的设计目的不是为了使任何复杂程度最小的事情.您可以在System.Drawing.Bitmap
上进行图形转换,或者,如果您的实际目标是在屏幕上显示某些内容,则可以通过重写虚拟方法System.Windows.Control.OnPaint
或处理事件System.Windows.Control.Paint
并使用System.Drawing.Graphics
的实例立即进行渲染.甚至在争论中传递给您.如果使用PictureBox
,它甚至对您都没有帮助,只会造成额外的麻烦,并消耗额外的资源和CPU时间.不要这样做.
有关更多详细信息,请参阅我过去的答案:
在图片框内添加图片 [在C#中绘制矩形 [如何从旧图纸中清除面板 [ ^ ].
另请参见:
Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [如何加快我的vb.net应用程序的速度? [在面板上捕获图形 [在mdi子表单之间画线 [ http://msdn.microsoft.com/en-us/library/system.drawing. graphics.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system. drawing.graphics.transform.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. drawing.drawing2d.matrix.aspx [ ^ ].
这很容易做到,而且尝试起来很有趣,不像线性度量空间中的普通ad-hoc线性代数运算那样乏味且容易出错.别人已经完成了大部分工作. :-)
—SA
First of all, it''s a really bad idea to rotate anything inPictureBox
. This control is not designed for anything even minimally complicated. You can either do graphical conversion on aSystem.Drawing.Bitmap
, or, if your real goal is showing something on screen, render it immediately by overriding the virtual methodSystem.Windows.Control.OnPaint
or handling the eventSystem.Windows.Control.Paint
and using the instance ofSystem.Drawing.Graphics
passed to you in even arguments. If you usePictureBox
, it does not help you even a bit, but only creates extra hassles and consumes extra resources and CPU time. Don''t do it.
For further detail on that, please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].
See also:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].
You can even do both bitmap and on-screen rendering in one method, which is the best approach if you need to do both independently. You only need to write a separate method with the parameter of the typeSystem.Drawing.Graphics
. In one call, the instance of theGraphics
will be from pain event arguments, in another call, from an instance ofBitmap
.
Now, what to do with rotation, as well as other transformations? What you do can work, but you are doing it in a hard way. Many basic problems like that are already solved for you in theSystem.Drawing.Graphics
. This is reliably done via theTransform
which is of the typeSystem.Drawing.Drawing2D.Matrix
. You can start from the unit matrix (doing identical transform) and modify it according to a set of transforms you need. This type provides you with predefined operations for rotations, shearing, translation, scaling, reflections, as well as the custom matrix transforms.
Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx[^].
This is easy to do and fun to try, not as boring and error-prone as your trivial ad-hoc linear algebra operations in linear metrics space. Someone else already done most of the job. :-)
—SA
这篇关于在Picturebox中旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文