none
UI的问题 RRS feed

  • 问题

  • 我最近做了一个类似ACDsee的图形处理软件,可是在做图片的放大的问题时,不知怎么处理才能使图片放大不怎么模糊。c#的库有自带的放大功能吗?请教高手们!谢谢
    2009年6月29日 0:45

答案

  • 建议使用放大后不失真的矢量图。位图放大的算法可以去计算机图形学论坛询问。
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
    Visual C++ MVP
    2009年6月29日 16:14
    版主

全部回复

  • 你好 C# 没有现在的方法,需要自己写算法来处理
    知识改变命运,奋斗成就人生!
    2009年6月29日 1:06
    版主
  • 能否具体点,我是刚入门。一开始做的时候,就是简单的要放大的图片的长宽乘以多少倍。能否给点具体的实例。谢谢
    2009年6月29日 1:23
  • using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;

    namespace Tutorial
    {
     class ImageResize
     {
      enum Dimensions
      {
       Width,
       Height
      }
      enum AnchorPosition
      {
       Top,
       Center,
       Bottom,
       Left,
       Right
      }
      [STAThread]
      static void Main(string[] args)
      {
       //set a working directory
       string WorkingDirectory = @"C:\Projects\Tutorials\ImageResize";

       //create a image object containing a verticel photograph
       Image imgPhotoVert = Image.FromFile(WorkingDirectory + @"\imageresize_vert.jpg");
       Image imgPhotoHoriz = Image.FromFile(WorkingDirectory + @"\imageresize_horiz.jpg");
       Image imgPhoto = null;

       imgPhoto = ScaleByPercent(imgPhotoVert, 50);
       imgPhoto.Save(WorkingDirectory + @"\images\imageresize_1.jpg", ImageFormat.Jpeg);
       imgPhoto.Dispose();

       imgPhoto = ConstrainProportions(imgPhotoVert, 200, Dimensions.Width);
       imgPhoto.Save(WorkingDirectory + @"\images\imageresize_2.jpg", ImageFormat.Jpeg);
       imgPhoto.Dispose();

       imgPhoto = FixedSize(imgPhotoVert, 200, 200);
       imgPhoto.Save(WorkingDirectory + @"\images\imageresize_3.jpg", ImageFormat.Jpeg);
       imgPhoto.Dispose();

       imgPhoto = Crop(imgPhotoVert, 200, 200, AnchorPosition.Center);
       imgPhoto.Save(WorkingDirectory + @"\images\imageresize_4.jpg", ImageFormat.Jpeg);
       imgPhoto.Dispose();

       imgPhoto = Crop(imgPhotoHoriz, 200, 200, AnchorPosition.Center);
       imgPhoto.Save(WorkingDirectory + @"\images\imageresize_5.jpg", ImageFormat.Jpeg);
       imgPhoto.Dispose();

      }
      static Image ScaleByPercent(Image imgPhoto, int Percent)
      {
       float nPercent = ((float)Percent/100);

       int sourceWidth = imgPhoto.Width;
       int sourceHeight = imgPhoto.Height;
       int sourceX = 0;
       int sourceY = 0;

       int destX = 0;
       int destY = 0;
       int destWidth  = (int)(sourceWidth * nPercent);
       int destHeight = (int)(sourceHeight * nPercent);

       Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
       bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

       Graphics grPhoto = Graphics.FromImage(bmPhoto);
       grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

       grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX,destY,destWidth,destHeight),
        new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
        GraphicsUnit.Pixel);

       grPhoto.Dispose();
       return bmPhoto;
      }
      static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension)
      {
       int sourceWidth = imgPhoto.Width;
       int sourceHeight = imgPhoto.Height;
       int sourceX = 0;
       int sourceY = 0;
       int destX = 0;
       int destY = 0;
       float nPercent = 0;

       switch(Dimension)
       {
        case Dimensions.Width:
         nPercent = ((float)Size/(float)sourceWidth);
         break;
        default:
         nPercent = ((float)Size/(float)sourceHeight);
         break;
       }
        
       int destWidth  = (int)(sourceWidth * nPercent);
       int destHeight = (int)(sourceHeight * nPercent);

       Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
       bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

       Graphics grPhoto = Graphics.FromImage(bmPhoto);
       grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

       grPhoto.DrawImage(imgPhoto,
       new Rectangle(destX,destY,destWidth,destHeight),
       new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
       GraphicsUnit.Pixel);

       grPhoto.Dispose();
       return bmPhoto;
      }

      static Image FixedSize(Image imgPhoto, int Width, int Height)
      {
       int sourceWidth = imgPhoto.Width;
       int sourceHeight = imgPhoto.Height;
       int sourceX = 0;
       int sourceY = 0;
       int destX = 0;
       int destY = 0;

       float nPercent = 0;
       float nPercentW = 0;
       float nPercentH = 0;

       nPercentW = ((float)Width/(float)sourceWidth);
       nPercentH = ((float)Height/(float)sourceHeight);

       
       if(nPercentH < nPercentW)
       {
        nPercent = nPercentH;
        destX = (int)((Width - (sourceWidth * nPercent))/2);
       }
       else
       {
        nPercent = nPercentW;
        destY = (int)((Height - (sourceHeight * nPercent))/2);
       }
      
       int destWidth  = (int)(sourceWidth * nPercent);
       int destHeight = (int)(sourceHeight * nPercent);

       Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
       bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

       Graphics grPhoto = Graphics.FromImage(bmPhoto);
       grPhoto.Clear(Color.Red);
       grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

       grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX,destY,destWidth,destHeight),
        new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
        GraphicsUnit.Pixel);

       grPhoto.Dispose();
       return bmPhoto;
      }
      static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
      {
       int sourceWidth = imgPhoto.Width;
       int sourceHeight = imgPhoto.Height;
       int sourceX = 0;
       int sourceY = 0;
       int destX = 0;
       int destY = 0;

       float nPercent = 0;
       float nPercentW = 0;
       float nPercentH = 0;

       nPercentW = ((float)Width/(float)sourceWidth);
       nPercentH = ((float)Height/(float)sourceHeight);

       if(nPercentH < nPercentW)
       {
        nPercent = nPercentW;
        switch(Anchor)
        {
         case AnchorPosition.Top:
          destY = 0;
          break;
         case AnchorPosition.Bottom:
          destY = (int)(Height - (sourceHeight * nPercent));
          break;
         default:
          destY = (int)((Height - (sourceHeight * nPercent))/2);
          break;
        }    
       }
       else
       {
        nPercent = nPercentH;
        switch(Anchor)
        {
         case AnchorPosition.Left:
          destX = 0;
          break;
         case AnchorPosition.Right:
          destX = (int)(Width - (sourceWidth * nPercent));
          break;
         default:
          destX = (int)((Width - (sourceWidth * nPercent))/2);
          break;
        }   
       }

       int destWidth  = (int)(sourceWidth * nPercent);
       int destHeight = (int)(sourceHeight * nPercent);

       Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
       bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

       Graphics grPhoto = Graphics.FromImage(bmPhoto);
       grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

       grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX,destY,destWidth,destHeight),
        new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
        GraphicsUnit.Pixel);

       grPhoto.Dispose();
       return bmPhoto;
      }
     }
    }

    你说的是图像放大后不失真,你可以学习一下比较图像插值算法等。有问题在交流!


    ♡. Microsoft .NET Platform
    2009年6月29日 1:34
  • 非常感谢!努力学习
    2009年6月29日 1:45
  • 我是这么用了,没有实现我所需的放大功能啊。我用的是一个按钮来实现的,具体如下:
     private void picBox_Click(object sender, EventArgs e)//该按钮要实现放大功能
            {
               
                Image imgPhotoVert = Image.FromFile(@"F:\风景图片\image1.jpg");//我的图片在F盘
                Image imgPhoto = null;
                imgPhoto = ScaleByPercent(imgPhotoVert, 50);
                imgPhoto.Save(@"F:\风景图片\image1.jpg", ImageFormat.Jpeg);//保存的F盘下
                imgPhoto.Dispose();
             }
    static Image ScaleByPercent(Image imgPhoto, int Percent)
            {
                float nPercent = ((float)Percent / 100);

                int sourceWidth = imgPhoto.Width;
                int sourceHeight = imgPhoto.Height;
                int sourceX = 0;
                int sourceY = 0;

                int destX = 0;
                int destY = 0;
                int destWidth = (int)(sourceWidth * nPercent);
                int destHeight = (int)(sourceHeight * nPercent);

                Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

                grPhoto.DrawImage(imgPhoto,
                 new Rectangle(destX, destY, destWidth, destHeight),
                 new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                 GraphicsUnit.Pixel);

                grPhoto.Dispose();
                return bmPhoto;
            }
    请高手指教,谢谢!

    2009年6月29日 13:25
  • 建议使用放大后不失真的矢量图。位图放大的算法可以去计算机图形学论坛询问。
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
    Visual C++ MVP
    2009年6月29日 16:14
    版主