猿问

如何在WinForms中旋转图片

我想在我的应用程序中有一幅图片,可以旋转以指示风向等方向。甚至是时间。我用什么代码旋转图片?谢谢

更新:我正在使用.NET 2.0,Windows 2000,VS C#2005


森栏
浏览 460回答 3
3回答

胡说叔叔

您可以使用以下方法在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:&nbsp;/// Positive values will rotate clockwise/// negative values will rotate counter-clockwise/// </param>/// <returns></returns>public static Image RotateImage(Image img, float rotationAngle){&nbsp; &nbsp; //create an empty Bitmap image&nbsp; &nbsp; Bitmap bmp = new Bitmap(img.Width, img.Height);&nbsp; &nbsp; //turn the Bitmap into a Graphics object&nbsp; &nbsp; Graphics gfx = Graphics.FromImage(bmp);&nbsp; &nbsp; //now we set the rotation point to the center of our image&nbsp; &nbsp; gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);&nbsp; &nbsp; //now rotate the image&nbsp; &nbsp; gfx.RotateTransform(rotationAngle);&nbsp; &nbsp; gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);&nbsp; &nbsp; //set the InterpolationMode to HighQualityBicubic so to ensure a high&nbsp; &nbsp; //quality image once it is transformed to the specified size&nbsp; &nbsp; gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;&nbsp; &nbsp; //now draw our new image onto the graphics object&nbsp; &nbsp; gfx.DrawImage(img, new Point(0, 0));&nbsp; &nbsp; //dispose of our Graphics object&nbsp; &nbsp; gfx.Dispose();&nbsp; &nbsp; //return the image&nbsp; &nbsp; return bmp;}

幕布斯7119047

这是一个旧线程,还有其他几个有关C#WinForms图像旋转的线程,但是现在我想出了解决方案,我认为这是发布它的好地方。&nbsp; /// <summary>&nbsp; /// Method to rotate an Image object. The result can be one of three cases:&nbsp; /// - upsizeOk = true: output image will be larger than the input, and no clipping occurs&nbsp;&nbsp; /// - upsizeOk = false & clipOk = true: output same size as input, clipping occurs&nbsp; /// - upsizeOk = false & clipOk = false: output same size as input, image reduced, no clipping&nbsp; ///&nbsp;&nbsp; /// A background color must be specified, and this color will fill the edges that are not&nbsp;&nbsp; /// occupied by the rotated image. If color = transparent the output image will be 32-bit,&nbsp;&nbsp; /// otherwise the output image will be 24-bit.&nbsp; ///&nbsp;&nbsp; /// Note that this method always returns a new Bitmap object, even if rotation is zero - in&nbsp;&nbsp; /// which case the returned object is a clone of the input object.&nbsp;&nbsp; /// </summary>&nbsp; /// <param name="inputImage">input Image object, is not modified</param>&nbsp; /// <param name="angleDegrees">angle of rotation, in degrees</param>&nbsp; /// <param name="upsizeOk">see comments above</param>&nbsp; /// <param name="clipOk">see comments above, not used if upsizeOk = true</param>&nbsp; /// <param name="backgroundColor">color to fill exposed parts of the background</param>&nbsp; /// <returns>new Bitmap object, may be larger than input image</returns>&nbsp; public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bool clipOk, Color backgroundColor)&nbsp; {&nbsp; &nbsp; &nbsp;// Test for zero rotation and return a clone of the input image&nbsp; &nbsp; &nbsp;if (angleDegrees == 0f)&nbsp; &nbsp; &nbsp; &nbsp; return (Bitmap)inputImage.Clone();&nbsp; &nbsp; &nbsp;// Set up old and new image dimensions, assuming upsizing not wanted and clipping OK&nbsp; &nbsp; &nbsp;int oldWidth = inputImage.Width;&nbsp; &nbsp; &nbsp;int oldHeight = inputImage.Height;&nbsp; &nbsp; &nbsp;int newWidth = oldWidth;&nbsp; &nbsp; &nbsp;int newHeight = oldHeight;&nbsp; &nbsp; &nbsp;float scaleFactor = 1f;&nbsp; &nbsp; &nbsp;// If upsizing wanted or clipping not OK calculate the size of the resulting bitmap&nbsp; &nbsp; &nbsp;if (upsizeOk || !clipOk)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; double angleRadians = angleDegrees * Math.PI / 180d;&nbsp; &nbsp; &nbsp; &nbsp; double cos = Math.Abs(Math.Cos(angleRadians));&nbsp; &nbsp; &nbsp; &nbsp; double sin = Math.Abs(Math.Sin(angleRadians));&nbsp; &nbsp; &nbsp; &nbsp; newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);&nbsp; &nbsp; &nbsp; &nbsp; newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;// If upsizing not wanted and clipping not OK need a scaling factor&nbsp; &nbsp; &nbsp;if (!upsizeOk && !clipOk)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);&nbsp; &nbsp; &nbsp; &nbsp; newWidth = oldWidth;&nbsp; &nbsp; &nbsp; &nbsp; newHeight = oldHeight;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;// Create the new bitmap object. If background color is transparent it must be 32-bit,&nbsp;&nbsp; &nbsp; &nbsp;//&nbsp; otherwise 24-bit is good enough.&nbsp; &nbsp; &nbsp;Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);&nbsp; &nbsp; &nbsp;newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);&nbsp; &nbsp; &nbsp;// Create the Graphics object that does the work&nbsp; &nbsp; &nbsp;using (Graphics graphicsObject = Graphics.FromImage(newBitmap))&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.SmoothingMode = SmoothingMode.HighQuality;&nbsp; &nbsp; &nbsp; &nbsp; // Fill in the specified background color if necessary&nbsp; &nbsp; &nbsp; &nbsp; if (backgroundColor != Color.Transparent)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;graphicsObject.Clear(backgroundColor);&nbsp; &nbsp; &nbsp; &nbsp; // Set up the built-in transformation matrix to do the rotation and maybe scaling&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);&nbsp; &nbsp; &nbsp; &nbsp; if (scaleFactor != 1f)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;graphicsObject.ScaleTransform(scaleFactor, scaleFactor);&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.RotateTransform(angleDegrees);&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);&nbsp; &nbsp; &nbsp; &nbsp; // Draw the result&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; graphicsObject.DrawImage(inputImage, 0, 0);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;return newBitmap;&nbsp; }这是从StackOverflow和其他地方获得许多灵感的结果。Naveen在此线程上的回答特别有用。

翻翻过去那场雪

简单方法:public Image RotateImage(Image img){&nbsp; &nbsp; var bmp = new Bitmap(img);&nbsp; &nbsp; using (Graphics gfx = Graphics.FromImage(bmp))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; gfx.Clear(Color.White);&nbsp; &nbsp; &nbsp; &nbsp; gfx.DrawImage(img, 0, 0, img.Width, img.Height);&nbsp; &nbsp; }&nbsp; &nbsp; bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);&nbsp; &nbsp; return bmp;}
随时随地看视频慕课网APP
我要回答