Graphics.RotateTransform()不会旋转我的图片 [英] Graphics.RotateTransform() does not rotate my picture

查看:366
本文介绍了Graphics.RotateTransform()不会旋转我的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用以下代码的Graphics.RotateTransfrom()时遇到问题:

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

    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

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

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

推荐答案

当前的问题是RotateTransform方法不适用于现有图像.

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

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

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

尝试以下方法:

    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.

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

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 :

    ' 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))

但是要小心旋转,您会发现需要更改绘制图像的坐标(或者更改图形的RenderingOrigin属性,将其设置为图像的中心,这样更易​​于处理旋转) ,否则您的图片将不可见(将被绘制,但会偏离图形的可见部分).

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).

希望有帮助

这篇关于Graphics.RotateTransform()不会旋转我的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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