为了账号安全,请及时绑定邮箱和手机立即绑定

如何在WinForms中旋转图片

如何在WinForms中旋转图片

C#
森栏 2019-09-24 16:16:37
我想在我的应用程序中有一幅图片,可以旋转以指示风向等方向。甚至是时间。我用什么代码旋转图片?谢谢更新:我正在使用.NET 2.0,Windows 2000,VS C#2005
查看完整描述

3 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

您可以使用以下方法在C#中旋转图像:


/// <summary>

/// method to rotate an image either clockwise or counter-clockwise

/// </summary>

/// <param name="img">the image to be rotated</param>

/// <param name="rotationAngle">the angle (in degrees).

/// NOTE: 

/// Positive values will rotate clockwise

/// negative values will rotate counter-clockwise

/// </param>

/// <returns></returns>

public static Image RotateImage(Image img, float rotationAngle)

{

    //create an empty Bitmap image

    Bitmap bmp = new Bitmap(img.Width, img.Height);


    //turn the Bitmap into a Graphics object

    Graphics gfx = Graphics.FromImage(bmp);


    //now we set the rotation point to the center of our image

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


    //now rotate the image

    gfx.RotateTransform(rotationAngle);


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


    //set the InterpolationMode to HighQualityBicubic so to ensure a high

    //quality image once it is transformed to the specified size

    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;


    //now draw our new image onto the graphics object

    gfx.DrawImage(img, new Point(0, 0));


    //dispose of our Graphics object

    gfx.Dispose();


    //return the image

    return bmp;

}


查看完整回答
反对 回复 2019-09-24
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

这是一个旧线程,还有其他几个有关C#WinForms图像旋转的线程,但是现在我想出了解决方案,我认为这是发布它的好地方。


  /// <summary>

  /// Method to rotate an Image object. The result can be one of three cases:

  /// - upsizeOk = true: output image will be larger than the input, and no clipping occurs 

  /// - upsizeOk = false & clipOk = true: output same size as input, clipping occurs

  /// - upsizeOk = false & clipOk = false: output same size as input, image reduced, no clipping

  /// 

  /// A background color must be specified, and this color will fill the edges that are not 

  /// occupied by the rotated image. If color = transparent the output image will be 32-bit, 

  /// otherwise the output image will be 24-bit.

  /// 

  /// Note that this method always returns a new Bitmap object, even if rotation is zero - in 

  /// which case the returned object is a clone of the input object. 

  /// </summary>

  /// <param name="inputImage">input Image object, is not modified</param>

  /// <param name="angleDegrees">angle of rotation, in degrees</param>

  /// <param name="upsizeOk">see comments above</param>

  /// <param name="clipOk">see comments above, not used if upsizeOk = true</param>

  /// <param name="backgroundColor">color to fill exposed parts of the background</param>

  /// <returns>new Bitmap object, may be larger than input image</returns>

  public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk, 

                                   bool clipOk, Color backgroundColor)

  {

     // Test for zero rotation and return a clone of the input image

     if (angleDegrees == 0f)

        return (Bitmap)inputImage.Clone();


     // Set up old and new image dimensions, assuming upsizing not wanted and clipping OK

     int oldWidth = inputImage.Width;

     int oldHeight = inputImage.Height;

     int newWidth = oldWidth;

     int newHeight = oldHeight;

     float scaleFactor = 1f;


     // If upsizing wanted or clipping not OK calculate the size of the resulting bitmap

     if (upsizeOk || !clipOk)

     {

        double angleRadians = angleDegrees * Math.PI / 180d;


        double cos = Math.Abs(Math.Cos(angleRadians));

        double sin = Math.Abs(Math.Sin(angleRadians));

        newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);

        newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);

     }


     // If upsizing not wanted and clipping not OK need a scaling factor

     if (!upsizeOk && !clipOk)

     {

        scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);

        newWidth = oldWidth;

        newHeight = oldHeight;

     }


     // Create the new bitmap object. If background color is transparent it must be 32-bit, 

     //  otherwise 24-bit is good enough.

     Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ? 

                                      PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);

     newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);


     // Create the Graphics object that does the work

     using (Graphics graphicsObject = Graphics.FromImage(newBitmap))

     {

        graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;

        graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;

        graphicsObject.SmoothingMode = SmoothingMode.HighQuality;


        // Fill in the specified background color if necessary

        if (backgroundColor != Color.Transparent)

           graphicsObject.Clear(backgroundColor);


        // Set up the built-in transformation matrix to do the rotation and maybe scaling

        graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);


        if (scaleFactor != 1f)

           graphicsObject.ScaleTransform(scaleFactor, scaleFactor);


        graphicsObject.RotateTransform(angleDegrees);

        graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);


        // Draw the result 

        graphicsObject.DrawImage(inputImage, 0, 0);

     }


     return newBitmap;

  }

这是从StackOverflow和其他地方获得许多灵感的结果。Naveen在此线程上的回答特别有用。


查看完整回答
反对 回复 2019-09-24
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

简单方法:


public Image RotateImage(Image img)

{

    var bmp = new Bitmap(img);


    using (Graphics gfx = Graphics.FromImage(bmp))

    {

        gfx.Clear(Color.White);

        gfx.DrawImage(img, 0, 0, img.Width, img.Height);

    }


    bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);

    return bmp;

}


查看完整回答
反对 回复 2019-09-24
  • 3 回答
  • 0 关注
  • 432 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信