如何调整图形大小
本文关键字:图形 调整 何调整 | 更新日期: 2023-09-27 17:57:04
我有两个图片框"source"和"dest"。现在我想调整"源"图像的大小并将其显示在我的"目标"图片框中。
问题:如何调整"源"图像的大小并将其显示在"目标"图片框中?
这是我的代码,它只再次显示相同的图像:
private void pictureBoxZoom_Paint(object sender, PaintEventArgs e)
{
var settings = new Settings();
// Create a local version of the graphics object for the PictureBox.
Graphics Draw = e.Graphics;
Draw.ScaleTransform(2, 2); // rescale by factor 2
IntPtr hDC = Draw.GetHdc(); // Get a handle to pictureBoxZoom.
Draw.ReleaseHdc(hDC); // Release pictureBoxZoom handle.
}
在每种情况下,您都必须处理 PictureBox.SizeMode-Property。它决定了绘画时图片框对图像的作用。
如果要应用自定义的缩放因子,可以使用 Graphics.ScaleTransform-Property。它允许您缩放完整的图形。缩放可以通过创建位图(其中包含您可以使用的图形对象)或重写 PictureBox.OnPaint-Method(这要求您创建从 PictureBox 派生的类)来完成。这是最灵活的尝试,因为如果你愿意的话,你可以自己控制一切。
编辑:很抱歉不够清楚,使用 Paint
-event 的方式不起作用。您需要创建自己的PictureBox
类(派生自 PictureBox)并重写 OnPaint
-方法。然后,您可以在 PictureBox 使用的 Graphics
对象上调用 ScaleTransform
。我为您准备了一个经过测试的样品:
public class PictureBoxScaleable : PictureBox
{
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.ScaleTransform(.2f, .2f);
base.OnPaint(pe);
}
}