裁剪图像中心

3
假设我有一张400x300像素的图片,我想将其裁剪为200x200像素并在服务器端(C#,.NET 4.0)居中。我该如何做?使用类似画布移动的方法吗?有任何教程/代码示例/建议吗?

我认为这与此问题相似:https://dev59.com/wnXYa4cB1Zd3GeqP3jax#27164374 - h3n
3个回答

5
试试这样做:
        Bitmap sourceImage = ...;

        int targetWidth = 200;
        int targetHeight = 200;

        int x = sourceImage.Width / 2 - targetWidth / 2;
        int y = sourceImage.Height / 2 - targetHeight / 2;

        Rectangle cropArea = 
            new Rectangle(x, y, targetWidth, targetHeight);

        Bitmap targetImage = 
            sourceImage.Clone(cropArea, sourceImage.PixelFormat);

如果源图像比目标图像大小小,那么这显然会失败,但你懂的。


0

如果需要,此方法将保存在中心裁剪的图像:

bool SaveCroppedImage(Image image, int targetWidth, int targetHeight, string filePath)
{
    ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/jpeg").First();
    Image finalImage = image;
    System.Drawing.Bitmap bitmap = null;
    try
    {
        int left = 0;
        int top = 0;
        int srcWidth = targetWidth;
        int srcHeight = targetHeight;
        bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
        double croppedHeightToWidth = (double)targetHeight / targetWidth;
        double croppedWidthToHeight = (double)targetWidth / targetHeight;

        if (image.Width > image.Height)
        {
            srcWidth = (int)(Math.Round(image.Height * croppedWidthToHeight));
            if (srcWidth < image.Width)
            {
                srcHeight = image.Height;
                left = (image.Width - srcWidth) / 2;
            }
            else
            {
                srcHeight = (int)Math.Round(image.Height * ((double)image.Width / srcWidth));
                srcWidth = image.Width;
                top = (image.Height - srcHeight) / 2;
            }
        }
        else
        {
            srcHeight = (int)(Math.Round(image.Width * croppedHeightToWidth));
            if (srcHeight < image.Height)
            {
                srcWidth = image.Width;
                top = (image.Height - srcHeight) / 2;
            }
            else
            {
                srcWidth = (int)Math.Round(image.Width * ((double)image.Height / srcHeight));
                srcHeight = image.Height;
                left = (image.Width - srcWidth) / 2;
            }
        }
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(left, top, srcWidth, srcHeight), GraphicsUnit.Pixel);
        }
        finalImage = bitmap;
    }
    catch { }
    try
    {
        using (EncoderParameters encParams = new EncoderParameters(1))
        {
            encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);
            //quality should be in the range [0..100] .. 100 for max, 0 for min (0 best compression)
            finalImage.Save(filePath, jpgInfo, encParams);
            return true;
        }
    }
    catch { }
    if (bitmap != null)
    {
        bitmap.Dispose();
    }
    return false;
}

-1
创建一个新的位图对象,指定目标大小。
在该位图周围创建一个图形对象。
在该图形对象上,使用适当的参数调用DrawImage(),这将从第一张图片中剪切出正确的片段。
代码将类似于:
Bitmap dstBitmap=new Bitmap(200, 200);
using (Graphics g=Graphics.FromImage(dstBitmap)) 
{
    srcBitmap.DrawImage(dstBitmap, /* cropping parameters here */);
}
//  at the end you'll have your bitmap in dstBitmap, ...

我没有为方法包含字面参数,请使用智能感知和手动方式来确定它们。


你在这里所说的“裁剪参数”是什么意思? - markzzz
尝试自己找出参数。DrawImage() 方法有几个重载,你需要找到适合你的一个。你需要定义一个矩形,其中包含你想要剪切出来的图像部分的坐标。 - Daniel Mošmondor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接