代码之家  ›  专栏  ›  技术社区  ›  Chaddeus

在C#中,如何在没有空格填充的情况下对上传的照片进行方格处理?

  •  3
  • Chaddeus  · 技术社区  · 15 年前

    在我的网站上,用户可以上传照片。我目前压缩和调整照片大小,以确保他们不是巨大的文件。但这会产生不同尺寸的照片。。。这使得这个网站在我看来有点“丑陋”。

    我想确保缩略图是正方形的图像,但不使用填充。如果缩略图中的照片丢失了,也没关系。我想保持照片的保真度高,即使这意味着一些裁剪需要发生。

    4 回复  |  直到 7 年前
        1
  •  3
  •   Adrian    15 年前

    我写了一些代码来做这件事。我选择裁剪它是因为在不保留纵横比的情况下调整大小看起来非常可怕。我进行裁剪,然后调整大小以创建thumnail图像:

      public Bitmap CreateThumbnail(Bitmap RawImage)
        {
            int width = RawImage.Width;
            int height = RawImage.Height;
    
            Size ThumbnailDimensions = new Size();
            ThumbnailDimensions.Width = 100;
            ThumbnailDimensions.Height = 100;
    
            Rectangle cropArea = new Rectangle();
            if (width > height)
            {               
                cropArea.Width = height;
                cropArea.Height = height;
                cropArea.X = 0;
                cropArea.Y = 0;                
            }
            else if (width < height)
            {
                cropArea.Width = width;
                cropArea.Height = width;
                cropArea.X = 0;
                cropArea.Y = 0;
            }
            if(width != height) Bitmap thumbnail = CropImage(RawImage, cropArea);
    
            thumbnail = ResizeImage(thumbnail, ThumbnailDimensions);
            return thumbnail;
        }
    

    这只是从左上角裁剪,然后将其调整为缩略图尺寸。

        2
  •  2
  •   Community Mohan Dere    8 年前

    我可以想象你需要采取最短的维度(或者 w this article 例如裁剪图像。还可以结账 this Stack Overflow question 关于图像质量。

        3
  •  -1
  •   µBio    15 年前

    你打算怎么修剪它?从左上角?右下角?中心?

        4
  •  -1
  •   Lou Franco    15 年前

    要将矩形变成正方形,您需要在不保留纵横比或裁剪(或组合)的情况下进行填充、调整大小。

    http://snippets.dzone.com/posts/show/1484

    (我为Atalasoft工作——在我们的DotImage照片SDK中),它是

    AtalaImage img = new AtalaImage("filename.jpg");
    AtalaImage img2 = new CropCommand( /*point and size of crop */).Apply(img).Image;
    img2.Save("filename", new JpegEncoder(quality), null);