尝试在图片框中旋转图像时出现小错误。 一切正常。但是在旋转时,它并不能完美地围绕中心旋转。它有点偏离(不是很明显)但有点烦人。这是我的代码:
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;
}
图片显示我的意思:
它不能完美旋转。有针对这个的解决方法吗?我没有为我的图片框属性设置什么?我已经尝试了很多。
谢谢。
请您参考如下方法:
我找到了一个从中心获取旋转图像的解决方案
[我的测试条件]
我正在根据下一个考虑因素对此进行测试,如果它对你有用就没问题:3
1.-我使用的源图像是一个完美的正方形[LxL],取自png文件,正方形的大小无关紧要,只需要LxL;//(宽度 == 高度) = true;
2.- 我正在旋转的 png 源图像文件是透明的方形的,我从 illustrator 那里得到的
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;
}
}
}
我只是给这个函数取名为“RotateImageN”,因为这是我尝试过的第 5 个解决方案:3 我希望对您有所帮助
对不起我的英语和/或语法呵呵:3
