围绕中心C#旋转图像 [英] Rotating image around center C#

查看:93
本文介绍了围绕中心C#旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试旋转图片框内的图像时出现一个小错误. 一切正常.但是,当旋转时,它不能围绕中心完美旋转.它稍微有点偏离(不是很明显),但是有点烦人.这是我的代码:

I have a small error when trying to rotate an image within a picture box. It all works. But when rotating, it doesn't rotate perfectly around the center. It's slightly off (not very noticeable) but kinda annoying. Here is my code:

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;
        }

图片展示了我的意思

旋转不完美.有针对这个的解决方法吗?我尚未为图片框属性设置某些设置?我尝试了很多.

It doesn't rotate perfectly. Is there a solution to this? Something I haven't set for my picturebox properties? I've tried alot.

谢谢.

推荐答案

我找到了一种从其中心获取旋转图像的解决方案

I found a solution to get the rotated image from it's center

如果有问题的话,我正在考虑以下几点:3

I'm testing this with the next considerations, if it works for you its ok :3

1.-我使用的源图像是一个完美的正方形[LxL],取自png文件,无论正方形大小如何,都只需为LxL; //(宽度==高度)= true;

1.-The source image i used is a perfect square[LxL], taked from a png file, size of the square no matter, just needs to be LxL; //(Width == Height) = true;

2.-我正在旋转的png源图像文件是透明且方形的,我是从插画家那里得到的

2.- The png source image file that i am rotating is transparent and square shaped, i got it from illustrator

3.-我测试了尺寸为417x417、520x520和1024x1024的文件

3.- I tested files of size 417x417, and 520x520, and 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",因为这是我尝试过的第五种解决方案:3我希望能对您有所帮助

I just gived the name "RotateImageN" to the function, because is the 5th solution I have tried :3 i Hope to be helpful

对不起,我的英语和/或语法呵呵:3

Sorry for my english and/or grammar hehe :3

这篇关于围绕中心C#旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆