Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像适配到边界框,并用C中的黑色填充其余部分_C#_.net_Image_Transparency_Resize - Fatal编程技术网

C# 将图像适配到边界框,并用C中的黑色填充其余部分

C# 将图像适配到边界框,并用C中的黑色填充其余部分,c#,.net,image,transparency,resize,C#,.net,Image,Transparency,Resize,我有一个功能: private static Image ScaleAndPadBlack(Image original_img, Thumbnail thumb) { if (thumb.OptimalSizeHeight <= 0) { throw new ArgumentOutOfRangeException("OptimalSizeHeight", "OptimalSizeHeight must be decl

我有一个功能:

    private static Image ScaleAndPadBlack(Image original_img, Thumbnail thumb)
    {
        if (thumb.OptimalSizeHeight <= 0)
        {
            throw new ArgumentOutOfRangeException("OptimalSizeHeight", "OptimalSizeHeight must be declared and positive when using the ScaleAndCrop method.");
        }
        if (thumb.OptimalSizeWidth <= 0)
        {
            throw new ArgumentOutOfRangeException("OptimalSizeWidth", "OptimalSizeWidth must be declared and positive when using the ScaleAndCrop method.");
        }

        Size dest_size = new Size(thumb.OptimalSizeWidth, thumb.OptimalSizeHeight);

        decimal act_width_rate = (decimal)dest_size.Width / (decimal)original_img.Width;
        decimal act_height_rate = (decimal)dest_size.Height / (decimal)original_img.Height;

        decimal scale_rate;
        if (act_width_rate <= act_height_rate)
            scale_rate = act_width_rate;
        else
            scale_rate = act_height_rate;

        Size act_size = new Size(Convert.ToInt32(original_img.Width * scale_rate), Convert.ToInt32(original_img.Height * scale_rate));

        var res = resizeImage(original_img, act_size);

        var b = new Bitmap(thumb.OptimalSizeWidth, thumb.OptimalSizeHeight);
        var g = Graphics.FromImage(b);

        Point p = new Point(((dest_size.Width - act_size.Width) / 2), ((dest_size.Height - act_size.Height) / 2));
        g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), dest_size));
        g.SmoothingMode = SmoothingMode.None;
        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        g.DrawImage(res, p);
        res.Dispose();
        res = null;
        return b;
    }
这样做的目的是调整图像大小以适应边界框,创建一个新图像,用黑色填充,然后将调整大小的图像放到黑色图像上

问题在于,将全白色jpg 761x896图像调整为270x180边界框会导致以下情况:

您可以看到,图像顶部有一个灰色条,这是因为调整大小后的图像具有透明的边缘

为什么会这样?调整图像大小会产生透明边缘吗?在这种情况下,这显然是不可取的

或者说是因为其他原因导致的

我应该如何写这个函数,在这样的情况下没有黑灰色的边缘?我只想填充没有图像的部分,我应该做一些完全不同的事情吗

编辑:


g.SmoothingMode=SmoothingMode.None;g、 插值模式=插值模式。最近的邻居;是因为我在测试这个问题。它们似乎无关紧要。

我已将drawimage和fillrectangle部分更改为:

        g.DrawImage(res, p);            
        //left
        g.FillRectangle(Brushes.Black, new Rectangle(0, 0, p.X, dest_size.Height));
        //right
        g.FillRectangle(Brushes.Black, new Rectangle(p.X + act_size.Width, 0, p.X, dest_size.Height));
        //up
        g.FillRectangle(Brushes.Black, new Rectangle(0, 0, dest_size.Width, p.Y));
        //down
        g.FillRectangle(Brushes.Black, new Rectangle(0, p.Y + act_size.Width, dest_size.Width, p.Y));

它似乎与我的测试用例一起工作。我仍然不确定是什么原因导致了最初的问题。

您确定这是一个透明的边缘吗?您可以看到生成的图片。我不确定。我在等解释。