0

I have problem that image is getting cropped when I execute a function there are images which rotation do and my code:

Cropped1 Cropped2 Cropped3 Cropped4

I copied this code from StackOverflow and searched for this quite a long time, so some help would be good I don't know why is cutting the image at every rotation.

public static Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle, bool bNoClip)
{
    int W, H, X, Y;
    if (bNoClip)
    {
        double dW = (double)image.Width;
        double dH = (double)image.Height;

        double degrees = Math.Abs(angle);
        if (degrees <= 90)
        {
            double radians = 0.0174532925 * degrees;
            double dSin = Math.Sin(radians);
            double dCos = Math.Cos(radians);
            W = (int)(dH * dSin + dW * dCos);
            H = (int)(dW * dSin + dH * dCos);
            X = (W - image.Width) / 2;
            Y = (H - image.Height) / 2;
        }
        else
        {
            degrees -= 90;
            double radians = 0.0174532925 * degrees;
            double dSin = Math.Sin(radians);
            double dCos = Math.Cos(radians);
            W = (int)(dW * dSin + dH * dCos);
            H = (int)(dH * dSin + dW * dCos);
            X = (W - image.Width) / 2;
            Y = (H - image.Height) / 2;
        }
    }
    else
    {
        W = image.Width;
        H = image.Height;
        X = 0;
        Y = 0;
    }

    //create a new empty bitmap to hold rotated image
    Bitmap bmpRet = new Bitmap(W, H);
    bmpRet.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    //make a graphics object from the empty bitmap
    Graphics g = Graphics.FromImage(bmpRet);

    //Put the rotation point in the "center" of the image
    g.TranslateTransform(rotateAtX + X, rotateAtY + Y);

    //rotate the image
    g.RotateTransform(angle);

    //move the image back
    g.TranslateTransform(-rotateAtX - X, -rotateAtY - Y);

    //draw passed in image onto graphics object
    g.DrawImage(image, new PointF(0 + X, 0 + Y));
    return bmpRet;
}

public static Bitmap RotateImage(Image image, float angle)
{
    // center of the image
    float rotateAtX = image.Width / 2;
    float rotateAtY = image.Height / 2;
    bool bNoClip = false;
    return RotateImage(image, rotateAtX, rotateAtY, angle, bNoClip);
}

public static Bitmap RotateImage(Image image, float angle, bool bNoClip)
{
    // center of the image
    float rotateAtX = image.Width / 2;
    float rotateAtY = image.Height / 2;
    return RotateImage(image, rotateAtX, rotateAtY, angle, bNoClip);
}



private void DOWN_Click(object sender, EventArgs e)
{
    locomotive.Image = RotateImage(locomotive.Image, 35);
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • I suggest you use ctrl-S - the snippet tool - which lets you capture the part of the screen which shows just your problem, rather than your whole desktop, and edit your question to include those images.. – stuartd Jul 20 '18 at 00:21
  • 1
    Why would you post such huge screenshots when the area of interest is so small? – jmcilhinney Jul 20 '18 at 00:21
  • Maybe because, from you `Button.Click()`, your are calling the `RotateBitmap()` overload without the `bNoClip` parameter. Use it like this instead: `RotateBitmap(locomotive.Image, 35, true)`. – Jimi Jul 20 '18 at 02:17

0 Answers0