# Rotating an image (45°)

Rotating an image (watch the white rect with a small blue rect below), my image goes out of view (mostly), far more than what GIMP do (image below). The yellow part is… outside of the real image (was grey, but was a bit misleading).

The example in the documentation is useless.

I checked the release notes, found the Feature request (this one have nice artwork Xojo: Account Login).

I’ve created a method that receive the Angle value for the rotation in °.
But I do not know how to get the result width and height to create a larger Picture to draw the rotated image into (so I will not loose parts of the rotated image).

I also want to be able to set the rotated image “centered”, but how do I do that (too) ?

In GIMP, I get that (for example) when asking for a 45° rotation:

For a 90° rotation, the “out of the square part" is missing (I get only a square from my rectangle).

I wanted to rotate the blue rectangle using LibreOffice, but… I do not found how to rotate it

Ideas ?

NB: with Preview (macOS), the rotation is correct (for 90°, L and R rotations).

Oh ! Yes, I searched the net, but I only get bits rotations and crypted code.

So you want to make this in Xojo, right? (you didn’t specify)

You have several possibilities to end up with a picture of the correct size:
• Use the MBS plugin which has Picture.RotateMBS. This function returns a new picture with a size covering the entire result.
• Compute the new size the rotated picture will take (it’s just geometry; sinus, cosinus, pythagorean theorem). Then make a new picture with this size and draw the rotated picture there.
• Have a large picture with width and height being equal to the biggest size (width or height of the source, so you have a squared picture) and you draw there.

Solution 1 is costly, solution 2 may be resource intensive (you create a picture each time you rotate a given picture) and solution 3 uses picture with unnecessary wasted space; beyond that, surely one fits your needs and the undesirable effects would be mostly irrelevant if you choose the best for you.

It depends on your source pictures. Normally, you’d either do one of these:
• if you draw a picture into another one, use Graphics.DrawPicture with x=BackPicture.Width/2-PictureToAdd.Width/2 and y=BackPicture.Height/2-PictureToAdd.Height/2.
• If you draw it yourself without a buffering picture, you have to compute yourself, considering you know the width and height of what you draw.

I can’t say more without knowing how and where you draw.

Thank you Arnaud.

I will let some days off (on this) and come back with “new ideas” (I hope). Things goes wrong today. Maybe a new good day comes in the week…

For that maybe it can be of help my last blog post (sorry, by now… only in Spanish). You can find it here.

Thank you Javier.

basic rotate about a center point (center means x=0,y=0)
https://www.codegrepper.com/code-examples/whatever/rotate+2d+vector+by+angle
at sin/cos/math methods you have to look if they use radian or degree.
1° × π/180 = 0.01745 rad or = 22.0 / 7.0 / 180.0

Hello Emile,
I like to use the 2D pixmapshape shape
I can easily rotate and enlarge them
and draw anywhere with graphics.drawobject

look at the example

https://www.dropbox.com/s/ovxrmljcunmw8lo/Picture-%20rotation.xojo_binary_project?dl=1

Have-you looked at Javier Blog page where he do Rotate ?

He uses:
g.Translate
g.Rotate

For standard Picture.

Graphics.Translate
Graphics.Rotate

no trig to use (no sin nor cos involver, excepted, maybe under the hood).

But I do not know how to get the result width and height to create a larger Picture to

i mention it to calculate new size from rotated vectors.

With a rotation of 45 degrees, parts of the image are outside of the image in order to fully display the image sizes, I reduce with scale
the picture

• here when saving to an empty image Object so that the background
is transparent.

Var f As FolderItem
Var neu As New picture(px.Width,px.height)
f = FolderItem.ShowsaveFileDialog(“”,“Mein Bild.png”)
If f <> Nil Then
px.scale = Textscalepx.Text.Val/100 // px.scale 80% passt bei 45°
neu.Graphics.drawobject(px,px.Width/2+centerx.Text.Val,px.height/2+centery.Text.Val)
neu.save(f,picture.SaveAsPNG)

MessageBox "Bild gespeichert "+EndOfLine+f.NativePath

End If

Thank you for your answer, but I do not want to resize the image.

Look far above the image with a false (yellow) background: I show exactly that: a rotated image in the same area goes out of the original boundaries.