以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]图片旋转问题  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=65421)

--  作者:pcxjxjhkw
--  发布时间:2015/3/16 9:24:00
--  [求助]图片旋转问题
如何将图片旋转指定角度?谢谢。
如:旋转1度。

--  作者:Bin
--  发布时间:2015/3/16 9:28:00
--  
http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&Id=60177&page=2
--  作者:pcxjxjhkw
--  发布时间:2015/3/16 9:30:00
--  
这个实例也是我做的,是旋转90,180,270度,
我现在想实现,旋转任意角度。

--  作者:Bin
--  发布时间:2015/3/16 9:35:00
--  
http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=44191&skin=0
--  作者:有点甜
--  发布时间:2015/3/16 10:35:00
--  

 呃,弄错了对象......汗.....

 

Dim bmpSource As Image = getimage("d:\\test.jpg")
Dim bmpSrc As new Bitmap(bmpSource.Width, bmpSource.Height)
Dim g = Graphics.FromImage(bmpSrc)

Dim w As Integer = bmpSource.Width
Dim h As Integer = bmpSource.Height

g.TranslateTransform(w/2, h/2)
g.RotateTransform(20)
g.TranslateTransform(-w/2, -h/2)
g.DrawImage(bmpSource, 0, 0)

bmpSrc.Save("d:\\test2.jpg")

g.Dispose()
bmpSrc.Dispose


--  作者:pcxjxjhkw
--  发布时间:2015/3/16 15:14:00
--  
甜老师,按你的方法,存在的一个问题,旋转后图片的四角被截了,不能完整显示。
--  作者:pcxjxjhkw
--  发布时间: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编辑过]

--  作者:有点甜
--  发布时间:2015/3/16 15:35:00
--  

Dim bmpSource As Image = getimage("d:\\test.jpg")
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:\\test2.jpg")

g.Dispose()
bmpSrc.Dispose

 


--  作者:pcxjxjhkw
--  发布时间: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
--  发布时间:2015/3/16 15:49:00
--  
另:如何定义颜色?
dim bkColo as ???