【问题标题】:Rotating picture in C# cuts the picture [duplicate]C#中的旋转图片剪切图片[重复]
【发布时间】:2013-03-13 14:33:12
【问题描述】:

我正在尝试使用 C# 旋转图片,并且正在使用以下代码:

///create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(newBMP.Width, newBMP.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image
g.TranslateTransform((float)newBMP.Width / 2, (float)newBMP.Height / 2);
//rotate
g.RotateTransform(-90);
//move image back
g.TranslateTransform(-(float)newBMP.Width / 2, -(float)newBMP.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(newBMP, new Point(0, 0)); 

newBMP 是我从表单中获取的位图,我正在更改它的大小。然后我想旋转它,但是当我尝试上面的代码时,它会切割图片的顶部和底部。 完成所有这些后,我将新图片保存在服务器上。

一切正常,除了旋转...

有人发现问题了吗?

解决了我用这个:C# rotate bitmap 90 degrees

【问题讨论】:

  • 如果旋转 90°,确定宽度和高度或returnBitmap 需要交换?
  • 我同意前面的评论。您可以将宽度和高度更改为 90°、180°。对于任何学位,请在此处查看我的解决方案:stackoverflow.com: C#, rotating Graphics?

标签: c# .net image


【解决方案1】:

如果位图的宽度大于高度,则在将其旋转 90 度时会进行裁剪。当您致电g.TranslateTranform 时,您需要考虑到这一点。

【讨论】:

  • 所以我不明白我应该改变什么......
【解决方案2】:

这个答案刚刚在堆栈溢出时为 vb.net 回答(对于任何角度)...

How to rotate JPEG using Graphics.RotateTransform without clipping

应该很容易转换为 C#

【讨论】:

    【解决方案3】:
     public static Bitmap RotateImage(Bitmap image, float angle)
        {
            //create a new empty bitmap to hold rotated image
    
            double radius = Math.Sqrt(Math.Pow(image.Width, 2) + Math.Pow(image.Height, 2));
    
            Bitmap returnBitmap = new Bitmap((int)radius, (int)radius);
    
            //make a graphics object from the empty bitmap
            using (Graphics graphic = Graphics.FromImage(returnBitmap))
            {
                //move rotation point to center of image
                graphic.TranslateTransform((float)radius / 2, (float)radius / 2);
                //rotate
                graphic.RotateTransform(angle);
                //move image back
                graphic.TranslateTransform(-(float)image.Width / 2, -(float)image.Height / 2);
                //draw passed in image onto graphics object
                graphic.DrawImage(image, new Point(0, 0));
            }
            return returnBitmap;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多