Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some help with a vb.net project. to rotate images and scale so that the entire image fills the image.

I have tried many solutions to no avail

Does anyone have a good solution, images must be able to be rotated 180 both ways

What I have tried:

VB
Sub RotateAndZoomImage(loadfile As String, savefile As String, angle As Integer)
     Dim Image As New System.Drawing.Bitmap(loadfile)
     ' Opret en tom bitmap med den ønskede størrelse
     Dim targetWidth As Integer = Image.Width
     Dim scale2 As Double
     Dim targetHeight As Integer = Image.Height




     Dim rotatedBmp As New Bitmap(targetWidth, targetHeight)
         rotatedBmp.SetResolution(Image.HorizontalResolution, Image.VerticalResolution)

         ' Opret en grafisk kontekst
         Using g As Graphics = Graphics.FromImage(rotatedBmp)
             ' Indstil grafiske parametre
             g.InterpolationMode = InterpolationMode.HighQualityBicubic
             g.PixelOffsetMode = PixelOffsetMode.HighQuality
             g.SmoothingMode = SmoothingMode.AntiAlias

         ' Flyt origin til midten af den nye bitmap
         g.TranslateTransform(targetWidth / scale2, targetHeight / scale2)

         ' Roter grafikken
         g.RotateTransform(angle)

             ' Beregn skalering for at tilpasse billedet til den nye størrelse
             Dim scale As Single = Math.Max(targetWidth / Image.Width, targetHeight / Image.Height)
             g.ScaleTransform(scale, scale)

         ' Tegn det roterede og skalerede billede
         g.DrawImage(Image, -CSng(Image.Width), -CSng(Image.Height), CSng(Image.Width * scale2), CSng(Image.Height * scale2))
     End Using
         rotatedBmp.Save(savefile, ImageFormat.Jpeg)
         rotatedBmp.Dispose()
         Image.Dispose()
 End Sub
Posted
Updated 24-Jun-24 4:18am
v2

1 solution

Try setting scale2 to a value: 2.0 would be a good start since you want the image centre!

It's probably a good idea to switch from Double to Single since the Graphics methods you are calling take them.
 
Share this answer
 
v2
Comments
charles henington 12-Jul-24 13:57pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900