在图片框中显示图像比例的方法 [英] Way to display image scale in picturebox

查看:54
本文介绍了在图片框中显示图像比例的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在图片框中添加了图像缩放功能.我想显示图像的比例(如在地图的左下角).我该怎么办?

I have added image zoom functionality in my picturebox. I would like to display the scale of the image (like in bottom left of maps). How would I accomplish that?

推荐答案

您可以使用 Paint 事件在顶部绘制比例.

You can use the Paint event to draw the scale on top.

假设缩放在x和y方向上是对称的,这将是一个示例:

Assuming the zoom is symmetrical in x- and y-direction this would be an example:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    float zoom =100f *  pictureBox1.ClientSize.Width / pictureBox1.Image.Width;
    // this may or may not be necessary, depending on how to have implemented zooming!
    if (pictureBox1.SizeMode == PictureBoxSizeMode.Normal) zoom = 100f;

    string s = zoom.ToString("#.#")+ "%";
    StringFormat format = new StringFormat()
        { LineAlignment = StringAlignment.Far,
        Alignment = StringAlignment.Far };
    Rectangle bounds = pictureBox1.ClientRectangle;
    Rectangle bounds1 = pictureBox1.ClientRectangle;
    bounds1.Offset(1, 1);

    using (Font font = new Font("Consolas", 14f))
    {
        e.Graphics.DrawString(s, font, Brushes.White, bounds , format);
        e.Graphics.DrawString(s, font, Brushes.Black, bounds1 , format);
    }
}

您可以选择自己喜欢的路线.

You can pick the alignments to you liking.

请注意,我绘制了两次字符串,一次为白色,一次为黑色,偏移了1个像素,以允许任何背景.

Note that I draw the string twice, once in white and once in black, offset by 1 pixel to allow for any backgrounds..

还要注意, Image 是oistrich;风景是(未缩放的 BackgroundImage

Also note the the Image is the oistrich; the landscape is the (unscaled BackgroundImage!

这篇关于在图片框中显示图像比例的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆