首頁 > 後端開發 > C++ > 如何在WinForms中旋轉影像?

如何在WinForms中旋轉影像?

Linda Hamilton
發布: 2025-01-10 11:57:43
原創
431 人瀏覽過

How to Rotate Images in WinForms?

WinForms 圖片旋轉詳解

在WinForms應用程式中旋轉圖片是建立動態使用者介面和資料視覺化的實用技巧。本文將深入探討如何在WinForms中旋轉影像。

使用RotateImage方法

在WinForms中旋轉影像,您可以使用RotateImage方法,該方法接受兩個參數:

  1. img:要旋轉的影像。
  2. rotationAngle:影像應旋轉的角度(以度為單位)。正值順時針旋轉,負值逆時針旋轉。

程式碼實作

以下程式碼示範如何將影像順時針旋轉90度:

// 获取要旋转的图像。
Image image = Image.FromFile("image.png");

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

// 显示旋转后的图像。
pictureBox.Image = rotatedImage;
登入後複製

RotateImage方法定義

RotateImage方法的實作如下:

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;
}
登入後複製

透過使用RotateImage方法,您可以輕鬆地在WinForms應用程式中旋轉圖像以滿足您的特定需求。

以上是如何在WinForms中旋轉影像?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板