旋转图片框中的图像 [英] Rotate an image in a picturebox

查看:63
本文介绍了旋转图片框中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转图片框中的图像,然后将其添加到面板中(这样我就可以在面板上放置多张图像).问题在于它总是使图像(图像周围的区域)变大.

我发现我的代码可以旋转

I want to rotate an image in a picturebox then add it to a panel (so I can put multiple images on the panel). The problem is it always makes the image (area around the image) larger.

My code I found to rotate

Dim bm_in As New Bitmap(PictureBox1.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.
        PictureBox1.Image = bm_out

推荐答案

使用Panel代替PictureBox,然后将图像添加到Panel.覆盖面板的Paint事件处理程序,并使用e.Graphics.RotateTransform方法旋转图像.
Use a Panel instead of a PictureBox, and then add images to the Panel. Override the panel''s Paint event handler, and use the e.Graphics.RotateTransform method to rotate the image.


这是滥用PictureBox的另一个示例.此类不添加任何功能,只会增加麻烦和不必要的资源消耗.
我试图解释过去的解决方案如何清除旧图纸中的面板 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子窗体之间画线 [在面板上捕获图形 [
This is another example of misuse of PictureBox. This class does not add any functionality, will only add hassles and unwanted resource consumption.
I tried to explain what to do in my past solution How do I clear a panel from old drawing[^].

See also the following answer explaining some of the required techniques:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

—SA


这篇关于旋转图片框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆