Home > Backend Development > C++ > How to Rotate Images in WinForms?

How to Rotate Images in WinForms?

Linda Hamilton
Release: 2025-01-10 11:57:43
Original
359 people have browsed it

How to Rotate Images in WinForms?

Detailed explanation of WinForms image rotation

Rotating images in WinForms applications is a practical technique for creating dynamic user interfaces and data visualizations. This article takes an in-depth look at how to rotate images in WinForms.

Use the RotateImage method

To rotate an image in WinForms, you can use the RotateImage method, which accepts two parameters:

  1. img: The image to be rotated.
  2. rotationAngle: The angle (in degrees) by which the image should be rotated. Positive values ​​rotate clockwise, negative values ​​rotate counterclockwise.

Code implementation

The following code demonstrates how to rotate an image 90 degrees clockwise:

<code class="language-csharp">// 获取要旋转的图像。
Image image = Image.FromFile("image.png");

// 旋转图像。
Image rotatedImage = RotateImage(image, 90);

// 显示旋转后的图像。
pictureBox.Image = rotatedImage;</code>
Copy after login

RotateImage method definition

The implementation of the RotateImage method is as follows:

<code class="language-csharp">public static Image RotateImage(Image img, float rotationAngle)
{
    // 创建一个空的位图图像。
    Bitmap bmp = new Bitmap(img.Width, img.Height);

    // 将位图转换为图形对象。
    Graphics gfx = Graphics.FromImage(bmp);

    // 将旋转点设置为图像的中心。
    gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);

    // 旋转图像。
    gfx.RotateTransform(rotationAngle);

    gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);

    // 将InterpolationMode设置为HighQualityBicubic以进行高质量的图像转换。
    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

    // 将新图像绘制到图形对象上。
    gfx.DrawImage(img, new Point(0, 0));

    // 释放图形对象。
    gfx.Dispose();

    // 返回旋转后的图像。
    return bmp;
}</code>
Copy after login

By using the RotateImage method, you can easily rotate images in your WinForms application to suit your specific needs.

The above is the detailed content of How to Rotate Images in WinForms?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template