Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:47:19+00:00 2026-06-16T18:47:19+00:00

I have done some searching and i can not find any function thats doing

  • 0

I have done some searching and i can not find any function thats doing what i whant it todo.

I have a image file of a scanned document with text, but the document is some degrees rotated so i whant to rotate it so the text being inline with each other.

In a perfect world its should be a function doing this automaticly but i can not find anything and what i understand to get it to work automaticly its needed to be some analyze of the image and i think its to big thing todo.

But then i have done a tool to rotate the image on a website manually, but now i need a function to save the rotation to the image file.

This seems to be some differents methods for but no one i tested doing what i whant.

The function i have finded that works almost like i whant is:

public static Bitmap RotateImg(Bitmap bmp, float angle, Color bkColor) {
int w = bmp.Width;
int h = bmp.Height;
PixelFormat pf = default(PixelFormat);
if (bkColor == Color.Transparent)
{
    pf = PixelFormat.Format32bppArgb;
}
else
{
    pf = bmp.PixelFormat;
}

Bitmap tempImg = new Bitmap(w, h, pf);
Graphics g = Graphics.FromImage(tempImg);
g.Clear(bkColor);
g.DrawImageUnscaled(bmp, 1, 1);
g.Dispose();

GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0f, 0f, w, h));
Matrix mtrx = new Matrix();
//Using System.Drawing.Drawing2D.Matrix class 
mtrx.Rotate(angle);
RectangleF rct = path.GetBounds(mtrx);
Bitmap newImg = new Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height), pf);
g = Graphics.FromImage(newImg);
g.Clear(bkColor);
g.TranslateTransform(-rct.X, -rct.Y);
g.RotateTransform(angle);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImageUnscaled(tempImg, 0, 0);
g.Dispose();
tempImg.Dispose();
return newImg; }

But this do not change the height and width of the image file so the image file is in the same size but the image “object” has been scaled and rotated.

Any idea how i can do this good?

Answer
I find the solution that worked with my image that has a resolution on 300 at a old answer here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:47:20+00:00Added an answer on June 16, 2026 at 6:47 pm

    If I’ve understood your question correctly, you essentially want to work out the new size of an image once rotated, and how to position the rotated image in it’s new bitmap.

    enter image description here

    The diagram hopefully helps make clear the solution. Here is a bit of pseudo code:

    sinVal = abs(sin(angle))
    cosVal = abs(cos(angle))
    newImgWidth = sinVal * oldImgHeight + cosVal * oldImgWidth
    newImgHeight = sinVal * oldImgWidth + cosVal * oldImgHeight
    originX = 0
    originY = sinVal * oldImgWidth
    

    You want to make the new image from the newImgWidth and newImgHeight, and then perform a rotation around the origin (originX, originY) and then render the image to this point. This will fall over if the angle (in degrees) isn’t between -90 and 0 degrees (depicted). If it is between 0 and 90 degrees, then you just change the origin:

    originX = sinVal * oldImgHeight
    originY = 0
    

    If it is in the range 90 degrees to 270 degrees (-90 degrees) then it is a little tricker (see example code below).

    Your code re-written (briefly tested) – it is slightly dodgy but seems to work:

    public static Bitmap RotateImg(Bitmap bmp, float angle, Color bkColor)
    {
        angle = angle % 360;
        if (angle > 180)
            angle -= 360;
    
        System.Drawing.Imaging.PixelFormat pf = default(System.Drawing.Imaging.PixelFormat);
        if (bkColor == Color.Transparent)
        {
            pf = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
        }
        else
        {
            pf = bmp.PixelFormat;
        }
    
        float sin = (float)Math.Abs(Math.Sin(angle * Math.PI / 180.0)); // this function takes radians
        float cos = (float)Math.Abs(Math.Cos(angle * Math.PI / 180.0)); // this one too
        float newImgWidth = sin * bmp.Height + cos * bmp.Width;
        float newImgHeight = sin * bmp.Width + cos * bmp.Height;
        float originX = 0f;
        float originY = 0f;
    
        if (angle > 0)
        {
            if (angle <= 90)
                originX = sin * bmp.Height;
            else
            {
                originX = newImgWidth;
                originY = newImgHeight - sin * bmp.Width;
            }
        }
        else
        {
            if (angle >= -90)
            originY = sin * bmp.Width;
            else
            {
                originX = newImgWidth - sin * bmp.Height;
                originY = newImgHeight;
            }
        }
    
        Bitmap newImg = new Bitmap((int)newImgWidth, (int)newImgHeight, pf);
        Graphics g = Graphics.FromImage(newImg);
        g.Clear(bkColor);
        g.TranslateTransform(originX, originY); // offset the origin to our calculated values
        g.RotateTransform(angle); // set up rotate
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        g.DrawImageUnscaled(bmp, 0, 0); // draw the image at 0, 0
        g.Dispose();
    
        return newImg;
    }
    

    Note the Degrees to Radians Conversion (180 Degrees == Pi Radians) for the trig functions

    Edit: big issue was negative sin values, and me getting width/height confused when calculating origin x/y – this should work fine now (tested)

    Edit: modified code to handle any angle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done some searching around but have not been able to figure out
I've done some Googling and searching on SO, but I have not been able
I have done some extensive searching for code examples on this but cannot find
I've done some searching for this but cannot find what I'm after, specifically. I
I have done some searching but I am very new to JavaScript and jQuery
Using SilverStripe 2.4.7. I've done some searching but I can't seem to be able
I've done some searching around but I have a specific question on SQL Injection
I've done some searching (maybe I'm not describing my problem well enough) but haven't
I did do some googling and searching on this site but did not find
Have done some research and found some stuff that may be helpful. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.