我需要对任何选定的图片框进行此操作 [英] I need to make this work for any selected picturebox
本文介绍了我需要对任何选定的图片框进行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码可以旋转图片框.如何使此功能适用于任何选定的图片框?
I have this code to rotate a picturebox. How can I make this work for any selected picturebox?
Dim bm_in As New Bitmap(PictureBox9.Image)
' Make an array of points defining the
' image's corners.
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)}
' Translate to center the bounding box at the origin.
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
' Rotate.
Dim theta As Single = Single.Parse(5) * 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
' Translate so X >= 0 and Y >=0 for all corners.
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
' Create an output Bitmap and Graphics object.
Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
Dim gr_out As Graphics = Graphics.FromImage(bm_out)
' Drop the last corner lest we confuse DrawImage,
' which expects an array of three corners.
ReDim Preserve corners(2)
' Draw the result onto the output Bitmap.
gr_out.DrawImage(bm_in, corners)
' Display the result.
PictureBox9.Image = bm_out
推荐答案
您不旋转图片框,而是旋转图片框中显示的位图.您所要做的就是在返回位图的函数中编写此代码,而不是在图片框中显示结果...".
当结果返回到被调用时,只需将PictureBox.Image属性设置为它.
You''re not rotating a picturebox, you''re rotating the bitmap displayed in the picturebox. All you have to do is wrpa this code in a Function that returns a Bitmap instead of "Display the result...in a picturebox".
When the result is returned back to the called, you just set the PictureBox.Image property to it.
它有什么问题?
第一行是您需要处理的行.
Dim bm_in As New Bitmap(PictureBox9.Image)
代替固定的PictureBox9
,您需要使用选择的那个.
您可以将上面的代码作为一种将picturebox作为参数并只传递所选参数的方法.
Whats the issue with it?
The first line is the line where you need to take care of it.
Dim bm_in As New Bitmap(PictureBox9.Image)
Instead of a fixedPictureBox9
, you need to use the one that is selected.
You can make the above code as a method that takes picturebox as a parameter and just pass the one that is selected.
将代码放入自己的函数中,该函数接受PictureBox作为范围.用参数替换代码中的"PictureBox9".无论何时需要进行旋转,都可以调用该函数,并传递需要旋转其图像的PictureBox.
Put the code in a function of its own that accepts a PictureBox as a parameter. Replace "PictureBox9" in your code with the parameter. Wherever you need to do this rotation, call the function, passing the PictureBox whose image needs to be rotated.
这篇关于我需要对任何选定的图片框进行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文