缩放和旋转图像并将其放置在具有新尺寸的图片框控件中
我正在尝试将图像缩放并旋转,然后将其放回到图片框控件中,但我需要将图片框调整为新图像并在那里遇到一点麻烦 在做了一堆搜索之后,我用
结束了下面的代码,但是返回的图像与原始图像具有相同的尺寸,导致裁剪(当旋转或缩放图像时更大)或图像周围的空白间距过大(缩小图像)。这是
我第一次处理这种性质的图像处理,所以我假设我无法理解某个地方的概念,所以如果有人可以帮我解决这个问题那就太棒了。
Hi, I'm trying to work on getting an image to scale and rotate and then place it back in a picturebox control, but I need to resize the picturebox to the new image and running into a little trouble there. After doing a bunch of searching around, I've ended up with the below code, but the image that's being returned is of the same exact dimensions as the original, causing cropping (when rotating or scaling the image bigger) or excessive blank spacing around the image (scaling the image smaller). This is my first time dealing with image manipulation of this nature, so I'm assuming I'm failing to understand a concept somewhere, so if someone could help me figure it out that would be great.
我从这里获得了我正在使用的代码( https://stackoverflow.com/questions/40431154/rotating -an-image-in-a-box-box)
并且在我找到ScaleTransform方法时适应包括缩放。
I obtained the code I'm using from here (https://stackoverflow.com/questions/40431154/rotating-an-image-in-a-picture-box) and adapted to include the scaling when I found the ScaleTransform method.
应注意这是在VB.Net,使用Visual Studio 2012。
Should note this is in VB.Net, using Visual Studio 2012.
Private Function ScaleAndRotateImage(img As Image, scalefactor As Double, angle As Integer) As Bitmap
Dim retBMP As New Bitmap(img.Width, img.Height)
retBMP.SetResolution(img.HorizontalResolution, img.VerticalResolution)
Using g = Graphics.FromImage(retBMP)
g.TranslateTransform(img.Width \ 2, img.Height \ 2)
g.ScaleTransform(scalefactor, scalefactor)
g.RotateTransform(angle)
g.TranslateTransform(-img.Width \ 2, -img.Height \ 2)
g.DrawImage(img, New PointF(0, 0))
End Using
Return retBMP
End Function
谢谢!
编辑:
所以仍在努力解决这个问题,并尝试了以下代码更改。 这似乎有助于缩放,但我不知道是否有更好的方法? 现在需要"修复"因此它不会裁剪角落。
So still working on this and just tried out the below code changes. This appears to help scaling, though I don't know if there's a better way? Now need to "fix" rotation so it doesn't crop the corners.
Private Function ScaleAndRotateImage(img As Image, scalefactor As Double, angle As Integer) As Bitmap
Dim retBMP As New Bitmap(CInt(img.Width * scalefactor), CInt(img.Height * scalefactor))
retBMP.SetResolution(img.HorizontalResolution * scalefactor, img.VerticalResolution * scalefactor)
Using g = Graphics.FromImage(retBMP)
g.TranslateTransform(retBMP.Width \ 2, retBMP.Height \ 2)
'g.ScaleTransform(scalefactor, scalefactor)
g.RotateTransform(angle)
g.TranslateTransform(-retBMP.Width \ 2, -retBMP.Height \ 2)
g.DrawImage(img, New PointF(0, 0))
End Using
Return retBMP
End Function
请准确显示您的意思的图像。例如,这是我从这段代码中得到的。这就是我想要的。
Please show images of exactly what you mean. For example, this is what I get from this code. This is what I want to get.
如果你在一个方孔中旋转一个方形钉,那么角就会碰到(被裁剪)。因此,您必须减小图片大小或增加图片大小等等。
PS if you rotate a square peg in a square hole then the corners will hit (get cropped). So you must decrease the image size or increase picbox size or something.
首先,您必须计算减少多少。显示如何旋转图片并调整图片大小以适合图片框。
First you have to calc how much reduction. Show an image of how you want to rotate and size picture to fit into the picturebox.
例如,运行上面的第一个示例,并调用scalefactor = 1(无大小更改)scalefactor = 0.5(图像大小减半)并再次使用= 2(图像尺寸加倍)。看结果?如果没有告诉我们你得到了什么。
For example run your first example above and call with scalefactor = 1 (no size change) scalefactor = 0.5 (image size reduced by half) and again with = 2 (image size doubled). See the result? If not show us what you get.