Foxtable(狐表)用户栏目专家坐堂 → [求助]图片旋转问题


  共有4245人关注过本帖树形打印复制链接

主题:[求助]图片旋转问题

帅哥哟,离线,有人找我吗?
pcxjxjhkw
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
[求助]图片旋转问题  发帖心情 Post By:2015/3/16 9:24:00 [显示全部帖子]

如何将图片旋转指定角度?谢谢。
如:旋转1度。

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 9:30:00 [显示全部帖子]

这个实例也是我做的,是旋转90,180,270度,
我现在想实现,旋转任意角度。

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 15:14:00 [显示全部帖子]

甜老师,按你的方法,存在的一个问题,旋转后图片的四角被截了,不能完整显示。

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 15:29:00 [显示全部帖子]

甜老师,我在网上找到如下代码,如何转换成狐表代码?  或者封成一个内部函数。谢谢
     /// <summary>
        /// 任意角度旋转
        /// </summary>
        /// <param name="bmp">原始图Bitmap</param>
        /// <param name="angle">旋转角度</param>
        /// <param name="bkColor">背景色</param>
        /// <returns>输出Bitmap</returns>
        public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
        {
            int w = bmp.Width + 2;
            int h = bmp.Height + 2;

            PixelFormat pf;

            if (bkColor == Color.Transparent)
            {
                pf = PixelFormat.Format32bppArgb;
            }
            else
            {
                pf = bmp.PixelFormat;
            }

            Bitmap tmp = new Bitmap(w, h, pf);
            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, 1, 1);
            g.Dispose();
            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(new RectangleF(0f, 0f, w, h));
            Matrix mtrx = new Matrix();
            mtrx.Rotate(angle);
            RectangleF rct = path.GetBounds(mtrx);

            Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
            g = Graphics.FromImage(dst);
            g.Clear(bkColor);
            g.TranslateTransform(-rct.X, -rct.Y);
            g.RotateTransform(angle);
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.DrawImageUnscaled(tmp, 0, 0);
            g.Dispose();
            tmp.Dispose();
            return dst;
   }
[此贴子已经被作者于2015/3/16 15:30:02编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 15:47:00 [显示全部帖子]

甜老师,又出现新问题,执行下面的代码时,图片框的图片显示越来越小。
Dim pic As WinForm.PictureBox = e.Form.Controls("图片编辑框")

'Dim bmpSource As Image = getimage(pic.ImageFile)
Dim bmpSource As Image = pic.Image
MessageBox.Show(pic.ImageFile)
Dim w As Integer = bmpSource.Width
Dim h As Integer = bmpSource.Height
Dim angle As Double = 20
Dim a As Double = angle Mod 360

Dim radian As Double = a * Math.PI / 180.0
Dim cos As Double = Math.Cos(radian)
Dim sin As Double = Math.Sin(radian)
Dim newW As Integer = Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin))
Dim newH As Integer = Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos))
Dim bmpSrc As new Bitmap(newW, newH)
Dim g = Graphics.FromImage(bmpSrc)
g.TranslateTransform(newW/2, newH/2)
g.RotateTransform(angle)
g.TranslateTransform(-newW/2, -newH/2)
Dim x As Integer = (newW-w)/2
Dim y As Integer = (newH-h)/2
g.DrawImage(bmpSource, x, y, w, h)
'bmpSrc.Save("d:\test.jpg")
g.Dispose()
'bmpSrc.Dispose
pic.Image = bmpSrc

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 15:49:00 [显示全部帖子]

另:如何定义颜色?
dim bkColo as ???

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 16:40:00 [显示全部帖子]

甜老师,在原图上操作太麻烦。

可否通过截取多余的背景部分来实现。

 回到顶部
帅哥哟,离线,有人找我吗?
pcxjxjhkw
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/16 17:47:00 [显示全部帖子]

甜老师,按照您的思路,又出现一个新问题。
一图片先水平翻转后,再旋转任意度数,结果只执行将原图旋转任意度数,而水平翻转效果没实现。

 回到顶部