在图片框中显示图像比例的方法

时间:2017-02-21 14:00:01

标签: c# image winforms user-interface

我在我的图片框中添加了图片缩放功能。我想显示图像的比例(如地图左下角)。我将如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

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

假设变焦在x和y方向上是对称的,这就是一个例子:

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

您可以根据自己的喜好选择对齐方式。

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

另请注意Image是oistrich;景观是(未缩放的BackgroundImage

enter image description here enter image description here