【发布时间】:2014-06-10 05:30:35
【问题描述】:
我需要将 JPEG 图像旋转 90 度,不幸的是 System.Drawing.Image.RotateFlip 生成的文件与我的最终程序不兼容。因此,作为替代方案,我正在尝试使用 Graphics.RotateTransform。当前的代码似乎应该可以工作,但是图像在旋转时会被剪裁。
This is the image 用于测试。
这是我用来旋转图像的代码:
Public Shared Sub ProcessJpeg()
Dim filePath As String = "C:\test.JPG"
Dim img As Bitmap = Nothing
img = New Bitmap(filePath)
img = RotateImage(img, 90.0F)
img.Save(Replace(filePath, ".JPG", "_new.JPG"))
End Sub
Shared Function RotateImage(b As Bitmap, angle As Single) As Bitmap
'create a new empty bitmap to hold rotated image
Dim returnBitmap As New Bitmap(b.Width, b.Height)
'make a graphics object from the empty bitmap
Dim g As Graphics = Graphics.FromImage(returnBitmap)
'move rotation point to center of image
g.TranslateTransform(CSng(b.Width) / 2, CSng(b.Height) / 2)
'rotate
g.RotateTransform(angle)
'move image back
g.TranslateTransform(-CSng(b.Width) / 2, -CSng(b.Height) / 2)
'draw passed in image onto graphics object
g.DrawImage(b, New Rectangle(New Point(0, 0), New Size(b.Width, b.Height)))
Return returnBitmap
End Function
RotateImage 函数来自here。
我认为解决方案是在 RotateImage 函数的倒数第三行简单地反转 b.Width 和 b.Height,但这只会让事情变得更糟。
或者,如果有更好的方法来旋转 JPEG(除了 System.Drawing.Image.RotateFlip),我很想听听。
【问题讨论】:
-
not compatible with my end program是什么意思? -
我的软件导出 CAD 模型的 JPEG 图像并将图像旋转 90 度(根据客户说明)。然后,客户将 JPEG 导入单独的 ERP 程序,该程序将 JPEG 放入 PDF 报告中。由于某些让我们都难过的奇怪原因,如果使用 System.Drawing.RotateFlip 旋转了图像,则图像不会显示在 PDF 中。所以我的任务是找出另一种旋转图像的方法。
-
请注意:您的保存方法未指定 jpeg 格式。
-
我假设您是在暗示 .jpeg 和 .jpg 是不同的文件格式?如果是这种情况,请纠正我。在我的研究中,我了解到 .jpeg 和 .jpg 是可以互换的。
-
你最终是对的,LarsTech。这就是问题所在。谢谢。