gpt4 book ai didi

vb.net - 如何旋转保留其原始大小的图像?

转载 作者:行者123 更新时间:2023-12-04 17:33:07 24 4
gpt4 key购买 nike

我一直在尝试旋转图像,但遇到了一些麻烦。第一个代码块可以正常工作,但它导致了不良的副作用:旋转后,图像按比例缩小,以便 x 尺寸与先前存在的 y 相匹配维度。
所以我旋转了图像,但它只占据了 Canvas 的一部分。

为了解决这个问题,我想我应该创建一个更大的位图作为中间步骤,这样当它旋转时,图像就不需要为了适应而缩小。

该代码在第二个 block 中。不幸的是,当我运行它时,我得到了一个通用的 GDI 错误。
有人知道我做错了什么吗?

作品:

Imports System.Drawing

If XSize < YSize Then 'Needs to be rotated
Dim img As Image = Image.FromFile(strFilename)

Dim b = New Bitmap(img.Height, img.Width)
Dim gr As Graphics = Graphics.FromImage(b)
img.RotateFlip(RotateFlipType.Rotate90FlipNone)
gr.DrawImage(img, New Point(0, 0))
img = Nothing
b.Save(strFilename)
End If

此代码块不起作用:

'Needs to be rotated
If XSize < YSize Then
Dim img As Image = Image.FromFile(strFilename)
Dim bmpTemp As Image

If img.Height > img.Width Then
bmpTemp = New Bitmap(img.Height, img.Height)
Else
bmpTemp = New Bitmap(img.Width, img.Width)
End If

Dim gr2 As Graphics = Graphics.FromImage(bmpTemp)
gr2.DrawImage(img, New Point(0, 0))

Dim b = New Bitmap(img.Height, img.Width)
Dim gr As Graphics = Graphics.FromImage(b)
bmpTemp.RotateFlip(RotateFlipType.Rotate90FlipNone)
gr.DrawImage(bmpTemp, New Point(0, 0))
img = Nothing
b.Save(strFilename)
End If

最佳答案

我不太清楚您想如何旋转图像。问题是旋转轴、旋转图像的新尺寸还是其他?

无论如何,据我猜测,以下代码片段围绕 Canvas 的边缘或中心点旋转图像。

Private Function RotateImage(ByVal src As Bitmap,
ByVal width As Integer,
ByVal height As Integer,
ByVal angle As Single,
Optional ByVal CenterRotation As Boolean = False) As Bitmap

Dim cornors As Point() = {New Point(0, 0),
New Point(width, 0),
New Point(width, height),
New Point(0, height)}
Using m As New Matrix
If Not CenterRotation Then
m.RotateAt(angle, New PointF(width / 2, height / 2))
m.TransformPoints(cornors)
End If

Dim left = Integer.MaxValue
Dim right = Integer.MinValue
Dim top = Integer.MaxValue
Dim bottom = Integer.MinValue

For i = 0 To UBound(cornors)
If cornors(i).X < left Then left = cornors(i).X
If cornors(i).X > right Then right = cornors(i).X
If cornors(i).Y < top Then top = cornors(i).Y
If cornors(i).Y > bottom Then bottom = cornors(i).Y
Next

Dim b As New Bitmap(right - left, bottom - top)
Dim x = (b.Width - width) / 2
Dim y = (b.Height - height) / 2

Using g As Graphics = Graphics.FromImage(b)
m.Reset()
m.RotateAt(angle, New PointF(b.Width / 2, b.Height / 2))
g.Transform = m
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.SmoothingMode = SmoothingMode.HighQuality
g.CompositingQuality = CompositingQuality.HighQuality
g.Clear(Color.Transparent)
g.DrawImage(src, New Rectangle(x, y, width, height))
End Using
Return b
End Using
End Function

如果您还需要调整旋转图像的大小以适应 Canvas ,那么您还需要在第一个代码之后添加以下代码:

Private Function CreateThumbnail(ByVal bmp As Bitmap,
ByVal canvasWidth As Integer,
ByVal canvasHeight As Integer,
Optional Stretch As Boolean = False) As Bitmap

Dim bmpOut As Bitmap = Nothing

If Stretch Then
bmpOut = bmp.GetThumbnailImage(canvasWidth, canvasHeight, Nothing, IntPtr.Zero)
Else
Dim newWidth As Integer = 0
Dim newHeight As Integer = 0

bmpOut = New Bitmap(canvasWidth, canvasHeight)

Dim ratioX As Double = CDbl(canvasWidth) / CDbl(bmp.Width)
Dim ratioY As Double = CDbl(canvasHeight) / CDbl(bmp.Height)
Dim ratio = If(ratioX < ratioY, ratioX, ratioY)

newWidth = Convert.ToInt32(bmp.Width * ratio)
newHeight = Convert.ToInt32(bmp.Height * ratio)

If newWidth > bmp.Width Then
newWidth = bmp.Width
End If
If newHeight > bmp.Height Then
newHeight = bmp.Height
End If

Dim posX = Convert.ToInt32((canvasWidth - newWidth) / 2)
Dim posY = Convert.ToInt32((canvasHeight - newHeight) / 2)

Using g As Graphics = Graphics.FromImage(bmpOut)
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.SmoothingMode = SmoothingMode.HighQuality
g.CompositingQuality = CompositingQuality.HighQuality
g.Clear(Color.Transparent)
g.DrawImage(bmp, posX, posY, newWidth, newHeight)
End Using
End If

Return bmpOut
End Function

这是一个快速演示:

Image Rotation Demo

祝你好运。

关于vb.net - 如何旋转保留其原始大小的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57858537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com