Skip to content

Krakkus.com

Electronics

Menu
  • Home
  • Posts
Menu

How to rotate an image in C#

Posted on November 26, 2022
  1. Calculate SIN and COS once
  2. Apply these in a formula to all pixels
  3. Do this in reverse to avoid gaps in the final image

Rotation-matrix

To rotate a pixel around an axis, we need to do some sinus and co-sinus calculation to it because, in short, rotation follows a circular path.

The good part is that the sinus and co-sinus part of the calculation remains the same for every pixel, because they all rotate the same amount.

The next code sets up the values needed later in the main loop. And C#’s math functions want to see Radians, not degrees.

double rotationRadians = userRotate * (Math.PI / 180);

float sin = (float) Math.Cos(rotationRadians);
float cos = (float) Math.Sin(rotationRadians);

In reverse

If we where to go over every pixel in the original image and calculate it’s new position, then there would be gaps in the destination image.

Therefore, we go over every pixel in the destination image, and calculate back where its original position is, that way we always have a pixel color when within the image’s border.

for (x=rectangleLeft; x<rectangleRight; x++)
{
    for (y=rectangleTop; y<rectangleBottom; y++)
    {
        // Rotate
        xx = x * cos - y * sin;
        yy = x * sin + y * cos;

        // Check if we are within the original image
        if (xx > 0 && xx < imageWidth && yy>0 && yy < imageHeight)
        {
	    c = lockBitmap.GetPixel((int) xx, (int) yy);
	    c.A = 255;
	    destination.SetPixel(x, y, c);
        }
        else
        {
	    destination.SetPixel(x, y, MyColor.Black);
        }
    }
}

As you can see in the code, we loop over every pixel, apply the same sin and co-sinus values to it and end up with a source pixel-coordinate xx and yy.

Then we get the color of the pixel or assign a background color if not within the bounds of the original.

An image rotated with C# using a rotation matrix. Resulting image has no gaps.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Other Posts

  • How to create a power limiter to get zero-on-the-meter to save taxes

    How to create a power limiter to get zero-on-the-meter to save taxes

    If you are one of the many people exporting electricity to the grid, chances are you are familiar with the following… When you export electricity you also get to pay energy taxes...
  • How to create a smart switch for an electric boiler to save money

    How to create a smart switch for an electric boiler to save money

    My electric water heater consumes a lot of electricity, and it does so at times when it is most expensive. This is because I have dynamic prices which change every hour and...
  • Electromagnetism

    Electromagnetism

    Electromagnetism is the branch of physics that deals with the interaction between electrically charged particles and the electromagnetic force. This force is one of the four fundamental forces of nature, along with...
  • What is green power?

    What is green power?

    Green power refers to electricity generated from environmentally friendly and renewable sources such as solar, wind, geothermal, hydro, and bio-energy. These energy sources do not produce harmful pollutants or greenhouse gases and...
  • What is electrical power?

    What is electrical power?

    Electrical power is the rate at which electrical energy is transferred by an electric circuit. It is typically measured in watts (W) or kilowatts (kW). The amount of power used by an...
©2023 Krakkus.com | Design: Newspaperly WordPress Theme