|
|
|
This example shows how to draw an image rotated 90 degrees
clockwise. |
|
|
|
|
|
|
We will need source and destinatino bitmaps so we create them
here.
[C#]
using (Image image = Bitmap.FromFile(Server.MapPath("rez/laurentiu-iordache-qstGMhWuORE-unsplash.jpg")))
// NB horizontal and vertical measurements are swapped because we are rotating by 90 degrees
using (Bitmap bitmap = new Bitmap(image.Height, image.Width)) {
bitmap.SetResolution(image.VerticalResolution, image.HorizontalResolution);
|
|
|
|
|
|
Draw the image rotated 90 degrees. Because we rotate around the
origin we will lose the image off the edge of our bitmap unless we
translate the image back onto the bitmap.
[C#]
using (Graphics graphics = Graphics.FromImage(bitmap)) {
graphics.TranslateTransform(image.Height, 0);
graphics.RotateTransform(90);
graphics.DrawImage(image, image.Bounds);
}
|
|
|
|
|
|
Finally we save the picture as a medium quality JPEG image.
[C#]
EncoderParameters pars = new EncoderParameters() { Quality = 50 };
bitmap.Save(Server.MapPath("Rotating_an_Image_3.jpg"), ImageFormat.Jpeg, pars);
}
|
|
|
|
|
|
Sample input and output images are shown below.

laurentiu-iordache-qstGMhWuORE-unsplash.jpg

Rotating_an_Image_3.jpg
|