自动更改图片框中拉伸图像的字体大小

时间:2012-03-27 16:00:29

标签: c# winforms drawing

我有一个PictureBox,其大小固定为480x360像素。我使用DrawString在图片框中的图像上写了一些文字。如果用户为图片框选择的图像大小已经是480x360,那就没问题了!

当用户添加具有较小或较大尺寸的默认480x360像素的图像时,问题就开始了。在这种情况下,我在图像上写字符串的默认fonr大小将太大或太小。

有没有办法根据图像的宽度和高度选择字体大小?我在节目中使用的图片大多是4:3的比例。

目前我正在使用下面的代码......它在某种程度上有效,但这不是一个好方法。什么是更聪明的方式?!

        private int GetProperFontSize()
    {
        var width = _bitmap.Width;

        if(width > 480 && width <= 680)
        {
            return 20;
        }

        if (width > 680 && width <= 800)
        {
            return 24;
        }

        if (width > 800 && width <= 1024)
        {
            return 32;
        }

        if (width > 1024 && width <= 1600)
        {
            return 44;
        }

        if (width > 1600 && width <= 2048)
        {
            return 50;
        }

        if (width > 2048 && width <= 2560)
        {
            return 66;
        }

        if (width > 2560 && width <= 6000)
        {
            return 80;
        }

        return 16;
    }

1 个答案:

答案 0 :(得分:0)

您可以禁止用户使用较小尺寸的图像...较大尺寸只需将其缩放到约束宽度即可。您还可以尝试设置黑色的背景颜色,使图像居中,然后在文本的底部放置一个白色条带,如宝丽来......

相关问题