Nol*_*dge 1 c# rotation picturebox
尝试在图片框中旋转图像时出现小错误。这一切都有效。但是在旋转时,它不会完美地围绕中心旋转。它略微偏离(不是很明显)但有点烦人。这是我的代码:
private readonly Bitmap _origPowerKnob = Properties.Resources.PowerKnob;
//CODE WHERE ROTATE METHOD IS CALLED//
using (Bitmap b = new Bitmap(_origPowerKnob))
{
Bitmap newBmp = RotateImage(b, _powerAngle);
PowerKnob.BackgroundImage = newBmp;
}
private Bitmap RotateImage(Bitmap b, float angle)
{
//Create a new empty bitmap to hold rotated image.
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//Make a graphics object from the empty bitmap.
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image.
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TranslateTransform((float) b.Width / 2, (float)b.Height / 2);
//Rotate.
g.RotateTransform(angle);
//Move image back.
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//Draw passed in image onto graphics object.
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}
Run Code Online (Sandbox Code Playgroud)
图片显示了我的意思:


它不能完美旋转。这个问题有方法解决吗?我还没有为我的图片框属性设置什么?我已经尝试了很多。
谢谢。
小智 9
我找到了一个从它的中心获取旋转图像的解决方案
我正在根据下一个注意事项对此进行测试,如果它对您有用,则可以:3
1.-我使用的源图像是一个完美的正方形[LxL],取自一个png文件,正方形的大小无关紧要,只需要LxL;//(宽度==高度)=真;
2.-我旋转的png源图像文件是透明的方形,我从插画家那里得到的
3.- 我测试了大小为 417x417、520x520 和 1024x1024 的文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace YourNamespace
{
public static class TheClassinWhichYouWantToPlaceTheFunction
{
public static Bitmap RotateImageN(Bitmap b, float angle)
{
//Create a new empty bitmap to hold rotated image.
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//Make a graphics object from the empty bitmap.
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image.
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//Rotate.
g.RotateTransform(angle);
//Move image back.
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//Draw passed in image onto graphics object.
//Found ERROR 1: Many people do g.DwarImage(b,0,0); The problem is that you re giving just the position
//Found ERROR 2: Many people do g.DrawImage(b, new Point(0,0)); The size still not present hehe :3
g.DrawImage(b, 0,0,b.Width, b.Height); //My Final Solution :3
return returnBitmap;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我只是给函数命名了“RotateImageN”,因为这是我尝试过的第 5 个解决方案:3 我希望能有所帮助
对不起,我的英语和/或语法嘿嘿:3