none
图片失真问题。 RRS feed

  • 问题

  • 我现在在做一个项目,就是将一些倾斜的TIF图片调正,然后切需要的部分出来,把新切出来的图片保存为 600 dpi,8 bpp,Lzw压缩的新TIF。在这个过程中发现了一个问题就是,切出来的图片会失真。后来发现,原来图片经过旋转之后,会造成失真的现象。请问大家有没有解决的方法,麻烦指点一二,我感激不尽。谢谢。下面是我用来旋转的算法。

      public Bitmap RotateImage(Image image, float angle)
        {
          if (image == null)
            throw new ArgumentNullException("image");
          if (angle == 0)
            return image as Bitmap;
    
          const double pi2 = Math.PI / 2.0;
    
          double oldWidth = (double)image.Width;//记录下图片原来的宽和高
          double oldHeight = (double)image.Height;
    
          double theta = ((double)angle) * Math.PI / 180.0;//弧度=角度*PI/180;
    
          double locked_theta = theta;
    
          while (locked_theta < 0.0)
            locked_theta += 2 * Math.PI;//locked=locked+2*Math.PI
    
          double newWidth, newHeight;
          int nWidth, nHeight;
    
          double adjacentTop, oppositeTop;
          double adjacentBottom, oppositeBottom;
    
          if ((locked_theta >= 0.0 && locked_theta < pi2) || (locked_theta >= Math.PI && locked_theta < (Math.PI + pi2)))
          {
            adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
            oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth;
    
            adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight;
            oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
          }
          else
          {
            adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
            oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight;
    
            adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth;
            oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
          }
    
          newWidth = adjacentTop + oppositeBottom;
          newHeight = adjacentBottom + oppositeTop;
    
          nWidth = (int)Math.Ceiling(newWidth);
          nHeight = (int)Math.Ceiling(newHeight);
    
          Bitmap rotatedBmp = new Bitmap(nWidth, nHeight);//,image.PixelFormat);
          rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    
          using (Graphics g = Graphics.FromImage(rotatedBmp))
          {
            Point[] points;
            if (locked_theta >= 0.0 && locked_theta < pi2)
            {
              points = new Point[] { 
    											 new Point( (int) oppositeBottom, 0 ), 
    											 new Point( nWidth, (int) oppositeTop ),
    											 new Point( 0, (int) adjacentBottom )
    										 };
    
            }
            else if (locked_theta >= pi2 && locked_theta < Math.PI)
            {
              points = new Point[] { 
    											 new Point( nWidth, (int) oppositeTop ),
    											 new Point( (int) adjacentTop, nHeight ),
    											 new Point( (int) oppositeBottom, 0 )						 
    										 };
            }
            else if (locked_theta >= Math.PI && locked_theta < (Math.PI + pi2))
            {
              points = new Point[] { 
    											 new Point( (int) adjacentTop, nHeight ), 
    											 new Point( 0, (int) adjacentBottom ),
    											 new Point( nWidth, (int) oppositeTop )
    										 };
            }
            else
            {
              points = new Point[] { 
    											 new Point( 0, (int) adjacentBottom ), 
    											 new Point( (int) oppositeBottom, 0 ),
    											 new Point( (int) adjacentTop, nHeight )		
    										 };
            }
    
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(image, points);
          }
          return rotatedBmp;
        }
    

     

    2010年8月7日 2:32

答案

全部回复