【问题标题】:resize image c# / asp.net to specific format (widescreen 16:9)将图像 c# / asp.net 调整为特定格式(宽屏 16:9)
【发布时间】:2011-07-03 14:10:23
【问题描述】:

我正在寻找一种方法,以固定比例(例如 16:9)将图像调整大小并将其裁剪为特定大小。所以我有任何图像和任何尺寸,它应该以 16:9 的比例调整和裁剪。

只是裁剪不是很好。我希望图像的大小可以调整得更多,如有必要,可能会被裁剪。或者更好:我想尽可能多地重用原始图像,但调整大小并居中裁剪它,以便我可以在固定的 html div 中以 16:9 的比例使用它。

谢谢!

【问题讨论】:

  • 努力提高你的接受分数!!我知道答案,但会等你把分数提高一点再回复。

标签: asp.net resize thumbnails


【解决方案1】:

这是我多次使用的解决方案。这应该首先调整大小,然后裁剪任何多余的尺寸。图片不会被拉伸。

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

protected Image imageCrop(Image image, int width, int height, AnchorPosition anchor)
{
    int sourceWidth = image.Width;
    int sourceHeight = image.Height;
    int sourceX = 0;
    int sourceY = 0;
    int destX = 0;
    int destY = 0;

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

    nPercentW = (Convert.ToSingle(width) / Convert.ToSingle(sourceWidth));
    nPercentH = (Convert.ToSingle(height) / Convert.ToSingle(sourceHeight));

    if (nPercentH < nPercentW)
    {
        nPercent = nPercentW;
        switch (anchor) {
            case AnchorPosition.Top:
                destY = 0;
                break;
            case AnchorPosition.Bottom:
                destY = Convert.ToInt32(height - (sourceHeight * nPercent));
                break;
            default:
                destY = Convert.ToInt32((height - (sourceHeight * nPercent)) / 2);
                break;
        }
    }
    else
    {
        nPercent = nPercentH;
        switch (anchor) {
            case AnchorPosition.Left:
                destX = 0;
                break;
            case AnchorPosition.Right:
                destX = Convert.ToInt32((width - (sourceWidth * nPercent)));
                break;
            default:
                destX = Convert.ToInt32(((width - (sourceWidth * nPercent)) / 2));
                break;
        }
    }

    int destWidth = Convert.ToInt32((sourceWidth * nPercent));
    int destHeight = Convert.ToInt32((sourceHeight * nPercent));

    Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
    grPhoto.DrawImage(image, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
    grPhoto.Dispose();

    return bmPhoto;
}

public enum Dimensions
{
    Width,
    Height
}

public enum AnchorPosition
{
    Top,
    Center,
    Bottom,
    Left,
    Right
}

这是一个调用函数的例子:

Image image = image.FromFile(imagePath);
Image thumb = imageCrop(image, 100, 100, AnchorPosition.Top);

【讨论】:

    【解决方案2】:

    我做了类似的事情来创建标准尺寸的缩略图。这是代码的链接(在博客文章中)。

    http://blog.bobcravens.com/2009/07/mobileme-scrolling-image-viewer/

    概念是一样的。我使用了一些逻辑来根据尺寸自动选择要裁剪的位图区域。

    希望这会有所帮助。

    鲍勃

    【讨论】:

      【解决方案3】:

      您可以使用此代码:

       Size NewSize = new Size();
                      NewSize.Width = 70;
                      NewSize.Height = 60;
                      System.Drawing.Image small = resizeImage(Image.FromFile(source), NewSize);
                      small.Save(your path);
      

      //这是调整大小的函数

          public System.Drawing.Image resizeImage(Image imgToResize, Size size)
              {
                  int sourceWidth = imgToResize.Width;
                  int sourceHeight = imgToResize.Height;
                  float nPercent = 0;
                  float nPercentW = 0;
                  float nPercentH = 0;
                  nPercentW = ((float)size.Width / (float)sourceWidth);
                  nPercentH = ((float)size.Height / (float)sourceHeight);
                  if (nPercentH < nPercentW) nPercent = nPercentH;
                  else
                      nPercent = nPercentW;
                  int destWidth = (int)(sourceWidth * nPercent);
                  int destHeight = (int)(sourceHeight * nPercent);
                  Bitmap b = new Bitmap(destWidth, destHeight);
                  Graphics g = Graphics.FromImage((Image)b);
                  g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
                  g.Dispose();
                  return (System.Drawing.Image)b;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-06
        • 1970-01-01
        • 2012-08-14
        • 1970-01-01
        • 2011-12-22
        • 2012-06-29
        • 2012-01-24
        • 2016-06-27
        相关资源
        最近更新 更多