按比例调整图片框的大小以调整表格大小 [英] resizing pictureBox proportionally to Form resizing

查看:61
本文介绍了按比例调整图片框的大小以调整表格大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次,用户调整 Form 的大小,pictureBox 中的 Image,也使用相同的值(按比例)调整大小,

我在互联网上搜索了一些代码,并在 StackOverFlow 中找到了这个答案https://stackoverflow.com/a/6501997/3264464

static public Bitmap ScaleImage(Image image, int maxWidth, int maxHeight){var ratioX = (double)maxWidth/image.Width;var ratioY = (double)maxHeight/image.Height;var ratio = Math.Min(ratioX, ratioY);var newWidth = (int)(image.Width * ratio);var newHeight = (int)(image.Height * ratio);var newImage = new Bitmap(newWidth, newHeight);Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);Bitmap bmp = new Bitmap(newImage);返回 bmp;}

我为我的代码添加了函数,但不确定 MaxHeight、MaxWidth 的事情,我的意思是为什么我需要通过参数发送它

Form1_Resize 事件处理程序中我写道:

private void Form1_Resize(object sender, EventArgs e){Bitmap NewImg = ScaleImage(pictureBox1.Image, 1000, 1000);pictureBox1.Image = NewImg;}

但它不起作用..调整表单大小时什么也没发生

更新:尝试了所有相同的结果

看下面的图片,黑点是PictureBox的左边,它不能移动,你建议的很好,但我想要,图片的左边在开始时保持在同一点

调整大小之前:

调整大小后

解决方案

在 Winforms 中,您可以使用 Picturebox 属性来执行此操作,无需代码:

添加图片框并进入控件属性

这为您提供了 5 个选择.在下面的Winform上的效果是相同的:

  • Normal 仅显示图像并适合(我相信从像素 0,0 开始),无需缩放或移动.
  • StretchImage 将缩放并强制调整图像,即使这意味着扭曲图像
  • 在这种情况下,AutoSize 显示完整图像,在这种情况下,图像比表单大,这就是为什么只有一个巨大的蓝色块.
  • 中心图像保持控件完好无损并显示图像的中心区域
  • Zoom 可放大或缩小图像,以便在图片框控件中完整显示.
<小时>

将此与控件的anchordock 结合使用,可将控件保持在您想要的位置,并缩放图像.

听起来您可能想要 Zoom.

放大动作,将锚点设置为所有边(并添加了一个分组框来模拟控制框:

然后我可以调整表单的大小,在这种情况下是最大化的,PictureBox 控件无需额外代码即可自行处理:

注意,因为我使用的是非常矩形的图像,所以我设置了灰色背景以显示控件与图像的位置.您可以设置控件颜色背景以使其不那么明显,或者使用拉伸图像而不是缩放来强制图像始终填充控件,尽管这会在非方形图像上产生大量难看的伪像.>

拉伸工件示例:

I want everytime , the user resizes the Form , the Image in the pictureBox , also resizes with the same Values ( proportionally ) ,

I searched on the internet for some codes and found this answer in StackOverFlow https://stackoverflow.com/a/6501997/3264464

static public Bitmap ScaleImage(Image image, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);
    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);
    var newImage = new Bitmap(newWidth, newHeight);
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
    Bitmap bmp = new Bitmap(newImage);
    return bmp;
}

I added the function for my code , and am not sure about the MaxHeight,MaxWidth thing , i mean why do i need to send it via parameters

and In the Form1_Resize event handler i wrote:

private void Form1_Resize(object sender, EventArgs e)
{
    Bitmap NewImg = ScaleImage(pictureBox1.Image, 1000, 1000);
    pictureBox1.Image = NewImg;
}

but It won't work .. Nothing Happens when I resize the form

UPDATE: Tried everything with same results

Look at the pictures below , The black point is the Left of the PictureBox and it must not move , what you suggested is good , but i want , The left of the pictures stays on the same point at the beggining

Before Resize:

After Resize

解决方案

In Winforms you can use the Picturebox properties to do this, without code:

Add a picture box and go the control properties

This gives you 5 choices. The effect of which are of the same image on a Winform below:

  • Normal just shows the image and fits (I believe from pixel 0,0) at no scaling or moving.
  • StretchImage will scale and force a fit of the image, even if it means skewing the image
  • AutoSize in this case shows the full image, in this case the image is bigger than the form which is why there is just a huge blue chunk.
  • Center image leaves the control intact and shows the center region of the image
  • Zoom zooms in or out of the image so that it shows in its entirety in the picturebox control.

Use this in combination with anchor or dock of the control to keep the control where you want it, and also to scale the image.

It sounds like you might want Zoom.

Zoom in action, with Anchor points set to all sides (And a group box added to simulate a control box:

I can then resize the form, in this case maximized and the PictureBox control takes care of itself with out additional code:

Note, because I am using a very rectangular image I have set a gray background to show where the control is versus the image. You could set control color background to make this less obvious, or use stretchImage instead of zoom to force the image to always fill the control, although this creates plenty of ugly artifacts on non-square images.

example of stretched artifacts:

这篇关于按比例调整图片框的大小以调整表格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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