vb.net Graphics.RotateTransform() 不旋转我的图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13579409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:19:44  来源:igfitidea点击:

Graphics.RotateTransform() does not rotate my picture

vb.netrotationtransformpicturebox

提问by Tom Konski

I have problem with Graphics.RotateTransfrom() with the following code :

我对 Graphics.RotateTransfrom() 有问题,代码如下:

    Dim newimage As Bitmap
    newimage = System.Drawing.Image.FromFile("C:\z.jpg")
    Dim gr As Graphics = Graphics.FromImage(newimage)
    Dim myFontLabels As New Font("Arial", 10)
    Dim myBrushLabels As New SolidBrush(Color.Black)
    Dim a As String

    '# last 2 number are X and Y coords.
    gr.DrawString(MaskedTextBox2.Text * 1000 + 250, myFontLabels, myBrushLabels, 1146, 240) 
    gr.DrawString(MaskedTextBox2.Text * 1000, myFontLabels, myBrushLabels, 1146, 290)
    a = Replace(Label26.Text, "[ mm ]", "")

    gr.DrawString(a, myFontLabels, myBrushLabels, 620, 1509)
    a = Replace(Label5.Text, "[ mm ]", "")

    gr.DrawString(a, myFontLabels, myBrushLabels, 624, 548)

    gr.RotateTransform(90.0F)

    gr.DrawString(a, myFontLabels, myBrushLabels, 0, 0)

    PictureBox1.Image = newimage

I dont know why but my image in pictureBox1 is not rotated. Someone known solution ?

我不知道为什么,但我在pictureBox1 中的图像没有旋转。有人知道解决方案吗?

回答by T. Fabre

The issue at hand is that the RotateTransform method does not apply to the existing image.

手头的问题是 RotateTransform 方法不适用于现有图像。

Instead, it applies to the transformation matrix of the graphics object. Basically, the transformation matrix modifies the coordinate system used to add new items.

相反,它适用于图形对象的变换矩阵。基本上,变换矩阵会修改用于添加新项目的坐标系。

Try the following :

尝试以下操作:

    Dim gfx = Graphics.FromImage(PictureBox1.Image)

    gfx.DrawString("Test", Me.Font, Brushes.Red, New PointF(10, 10))
    gfx.RotateTransform(45)
    gfx.DrawString("Rotate", Me.Font, Brushes.Red, New PointF(10, 10))

The first string is drawn normally, while the second is drawn rotated.

第一个字符串是正常绘制的,而第二个字符串是旋转绘制的。

So what you need to do is create a new graphics object, apply your rotation, draw your source image onto the graphics (graphics.DrawImage), and then draw all your text :

所以你需要做的是创建一个新的图形对象,应用你的旋转,将你的源图像绘制到图形上(graphics.DrawImage),然后绘制所有文本:

    ' Easy way to create a graphisc object
    Dim gfx = Graphics.FromImage(PictureBox1.Image)

    gfx.Clear(Color.Black)
    gfx.RotateTransform(90) ' Rotate by 90°

    gfx.DrawImage(Image.FromFile("whatever.jpg"), New PointF(0, 0))
    gfx.DrawString("Test", Me.Font, Brushes.Red, New PointF(10, 10))
    gfx.DrawString("Rotate", Me.Font, Brushes.Red, New PointF(10, 10))

But beware of rotation, you'll find that you need to change the coordinates at which you draw your image (Or change the RenderingOrigin property of the graphics, setting it to the center of the image makes it easier to handle rotations), otherwise your picture won't be visible (it will be drawn, but off the visible part of the graphics).

但是要注意旋转,您会发现需要更改绘制图像的坐标(或者更改图形的 RenderingOrigin 属性,将其设置为图像的中心可以更轻松地处理旋转),否则您的图片将不可见(它将被绘制,但不在图形的可见部分)。

Hope that helps

希望有帮助