简体   繁体   English

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

[英]Automatically change font size for a stretched image in a picturebox

I have a PictureBox that its size is fixed to 480x360 pixels. 我有一个PictureBox ,它的大小固定为480x360像素。 I write some text using DrawString on the image in the picturebox. 我在图片DrawString的图像上使用DrawString编写了一些文本。 If size of the image that user is selecting for the picturebox is already 480x360, it is no problem! 如果用户为图片框选择的图像尺寸已经是480x360,那就没问题了!

Problems starts when user is adding an image with smaller or greated size of default 480x360 pixels. 当用户添加较小或较大的默认480x360像素的图像时,问题就开始了。 In this case the default fonr size that I am writing strings on the images will be either too big or too small. 在这种情况下,我在图像上写入字符串的默认字体大小将太大或太小。

Is there a way to select font size depending on the images width and height? 有没有一种方法可以根据图像的宽度和高度选择字体大小? The pictures I am using in the program mostly are 4:3 ratio. 我在程序中使用的图片大多是4:3的比例。

At the moment I am using the code below...it is somehow working but it is not a good way for doing so. 目前,我正在使用下面的代码...虽然可以正常工作,但这不是一个好方法。 What can be a smarter way?! 有什么更聪明的方法?

        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;
    }

you can prohibit the user from using images of a smaller size...with the larger size simply scale it to the constrained width. 您可以禁止用户使用较小尺寸的图像...较大尺寸的图像只需将其缩放到受限宽度即可。 you can also try things like setting a background color of black, centering the image, and then placing a white band at the bottom for the text like a polaroid... 您还可以尝试将背景颜色设置为黑色,将图像居中,然后在底部为文本放置白色带,例如宝丽来...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM