Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Image rotation

What is the best way to rotate a image in asp.net

I did use matrix.rotateAt but i can't get it to work so please tell me what is the best way?

I should write out that hate to rotate a image with the image object.

like image 732
Broadminded Avatar asked Sep 03 '09 07:09

Broadminded


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

Image myImage = Image.FromFile("myimage.png");
myImage.RotateFlip(RotateFlipType.Rotate180FlipNone);

http://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip.aspx

like image 162
MiffTheFox Avatar answered Sep 28 '22 19:09

MiffTheFox


Here is some sample code (not written by me - found some time ago here ) that worked for me, as long as you edit some details.

private Bitmap rotateImage(Bitmap b, float angle)
{
    //create a new empty bitmap to hold rotated image
    Bitmap returnBitmap = new Bitmap(b.Width, b.Height);

    //make a graphics object from the empty bitmap
    using (Graphics g = Graphics.FromImage(returnBitmap))
    {
        //move rotation point to center of image
        g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
        //rotate
        g.RotateTransform(angle);
        //move image back
        g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
        //draw passed in image onto graphics object
        g.DrawImage(b, new Point(0, 0));
    }

    return returnBitmap;
} 

Please note, that this may not work "out of the box" - there are some issues with the new bitmap. When you rotate it, it may not fit comfortably in the rectangle of the old bitmap (rectangle bounds b.Width, B.Height).

Anyway this is just to give you an idea. If you choose to do it this way, I'm sure you will be able to work out all the details. I'd post my final code, however I don't have it on me right now...

like image 29
David Božjak Avatar answered Sep 28 '22 19:09

David Božjak