【问题标题】:Resize and Fit Image in WPF在 WPF 中调整图像大小和适合图像
【发布时间】:2013-07-20 12:40:12
【问题描述】:

我有以下几点:

using (bmp == new Bitmap(50, 50)) {
    using (g == Graphics.FromImage(bmp)) {
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, 0, 0, 50, 50);
    }
}

用户向我的应用提供了一张图片,我的应用必须调整该图片的大小并适合整个图片。

上述代码的问题在于它拉伸图像,而不是适合(因为 4:3 电影适合宽屏电视留下黑条)。 p>

有人对此有任何解决方案吗?另外,我不想使用 GDI。

【问题讨论】:

标签: c# .net wpf vb.net image


【解决方案1】:

您需要考虑输入图像的大小。这是一个代码 sn-p 应该可以让您进入正确的方向:

int x1 = 0, y1 = 0, x2 = 50, y2 = 50;
if (img. Width <= img.Height) 
{
   // compute x1, y1 to fit img horizontally into bmp
}
else
{
   // compute y1, y2 to fit img vertically into bmp
}

g.DrawImage(img, x1,y1, x2,y2);

另请注意,您要求在 WPF 中调整图像大小,但使用 System.Drawing,后者在后台使用 GDI+。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2012-05-05
    • 2013-12-21
    相关资源
    最近更新 更多